Question about FileRead

 

Hi Everyone,


I want to read .csv file with precalculated data and load this data to indicator.

File data has such stucture:

Date time, data1, data2, data3, data4

2011.10.20 00:22,-0.04558,-0.76599,1.65205,2.83105

2011.10.20 00:26,-0.04582,-0.76509,1.65226,2.83158

For indicator line I need ofcource date and time, and one of data values.

How could I read this file ?

I find many FileRead commands, but don't know which are suitable best ?

I could change structure of data file, if it make sence.


Edward

 

Have a read of this thread: https://www.mql5.com/en/forum/136176

It might make your life easier to convert the date and time to a datetime before you read it via mql4.

 

Thanks, RaptorUk.

My first job with filewrite was trying to make data copier: reading one file and writing to another file the same data. However script work incorectly.

//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
 
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   
   //----   
   
   int source = FileOpen("data.csv",FILE_CSV|FILE_READ,',');
   int target = FileOpen("data_copy.csv",FILE_CSV|FILE_WRITE,',');   

   
   if (source<0) return;

   datetime date_digit;
   double data1,data2,data3,data4;
  


   while( !FileIsEnding(source) ) // While the file pointer..
   { // ..is not at the end of the file
   
   date_digit      = FileReadNumber(source);
   data1 = FileReadDouble(source);
   data2 = FileReadDouble(source);
   data3 = FileReadDouble(source);
   data4 = FileReadDouble(source);      

   FileWrite(target, date_digit, 
                     NormalizeDouble(data1,5), NormalizeDouble(data2,5) ,
                     NormalizeDouble(data3,5), NormalizeDouble(data4,5) );       
   
   }

   FileClose(source);         
   FileClose(target);   
   
//----
   return(0);
  }
//+------------------------------------------------------------------+


Here is part of source file:

1800,-6.40108,-2.29823,190.82977,100.31943
2700,-6.462,-2.30861,190.96053,100.33002
3600,-6.5229,-2.31898,191.09134,100.34061
4500,-6.5838,-2.32934,191.22218,100.35121
5400,-6.64469,-2.33969,191.35306,100.36179
6300,-6.70557,-2.35003,191.48398,100.37238

7200,-6.76643,-2.36037,191.61494,100.38297


I wanted just to copy file element by element, so rezult file should be the same. However is very different from source. Could you tell where problem is ? Maybe elements should be strings or numbers ?


Thanks,

Edward



Here is part of rezult file:

1800,0,0,0,0
-6,0,0,0,0
-2,0,0,0,0
190,0,0,0,0
100,0,0,0,0
2700,0,0,0,0
-6,0,0,0,0
-2,0,0,0,0
190,0,0,0,0
100,0,0,0,0
3600,0,0,0,0
-6,0,0,0,0
-2,0,0,0,0
191,0,0,0,0
100,0,0,0,0
4500,0,0,0,0
-6,0,0,0,0
-2,0,0,0,0
191,0,0,0,0
100,0,0,0,0
5400,0,0,0,0
-6,0,0,0,0
-2,0,0,0,0
191,0,0,0,0
100,0,0,0,0
6300,0,0,0,0
-6,0,0,0,0
-2,0,0,0,0
191,0,0,0,0
100,0,0,0,0
7200,0,0,0,0
-6,0,0,0,0
-2,0,0,0,0
191,0,0,0,0
100,0,0,0,0

 
Try using FileReadNumber instead of FileReadDouble
 

Yes, this helped :)

Thank you !

 
edas:

Yes, this helped :)

Thank you !

Cool :-) it's not something I have ever used . .
Reason: