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
}
return INIT_SUCCEEDED;
}

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

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

 

What are you waiting for?

START LEARNING FOREX TODAY!

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?

Reply

Leave a Reply

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

What are you waiting for?

START LEARNING FOREX TODAY!

as seen on: