Problem with read data from txt file

Back to topics list To post a new topic, please log in or register
avatar
29
castann86 2015.05.12 16:47
 

Hi all,

i was studying the article of interaction between matlab and metatrader via csv file.

I create the code in matlab, and matlab create a new txt file with results (In this case, moving average of period 5).

When i try to plot this moving average in Metatrader, i have a error, because it no appear nothing.

I use the following code for this step:

 

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_width1 2
#property indicator_color1 Tomato
extern int length = 100;   // The amount of bars to be sent for processing
double ExtMap[];           // Chart buffer
string nameData;
string nameResult;

int init()
{
   nameData = Symbol()+".txt";         // the name of the data file to be sent
   nameResult = Symbol()+"_result.txt";// the name of the received file containing results
   SetIndexStyle(0, DRAW_LINE);
   SetIndexBuffer(0, ExtMap);
   return(0);
}
int deinit()
  {
   Comment("");
   return(0);
  }
int start()
{
   static int attempt = 0;    // the amount of attempts to read the result
   static int old_bars = 0;   // remember the amount of the known bars
   
   if (old_bars != Bars)      // if a new bar has income 
   {
      FileDelete(nameResult);                   // delete the result file
      ArrayInitialize( ExtMap, EMPTY_VALUE);    // empty the chart
      write_data();                             // save the data file
      
      old_bars = Bars; return(0);               // nothing should be done this time                           
   }
   //
   int handle_read = FileOpen(nameResult,
                              FILE_CSV|FILE_READ,
                              ','); // try to open the result file
   attempt++;                       // count the attempt to open
   
   if(handle_read >= 0)             // if the file has opened for reading
   { 
      Comment(nameResult+". Opened with attempt #"+ attempt); // opening report
      read_n_draw(handle_read);  // read the result and draw a chart
      FileClose(handle_read);    // close the file
      FileDelete(nameResult);    // delete the result file
      attempt=0;                 // zeroize the amount of attempts to read
   }
   else                          // if we cannot open the result file
   {
      Comment( "Failed reading "+nameResult+
               ". Amount of attempts: "+attempt+
               ". Error #"+GetLastError()); //Report about failed reading
   }
   old_bars = Bars;              // remember how many bars are known
   return(0);
}
//+------------------------------------------------------------------+
void read_n_draw(int handle_read)
{
   int i=0;
   while ( !FileIsEnding(handle_read)) 
   {
      ExtMap[i] = FileReadNumber(handle_read);
      i++;     
   }
   ExtMap[i-1] = EMPTY_VALUE;
}

 
void write_data()
{
  int handle;
  handle = FileOpen(nameData, FILE_CSV|FILE_WRITE,';');
 
  FileWrite(handle, ServerAddress(), Symbol(), Period());                  // header
  FileWrite(handle, "DATE","TIME","HIGH","LOW","CLOSE","OPEN","VOLUME");   // header
  int i;
  for (i=length-1; i>=0; i--)
  {
    FileWrite(handle, TimeToStr(Time[i], TIME_DATE), TimeToStr(Time[i], TIME_SECONDS),
                      High[i], Low[i], Close[i], Open[i], Volume[i]);
  }
  
}

 And the data of results in txt file has the form:

 1.11535,1.11536,1.11536,1.11536,1.11534,1.11531,1.11527,1.11523,1.11520,1.11519,1.11518,1.11518,1.11519,1.11520,1.11523,NaN,NaN,NaN,NaN

 which may be the error?

 

Thank you very much!! 

avatar
29
castann86 2015.05.12 20:55 #
 
castann86:

Hi all,

i was studying the article of interaction between matlab and metatrader via csv file.

I create the code in matlab, and matlab create a new txt file with results (In this case, moving average of period 5).

When i try to plot this moving average in Metatrader, i have a error, because it no appear nothing.

I use the following code for this step:

 

 And the data of results in txt file has the form:

 1.11535,1.11536,1.11536,1.11536,1.11534,1.11531,1.11527,1.11523,1.11520,1.11519,1.11518,1.11518,1.11519,1.11520,1.11523,NaN,NaN,NaN,NaN

 which may be the error?

 

Thank you very much!! 

Hi, 

someone can help me??

Thank you very much!!! 

avatar
29
castann86 2015.05.12 21:43 #
 
castann86:

Hi, 

someone can help me??

Thank you very much!!! 

The delimited sign is "," but for some reason, the txt file is deleted before read. Can someone say me if the code is well?

 

Thank you very much 

avatar
101
cosmicbeing 2015.05.13 05:06 #
 
castann86:

Hi all,

i was studying the article of interaction between matlab and metatrader via csv file.

I create the code in matlab, and matlab create a new txt file with results (In this case, moving average of period 5).

When i try to plot this moving average in Metatrader, i have a error, because it no appear nothing.

I use the following code for this step:

 

 And the data of results in txt file has the form:

 1.11535,1.11536,1.11536,1.11536,1.11534,1.11531,1.11527,1.11523,1.11520,1.11519,1.11518,1.11518,1.11519,1.11520,1.11523,NaN,NaN,NaN,NaN

 which may be the error?

 

Thank you very much!! 

Hi castann86,

I took a quick look at the code for you (didn't try it yet)...

And there seems to be a big difference in the MatLab Text Results file and the Write To File section that looks like it's writing regular Market Data to file...

The MatLab results...looks like the MA price data for x-bars (?) and some strange NaN's at the end...and has 19 fields.

The Market Data has 7 fields...

They don't match...and I assume the file structures should match in order for MT4 to read the MatLab values properly...but  I am not sure what you are trying to do...

Also...regarding the files being deleted before you can see it...

There are 2 places in your code that use "FileDelete"...

You can blank those FileDelete code lines out temporarily (use //)...so the files don't get deleted and you can see the results...

You can always un-blank the lines later when you are done testing.

Hope this helps you get a few steps closer to some good solutions...

Robert







avatar
126
Demos 2015.05.13 13:43 #
 

Hello,

you are declaring

double ExtMap[]; 

 and then you are going straight to 

ArrayInitialize( ExtMap, EMPTY_VALUE); 

 But ExtMap has no size; either write e.g.

double ExtMap[100];

 in the first place, or use ArrayResize before ArrayInitialize

 

 

 

avatar
12983
WHRoeder 2015.05.13 14:02 #
 
  1. Add print statements before and inside your if's and find out what is happening.
  2. Check your return codes (FileOpen and FileDelete) What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
Back to topics list  

To add comments, please log in or register