Download MetaTrader 5
MQL5 ReferenceFile FunctionsFileReadDatetime 


Reads from the file of CSV type a string of one of the formats: "YYYY.MM.DD HH:MI:SS", "YYYY.MM.DD" or "HH:MI:SS" - and converts it into a value of datetime type.

datetime  FileReadDatetime(
   int  file_handle    // File handle



[in]  File descriptor returned by FileOpen().

Return Value

The value of datetime type.

Example (the file obtained after executing the example for FileWrite function is used here)

//|                                        Demo_FileReadDateTime.mq5 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                     |
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      ""
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
//---- plot Label1
#property indicator_label1  "UpSignal"
#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  4
//---- plot Label2
#property indicator_label2  "DownSignal"
#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrRed
#property indicator_style2  STYLE_SOLID
#property indicator_width2  4
//--- parameters for data reading
input string InpFileName="MACD.csv";  // file name
input string InpDirectoryName="Data"// directory name
//--- global variables
int      ind=0;       // index
double   upbuff[];    // indicator buffers of up arrows
double   downbuff[];  // indicator buffer of down arrows
bool     sign_buff[]; // signal array (true - buy, false - sell)
datetime time_buff[]; // array of signals' arrival time
int      size=0;      // size of signal arrays
//| Custom indicator initialization function                         |
int OnInit()
//--- open the file
   int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_CSV);
      PrintFormat("%s file is open for reading",InpFileName);
      //--- first, read the number of signals
      //--- allocate memory for the arrays
      //--- read data from the file
      for(int i=0;i<size;i++)
         //--- signal time
         //--- signal value
      //--- close the file
      PrintFormat("Failed to open %s file, Error code = %d",InpFileName,GetLastError());
//--- binding the arrays
//--- set the symbol code for drawing in PLOT_ARROW
//---- set the indicator values that will not be seen on the chart
//| Custom indicator iteration function                              |
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
//--- the loop for the bars that have not been handled yet
   for(int i=prev_calculated;i<rates_total;i++)
      //--- 0 by default
      //--- check if any data is still present
         for(int j=ind;j<size;j++)
            //--- if dates coincide, use the value from the file
               //--- draw the arrow according to the signal
               //--- increase the counter
//--- return value of prev_calculated for next call

See also

Type datetime, StringToTime, TimeToString, FileWrite

Updated: 2015.12.03