MQL4 Reference File Functions FileReadDouble


Reads a double-precision floating point number (double) from the current position of the binary file.

double  FileReadDouble(
   int  file_handle,       // File handle
   int  size=DOUBLE_VALUE  // Size



[in]  File descriptor returned by FileOpen().


Number of bytes (up to 8 inclusive), that should be read. The corresponding constants are provided: DOUBLE_VALUE = 8, FLOAT_VALUE = 4, so the function can read the whole value of double or float type.

Return Value

The value of double type.


For more details about the error, call GetLastError().

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

//|                                          Demo_FileReadDouble.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                     |
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      ""
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 1
//---- plot Label1
#property indicator_label1  "MA"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrGreen
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
#property indicator_separate_window
//--- data reading parameters
input string InpFileName="MA.bin";    // File name
input string InpDirectoryName="Data"// Folder name
//--- global variables
int      ind=0;
int      size=0;
double   ma_buff[];
datetime time_buff[];
//--- indicator buffer
double   buff[];
//| Custom indicator initialization function                         |
int OnInit()
//--- open the file
   int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_BIN);
      PrintFormat("%s file is available for reading",InpFileName);
      PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH));
      //--- first, read the amount of data in the file
      //--- allocate memory for the arrays
      //--- read data from the file
      for(int i=0;i<size;i++)
      //--- close the file
      PrintFormat("Data is written, %s file is closed",InpFileName);
      PrintFormat("Failed to open %s file, Error code = %d",InpFileName,GetLastError());
//--- bind the array to the indicator buffer with index 0
//---- set the indicator values that will not be visible on the chart
//--- set indexing as timeseries
//| 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
      for(int j=0;j<size;j++)
         //--- if the dates coincide, the value from the file is used
//--- return value of prev_calculated for next call

See also

Real types (double, float), StringToDouble(), DoubleToString(), FileWriteDouble()