If you're looking to enhance your trading strategy in MetaTrader 5, the Demo_FileWriteArray Expert Advisor (EA) is a game-changer. This EA captures the Bid and Ask prices with every tick and efficiently stores them in a structured array. Let’s dive into how it works and how you can leverage it for your trading.
How It Works
This EA operates by gathering Bid and Ask prices every tick and saving them into a "prices" structure array. Every twentieth tick, it writes this structured data into a binary file located in a subdirectory of your local terminal folder. To access the terminal's local folder, you can use the TerminalInfoString() function.
PrintFormat("The path to the terminal local folder: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH));
When you remove the EA from your chart, it ensures that any unsaved data is written to the file before terminating its operation.
Code Breakdown
Input Parameters:
//--- input parameters input string InpFileName="data.bin"; input string InpDirectoryName="SomeFolder";
Structure for Storing Price Data:
//+------------------------------------------------------------------+ //| Structure for storing price data | //+------------------------------------------------------------------+ struct prices { datetime date; // data double bid; // Bid price double ask; // Ask price };
Global Variables:
//--- global variables int count=0; int size=20; string path=InpDirectoryName+"//"+InpFileName; prices arr[];
Initialization and Deinitialization
The EA initializes by allocating memory for the array:
int OnInit() { //--- allocate memory for the array ArrayResize(arr,size); //--- return(INIT_SUCCEEDED); }
When it’s time to remove the EA, it will write any remaining data:
void OnDeinit(const int reason) { //--- write the remaining count strings if count<n WriteData(count); }
Tick Function
With every tick, the EA saves the current data:
void OnTick() { //--- save data to array arr[count].date=TimeCurrent(); arr[count].bid=SymbolInfoDouble(Symbol(),SYMBOL_BID); arr[count].ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK); //--- show current data Print("Date = ",arr[count].date," Bid = ",arr[count].bid," Ask = ",arr[count].ask); //--- increase the counter count++; //--- if the array is filled, write data to the file and zero it out if(count==size) { WriteData(size); count=0; } }
Writing Data to File
Finally, the EA writes the collected data to a file:
void WriteData(const int n) { //--- open the file ResetLastError(); int handle=FileOpen(path,FILE_READ|FILE_WRITE|FILE_BIN); if(handle!=INVALID_HANDLE) { //--- write array data to the end of the file FileSeek(handle,0,SEEK_END); FileWriteArray(handle,arr,0,n); //--- close the file FileClose(handle); } else Print("Failed to open the file, error ",GetLastError()); }
Conclusion
The Demo_FileWriteArray EA is a powerful tool for traders looking to collect and manage their trading data effectively. By automating data capture and storage, you can focus on what really matters—making informed trading decisions. Give it a whirl and see how it can enhance your trading experience!
Comments 0