Economic calendar timing out (ERR_CALENDAR_TIMEOUT)

 

I would like to add a news filter to my EAs. My plan is to build a service that will pull data from the brokers economic calendar and populate a local database. EAs would then use this database to avoid trading during certain news events; both for live testing and back testing.

Unfortunately, I really haven't gotten far. Can anyone see an obvious mistake I am making?

  • I can pull a list of MqlCalendarCountry instances using CalendarCountries()
  • I can pull a list of MqlCalendarEvent instances using CalendarEventByCountry() or CalendarEventByCurrency()

I cannot get any MqlCalendarValue data. I have tried both CalendarHistoryByEvent() and CalendarValueHistory(). Here is a minimal code example:

void OnStart()
{
    MqlCalendarValue values[];
    datetime from = D'2024.09.01';
    datetime to = 0;
    PrintFormat("dates: %s -> %s", TimeToString(from), TimeToString(to));
    if (CalendarValueHistory(values, from, to) != -1)
    {
        PrintFormat("#values=%d", ArraySize(values));
        ArrayPrint(values);
    }
    else
    {
        PrintFormat("Error: CalendarValueHistory() failed, error code %d.", GetLastError());
    }
}

Which produces the following output:

2024.10.02 20:25:28.245 cal (XAUUSD.a,M5) dates: 2024.09.01 00:00 -> 1970.01.01 00:00

2024.10.02 20:26:18.339 cal (XAUUSD.a,M5) Error: CalendarValueHistory() failed, error code 5401.

Error code 5401 corresponds to  ERR_CALENDAR_TIMEOUT. It would seem that I can fetch information about countries and events, but not the individual news entries (MqlCalendarValue).

I've tried several things without success:

  • Wrapping this code in retry logic
  • Enabling Options -> Expert Advisors -> Allow DLL imports
  • Enabling Options -> Expert Advisors -> Allow WebRequest
  • Adding "http://mql5.com" to the allow list
  • Enabling Options -> Community -> Calendar
Is there a reason why I can't access the economic calendar?
Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2024.10.02
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 
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
 
Bill M:

I would like to add a news filter to my EAs. My plan is to build a service that will pull data from the brokers economic calendar and populate a local database. EAs would then use this database to avoid trading during certain news events; both for live testing and back testing.

Unfortunately, I really haven't gotten far. Can anyone see an obvious mistake I am making?

  • I can pull a list of MqlCalendarCountry instances using CalendarCountries()
  • I can pull a list of MqlCalendarEvent instances using CalendarEventByCountry() or CalendarEventByCurrency()

I cannot get any MqlCalendarValue data. I have tried both CalendarHistoryByEvent() and CalendarValueHistory(). Here is a minimal code example:

Which produces the following output:

Error code 5401 corresponds to  ERR_CALENDAR_TIMEOUT. It would seem that I can fetch information about countries and events, but not the individual news entries (MqlCalendarValue).

I've tried several things without success:

  • Wrapping this code in retry logic
  • Enabling Options -> Expert Advisors -> Allow DLL imports
  • Enabling Options -> Expert Advisors -> Allow WebRequest
  • Adding "http://mql5.com" to the allow list
  • Enabling Options -> Community -> Calendar
Is there a reason why I can't access the economic calendar?

I think your issue is not specifying the date range properly.

The following EA gradually downloads all news events , maybe it will help :

it leaps a year back and gets news events with each request 

and you will also need the events and the countries for a proper presentation to the user besides the values.

#property version   "1.00"


int OnInit()
  {
  EventSetMillisecondTimer(333);
  return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
  }

void OnTick()
  {
  }

void OnTimer(){
  EventKillTimer();
  //request all?
    MqlCalendarValue values[];
    MqlCalendarCountry countries[];
    Comment("Getting countries");
    ResetLastError();
    int countriesfound=CalendarCountries(countries);
    int error=GetLastError();
    if(error==0){
      Print("Found "+IntegerToString(countriesfound)+" countries");
      for(int c=0;c<ArraySize(countries);c++){
         Print("C["+c+"] Country("+countries[c].name+") Currency("+countries[c].currency+") symbol("+countries[c].currency_symbol+")");
         }
      Comment("Getting values");
      ResetLastError();
      /* the request takes too long if we ask for everything at once so 
         we will step it in 
         the news use trade server time , allegedly so
         we can hop back 
      */
      long newertimestamp=(long)TimeTradeServer();
      long yearjump=(long)(60*60*24*365);
      long oldertimestamp=newertimestamp-yearjump;
      MqlCalendarValue tempvalues[];
      long limit=(long)(D'2000.01.01 00:00');
      while(newertimestamp>limit){
           datetime request_from=(datetime)oldertimestamp;
           datetime request_to=(datetime)newertimestamp;
           Comment("Total collected ("+IntegerToString(ArraySize(values))+")\nRequesting ("+TimeToString(request_from,TIME_DATE|TIME_MINUTES|TIME_SECONDS)+")->("+TimeToString(request_to,TIME_DATE|TIME_MINUTES|TIME_SECONDS)+")");
           ArrayFree(tempvalues);
           ResetLastError();
           int temptotals=CalendarValueHistory(tempvalues,request_from,request_to,NULL,NULL);
           int temperror=GetLastError();
               //if no error
               if(temperror==0){
               //and we got some 
                 if(temptotals>0){
                   int old_size=ArraySize(values);
                   int new_size=old_size+temptotals;
                   ArrayResize(values,new_size,0);
                   for(int i=0;i<temptotals;i++){
                      values[old_size]=tempvalues[i];
                      old_size++;
                      }
                   }
               //advance to the past
                 newertimestamp=oldertimestamp;
                 oldertimestamp=newertimestamp-yearjump;
                 Sleep(500);
               }else{
               Print("Error #"+IntegerToString(temperror));
               Sleep(1000);
               }
           }
           if(ArraySize(values)>0){
             Comment("Getting event ids");
             //get event types and ids per country
               MqlCalendarEvent event_info[],temp_event_info[];
               for(int c=0;c<ArraySize(countries);c++){
                  ArrayFree(temp_event_info);
                  ResetLastError();
                  int totaleventids=CalendarEventByCountry(countries[c].code,temp_event_info);
                  int eventidserror=GetLastError();
                  while(eventidserror!=0){
                       Sleep(500);
                       ResetLastError();
                       ArrayFree(temp_event_info);
                       totaleventids=CalendarEventByCountry(countries[c].code,temp_event_info);
                       eventidserror=GetLastError();
                       }
                  if(totaleventids>0){
                    int old_size=ArraySize(event_info);
                    int new_size=old_size+totaleventids;
                    ArrayResize(event_info,new_size,0);
                    for(int p=0;p<totaleventids;p++){
                       event_info[old_size]=temp_event_info[p];
                       old_size++;
                       }
                    }
                  }
                  Print("Total event ids ("+IntegerToString(ArraySize(event_info))+")");
             }
      }else{
      Print("Cannot get countries error#"+IntegerToString(error));
      }
  ExpertRemove();
  }
 
Lorentzos Roussos #:

I think your issue is not specifying the date range properly.

The following EA gradually downloads all news events , maybe it will help :

it leaps a year back and gets news events with each request 

and you will also need the events and the countries for a proper presentation to the user besides the values.

Hi @Lorentzos Roussos, thanks for the suggestion. I imagine I will ultimately need to incorporate some of this logic to walk through the calendar a piece at a time. Noted regarding the country and event information. I have actually managed to pull that OK and plan to integrate that information in the end, I just wanted to share a minimal code sample to show my problem. Thank you though!

Unfortunately, I think I am still getting the same error. Here is the Experts output from your EA on my terminal.

2024.10.02 22:07:03.794 News (AUDUSD.a,M30) Error #5401

2024.10.02 22:07:55.010 News (AUDUSD.a,M30) Error #5401

2024.10.02 22:08:46.300 News (AUDUSD.a,M30) Error #5401

2024.10.02 22:09:37.624 News (AUDUSD.a,M30) Error #5401

2024.10.02 22:10:29.011 News (AUDUSD.a,M30) Error #5401

It looks like I am still getting the ERR_CALENDAR_TIMEOUT error from the call to CalendarValueHistory().

I wonder whether this might be something caused by my broker? I can see events in the Calendar tab, so I am getting data from somewhere, I just can't seem to access it programically.

 
Bill M #:

Hi @Lorentzos Roussos, thanks for the suggestion. I imagine I will ultimately need to incorporate some of this logic to walk through the calendar a piece at a time. Noted regarding the country and event information. I have actually managed to pull that OK and plan to integrate that information in the end, I just wanted to share a minimal code sample to show my problem. Thank you though!

Unfortunately, I think I am still getting the same error. Here is the Experts output from your EA on my terminal.

It looks like I am still getting the ERR_CALENDAR_TIMEOUT error from the call to CalendarValueHistory().

I wonder whether this might be something caused by my broker? I can see events in the Calendar tab, so I am getting data from somewhere, I just can't seem to access it programically.

Ok, so if I adjust the EA logic from @Lorentzos Roussos to downloading a day at a time instead of a year at a time I get somewhere. But I can only go back about 10 days. Queries for date ranges older than that produce the 5401 timeout error code.

Is this common that you can only get very recent news?

 
Bill M #:

Ok, so if I adjust the EA logic from @Lorentzos Roussos to downloading a day at a time instead of a year at a time I get somewhere. But I can only go back about 10 days. Queries for date ranges older than that produce the 5401 timeout error code.

Is this common that you can only get very recent news?

I can querry from 2000 onwards but that may be because my terminal has already downloaded it (with this code in the past) and it is cached somewhere

Ill have to try a clean mt5 installation