Question about news filters in backtesting

 

I have been reading that news filters based on the MQL5 calendar do not work in the backtesting mode, and that a likely reason for this is that historical news data are rarely available and/or reliable.

Yet, when I look over the data available in the calendar, I see news back a few years. So could one backtest the effects of news for the times for which the MQL5 calendar has news? or is that capability disabled altogether in the backtesting part?

Economic Calendar – forex calendar with real-time forex news and reports, schedule of forthcoming world economy events
Economic Calendar – forex calendar with real-time forex news and reports, schedule of forthcoming world economy events
  • www.mql5.com
Economic Calendar – forex calendar with real-time forex news and reports, schedule of forthcoming world economy events. Economic calendar includes most important economic indicators and events from ministries and agencies of different countries. The Calendar is useful for traders in the forex market, stock exchanges and other financial markets.
 

Are you sure? Did you search before posting?

It seems that it does work in the Strategy Tester ...

MQL5 Economic Calendar / CalendarValueHistory in backtest - How to access the built-in economic calendar during backtest
MQL5 Economic Calendar / CalendarValueHistory in backtest - How to access the built-in economic calendar during backtest
  • 2019.11.15
  • www.mql5.com
Hi all, is there any way to access the built-in economic calendar during backtest. Honestly, it's far from any code that i'm proud of, i did very little in the field of news trading over the past months and in retrospect i would do it in a better organized way
 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
MQL5 forum: Expert Advisors and Automated Trading
MQL5 forum: Expert Advisors and Automated Trading
  • www.mql5.com
How to create an Expert Advisor (a trading robot) for Forex trading
 

Thanks Fernando,

If you read that post, what they claim is that one has to do an alternative code/trick for the use of the calendar data in the backtestings.

Basically, download the news data, save it in a file, and then load it in your EA for backstesting.

It seems weird to me why for backstesting, MQL5 would not allow an EA to read news data directly from the calendar, when the data is already available.

I have found in the Freelancer version of this webpage, that given this limitation many people have opted to create double versions of their EAs. For instance, i noted several requests to create double EAs, one working for the backtesting and other for the real ticks. Others have opted to have both functions optional in the same EA.

My thought is that may be what is happening is that the news calendar could be used in the backtesting directly into an EA, as it would do in real time, but only during the periods for which they have data. But not certain.

I would like to code a news filter, and wanted to know if one really has to code this functionality independently for the historical and real time versions of the EA.

 
Camilo Mora #:

Thanks Fernando,

If you read that post, what they claim is that one has to do an alternative code/trick for the use of the calendar data in the backtestings.

Basically, download the news data, save it in a file, and then load it in your EA for backstesting.

It seems weird to me why for backstesting, MQL5 would not allow an EA to read news data directly from the calendar, when the data is already available.

I have found in the Freelancer version of this webpage, that given this limitation many people have opted to create double versions of their EAs. For instance, i noted several requests to create double EAs, one working for the backtesting and other for the real ticks. Others have opted to have both functions optional in the same EA.

My thought is that may be what is happening is that the news calendar could be used in the backtesting directly into an EA, as it would do in real time, but only during the periods for which they have data. But not certain.

I would like to code a news filter, and wanted to know if one really has to code this functionality independently for the historical and real time versions of the EA.

Ok, but in that case on the same topic there is a reference to fxsaber's library for handling the Calender data in such a was as to work with both Live and in the Tester.

I don't know of it serves your needs but did you have a look at it?

Code Base

Calendar

ffxsaberxsaber , 2020.12.11 23:59

Calendar - fundamental analysis in history and in real time.
 

I looks like for backtesting, MQL5 blocks access to the news calendar even if the calendar has news available. Here is the test I did, and may be a more expert person can comment, if this is the right test, and conclusion.

Basically, I coded an EA that displays the list of news for the EU from the current candle time to the next 24 hours. Every tick the list of news is re-printed.

 If I run this code in real time, every tick I get the list of news in the next 24 hours. But if I run this same code in backtesting, say starting in January 2023 (when I know for certain the news calendar has news for the EU), the code returns an empty array.

This suggest that indeed one may have to code any news filter separately for backtesting and real time.

datetime From;   // date to start looking for
datetime Days=1; // number of days to look for
datetime To;     //holder variable of the end of the time period to check

void OnTick()
   { 
   From        =iTime(_Symbol,PERIOD_D1,0);
   To = From + PeriodSeconds(PERIOD_D1)*Days;
   
//--- country code for EU (ISO 3166-1 Alpha-2) 
   string EU_code="EU"; 
//--- get all EU event values 
   MqlCalendarValue values[]; 


//--- request EU event history since 2018 year 
   if(CalendarValueHistory(values,From,To,EU_code)) 
     { 
      PrintFormat("Received event values for country_code=%s: %d", 
                  EU_code,ArraySize(values)); 
      //--- decrease the size of the array for outputting to the Journal 
      ArrayResize(values,10); 
//--- display event values in the Journal 
      ArrayPrint(values);       
     } 
   else 
     { 
      PrintFormat("Error! Failed to receive events for country_code=%s",EU_code); 
      PrintFormat("Error code: %d",GetLastError()); 
     } 
//--- 
  } 

Reason: