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

2013.04.09 13:14

Demo_FileWriteArray - expert for MetaTrader 5

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

votes: 21


The Expert Advisor gets values of Bid and Ask prices on every tick and remembers them into the "prices" structure array. On every twentieth tick the Expert Advisor writes structure objects data into the file using the FileWriteArray() function. Data will be recorded in the binary file in subdirectory of the local terminal folder. The terminal local folder location can be obtained calling the TerminalInfoString() function.

PrintFormat("The path to the terminal local folder: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH)); 
When deleting the Expert Advisor from the chart, there is writing of not written data into the file after which the Expert Advisor terminates its work.


//--- input parameters
input string InpFileName="data.bin";
input string InpDirectoryName="SomeFolder";
//| Structure for storing price data                                 |
struct prices
   datetime          date; // data
   double            bid;  // Bid price
   double            ask;  // Ask price
//--- global variables
int    count=0;
int    size=20;
string path=InpDirectoryName+"//"+InpFileName;
prices arr[];
//| Expert initialization function                                   |
int OnInit()
//--- allocate memory for the array
//| Expert deinitialization function                                 |
void OnDeinit(const int reason)
//--- write the remaining count strings if count<n
//| Expert tick function                                             |
void OnTick()
//--- save data to array
//--- show current data
   Print("Date = ",arr[count].date," Bid = ",arr[count].bid," Ask = ",arr[count].ask);
//--- increase the counter
//--- if the array is filled, write data to the file and zero it out
//| Write n elements of the array to the file                        |
void WriteData(const int n)
//--- open the file
   int handle=FileOpen(path,FILE_READ|FILE_WRITE|FILE_BIN);
      //--- write array data to the end of the file
      //--- close the file
      Print("Failed to open the file, error ",GetLastError());

Translated from Russian by MetaQuotes Software Corp.
Original code:

Accelerator Oscillator (AC) Accelerator Oscillator (AC)

The Acceleration/Deceleration Indicator (AC) measures acceleration and deceleration of the current driving force.

Average Directional Movement Index (ADX) Average Directional Movement Index (ADX)

The Average Directional Movement Index Indicator (ADX) helps to determine if there is a price trend.

TP-SL_Removing TP-SL_Removing

Immediately deletes the SL/TP of all positions.

Closed_TOTAL Closed_TOTAL

Total closure of all positions (winners/losers). You can also select which are of a single instrument.