The function is intended for writing of data into a CSV file, delimiter being inserted automatically unless it is equal to 0. After writing into the file, the line end character "\r\n" will be added.

uint  FileWrite(
   int  file_handle,   // File handle
   ...                 // List of recorded parameters



[in]  File descriptor returned by FileOpen().


[in] The list of parameters separated by commas, to write to the file. The number of written parameters can be up to 63.

Return Value

Number of bytes written.


Numbers will be converted into a text at output (see the Print() function). Data of the double type are output with the accuracy of 16 digits after the decimal point, and the data can be displayed either in traditional or in scientific format - depending on which format will be the most compact. The data of the float type are shown with 5 digits after the decimal point. To output real numbers with different precision or in a clearly specified format, use DoubleToString().

Numbers of the bool type are displayed as "true" or "false" strings. Numbers of the datetime type are displayed as "YYYY.MM.DD HH:MI:SS".


//|                                               Demo_FileWrite.mq5 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                     |
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      ""
#property version   "1.00"
//--- show the window of input parameters when launching the script
#property script_show_inputs
//--- parameters for receiving data from the terminal
input string             InpSymbolName="EURUSD";           // currency pair
input ENUM_TIMEFRAMES    InpSymbolPeriod=PERIOD_H1;        // time frame
input int                InpFastEMAPeriod=12;              // fast EMA period
input int                InpSlowEMAPeriod=26;              // slow EMA period
input int                InpSignalPeriod=9;                // difference averaging period
input ENUM_APPLIED_PRICE InpAppliedPrice=PRICE_CLOSE;      // price type
input datetime           InpDateStart=D'2012.01.01 00:00'; // data copying start date
//--- parameters for writing data to file
input string             InpFileName="MACD.csv";  // file name
input string             InpDirectoryName="Data"// directory name
//| Script program start function                                    |
void OnStart()
   datetime date_finish; // data copying end date
   bool     sign_buff[]; // signal array (true - buy, false - sell)
   datetime time_buff[]; // array of signals' arrival time
   int      sign_size=0; // signal array size
   double   macd_buff[]; // array of indicator values
   datetime date_buff[]; // array of indicator dates
   int      macd_size=0; // size of indicator arrays
//--- end time is the current time
//--- receive MACD indicator handle
   int macd_handle=iMACD(InpSymbolName,InpSymbolPeriod,InpFastEMAPeriod,InpSlowEMAPeriod,InpSignalPeriod,InpAppliedPrice);
      //--- failed to receive indicator handle
      PrintFormat("Error when receiving indicator handle. Error code = %d",GetLastError());
//--- being in the loop until the indicator calculates all its values
      Sleep(10); // pause to allow the indicator to calculate all its values
//--- copy the indicator values for a certain period of time
      PrintFormat("Failed to copy indicator values. Error code = %d",GetLastError());
//--- copy the appropriate time for the indicator values
      PrintFormat("Failed to copy time values. Error code = %d",GetLastError());
//--- free the memory occupied by the indicator
//--- receive the buffer size
//--- analyze the data and save the indicator signals to the arrays
   for(int i=1;i<macd_size;i++)
      //--- buy signal
      if(macd_buff[i-1]<0 && macd_buff[i]>=0)
      //--- sell signal
      if(macd_buff[i-1]>0 && macd_buff[i]<=0)
//--- open the file for writing the indicator values (if the file is absent, it will be created automatically)
   int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_WRITE|FILE_CSV);
      PrintFormat("%s file is available for writing",InpFileName);
      PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH));
      //--- first, write the number of signals
      //--- write the time and values of signals to the file
      for(int i=0;i<sign_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());

See also

Comment, Print, StringFormat