Download MetaTrader 5

Watch how to download trading robots for free

Interesting script?
So post a link to it -
let others appraise it

You liked the script? Try it in the MetaTrader 5 terminal


Demo_FileWriteDouble - script for MetaTrader 5

| English Русский 中文 Español Deutsch 日本語 Português

votes: 30
2013.04.10 13:09
2016.11.22 07:32

The script gets the values of the Moving Average indicator and writes them in the binary file in "Data" subdirectory of the local terminal folder. In script input parameters can be specified parameters for the MA indicator calculation, the name of the currency pair and timeframe, as well as date from which we will calculate the indicator values.

In addition to the MA indicator values ​​the corresponding values ​​of time preliminary led to a double type are also written to the file.


//--- show the window of input parameters when launching the script
#property script_show_inputs
//--- parameters for receiving data from the terminal
input string             InpSymbolName="EURJPY";           // currency pair
input ENUM_TIMEFRAMES    InpSymbolPeriod=PERIOD_M15;       // timeframe
input int                InpMAPeriod=10;                  // smoothing period
input int                InpMAShift=0;                    // indicator shift
input ENUM_MA_METHOD     InpMAMethod=MODE_SMA;             // smoothing type
input ENUM_APPLIED_PRICE InpAppliedPrice=PRICE_CLOSE;      // price type
input datetime           InpDateStart=D'2013.01.01 00:00'; // data copying start date
//--- parameters for writing data to the file
input string             InpFileName="MA.csv";    // file name
input string             InpDirectoryName="Data"; // directory name
//| Script program start function                                    |
void OnStart()
   datetime date_finish=TimeCurrent();
   double   ma_buff[];
   datetime time_buff[];
   int      size;
//--- get MA indicator handle
   int ma_handle=iMA(InpSymbolName,InpSymbolPeriod,InpMAPeriod,InpMAShift,InpMAMethod,InpAppliedPrice);
      //--- failed to  get the indicator handle
      PrintFormat("Error when receiving indicator handle. Error code = %d",GetLastError());
//--- being in the loop until the indicator calculates all its values
      Sleep(20); // a pause to allow the indicator to calculate all its values
   PrintFormat("Indicator values starting from %s will be written to the file",TimeToString(InpDateStart));
//--- copy the indicator values
      PrintFormat("Failed to copy the indicator values. Error code = %d",GetLastError());
//--- copy the time of the appropriate bars' arrival
      PrintFormat("Failed to copy time values. Error code = %d",GetLastError());
//--- receive the buffer size
//--- free the memory occupied by the indicator
//--- 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_BIN);
      PrintFormat("%s file is available for writing",InpFileName);
      PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH));
      //--- first, write the size of data sample
      //--- write the indicator time and value to 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());

Translated from Russian by MetaQuotes Software Corp.
Original code:

Demo_FileReadDatetime Demo_FileReadDatetime

The indicator demonstrates the example of using the FileReadDatetime() function

Demo_FileWrite Demo_FileWrite

The script demonstrates the example of using the FileWrite() function

Demo_FileReadDouble Demo_FileReadDouble

The indicator demonstrates the example of using the FileReadDouble() function

Demo_FileSize Demo_FileSize

The script demonstrates the example of using the FileSize() function