The Expert Advisor that saves Live Data for Backtesting

In this post we share with you an Expert Advisor that helps us with our Backtesting. Specifically, we want to haveclean and live data before we run our Backtesting. This Expert Advisor will automatically save data at any given time in our MT4 platform, giving us reliable information to test our strategies.

Feel free to leave us some feedback if you have any questions on the code or if you just like our tutorials!

Source code below

Feel free to share!

input string InpDirectoryName = "Data"; // Folder name as an input

int file_handle; // Variable for saving file handle
string InpFileName = _Symbol + ".txt"; // File name
string delimiter="\t"; // User-defined delimiter

int OnInit()
file_handle = FileOpen(InpDirectoryName + "//" + InpFileName, FILE_WRITE|FILE_TXT|FILE_ANSI); // Open File
if(file_handle == INVALID_HANDLE) { // Check if file opened
Print("Failed to open", InpFileName, "file, Error code = %d", GetLastError()); // Print error if not opened
ExpertRemove(); // Remove expert

void OnTick()
string s = StringConcatenate(TimeToStr(TimeGMT(),TIME_DATE|TIME_SECONDS), delimiter, // Save Datetime, Bid, Volume
DoubleToStr(Bid, Digits()), delimiter,
FileWriteString(file_handle, s + "\r\n"); // Write in file

void OnDeinit(const int reason)
FileClose(file_handle); // Close File


What are you waiting for?


Muhammad Awais

One Response to “The Expert Advisor that saves Live Data for Backtesting”

March 02, 2017 at 1:24 pm, marion “haruRot” pastoral said:

Hi Kirill, could you explain to us what is a tick chart? How does it differ from the timeframes already in MT4 and can we use the tick chart in manual trading in MT4?


Leave a Reply

Your email address will not be published. Required fields are marked *

What are you waiting for?


as seen on: