The Expert Advisor that saves Live Data for Backtesting

Forex advisor 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?

CHOOSE YOUR COURSE NOW AND START LEARNING FOREX TODAY!

Team ForexBoat
Team ForexBoat

Our goal is to share this passion with others and guide newbies to avoid costly mistakes. Today we want to share all our knowledge and insights, so you can take your trading skills to the next level.

What are you waiting for?

CHOOSE YOUR COURSE NOW AND START LEARNING FOREX TODAY!

as seen on: