getting error "array out of range in 'file.mq5' (113,26)"

 

I'm currently working on getting calendar events and coded the following , but it gives me an error of "array out of range in 'file.mq5' (113,26)" in journal when I backtest.


void OnTick(void) {
//--- country code for EU (ISO 3166-1 Alpha-2)
   string US_code="US";
//--- get EU events
   MqlCalendarEvent events[];
   int events_count=CalendarEventByCountry(US_code,events);
//--- display EU events in the Journal
   if(events_count>0)
     {
      PrintFormat("US events: %d",events_count);
      //--- reduce the event list, 5 events are sufficient for analysis
      ArrayResize(events,5);
      ArrayPrint(events);
     }
     
     
     
//--- see that the "ECB Interest Rate Decision" event has event_id=999010007
113   ulong event_id=events[1].id;        // the event's ID may change in the Calendar, so be sure to verify
   string event_name=events[1].name;   // name of a Calendar event
   PrintFormat("Get values for event_name=%s event_id=%d",event_name,event_id);
//--- get all values of the "ECB Interest Rate Decision" event
   MqlCalendarValue values[];
//--- set the boundaries of the interval we take the events from
   datetime date_from=0;           // take all events from the beginning of the available history
   datetime date_to=D'01.01.2016'; // take events not older than 2016
   if(CalendarValueHistoryByEvent(event_id,values,date_from,date_to))
     {
      PrintFormat("Received values for %s: %d",
                  event_name,ArraySize(values));
      //--- reduce the value list, 10 events are sufficient for analysis
      ArrayResize(values,5);
      ArrayPrint(values);
     }
   else
     {
      PrintFormat("Error! Failed to get values for event_id=%d",event_id);
      PrintFormat("Error code: %d",GetLastError());
     }
}


could someone point out what i did wrong?

 

I found out that the value of events_count is -1.

Does CalendarEventByCountry return -1 for anything on backtest?

 
I needed to put the code in OnStart() and had to run it on MetaEditor, so then I could see all events on [Experts] in Toolbox.
 
leaN5525: I'm currently working on getting calendar events and coded the following , but it gives me an error of "array out of range in 'file.mq5' (113,26)" in journal when I backtest. could someone point out what i did wrong?

Arrays start at index [0] and not index [1]. Also, please note that the OnTick event handler runs on every single tick, and since the Economic Calendar does not update all the often, it will be inefficient to poll it on every single tick. If you really want to check it very often, then once per minute is more than sufficiently aggressive poling.

 
@leaN5525 are you able to solve this problem? I have the same problem
leaN5525
leaN5525
  • 2021.08.07
  • www.mql5.com
Trader's profile
Reason: