CalendarEventByCountry return -1

 

Hello Guys;


I am coding a strategy that no permission to trade when economic calendar has important news.

I write a function that correct work in script (onstart) but dont work in expert in ontick and ontimer event. this is my function:

string eco_calender_new(string country_code)
  {
   string _code=country_code;
   MqlCalendarEvent events[];
   int events_count=CalendarEventByCountry(_code,events);
   ArrayResize(events,2);
   Print("events_count:  ",events_count);
   return events[0].name;
  }

this function work in script but return -1 when use in expert. this is my full code:

//+------------------------------------------------------------------+
//|                                                     eco_test.mq5 |
//|                                                       emadkoosha |
//|                                          https://www.Emademy.com |
//+------------------------------------------------------------------+
#property copyright "emadkoosha"
#property link      "https://www.Emademy.com"
#property version   "1.00"
struct AdjustedCalendarValue
  {
   ulong                               id;                    // value ID
   ulong                               event_id;              // event ID
   datetime                            time;                  // event date and time
   datetime                            period;                // event reporting period
   int                                 revision;              // revision of the published indicator relative to the reporting period
   double                              actual_value;          // actual value
   double                              prev_value;            // previous value
   double                              revised_prev_value;    // revised previous value
   double                              forecast_value;        // forecast value
   ENUM_CALENDAR_EVENT_IMPACT          impact_type;           // potential impact on the currency rate
  };
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
 
   EventSetTimer(5*24*60*60);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {

  }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
void OnTimer()
  {
//Print("test");

//string usd= eco_calnder("US");
//string eur= eco_calnder("EU");
//Print("eur: ",eur," ","usd: ",usd);
   string usd= eco_calender_new("US");
   Print("usd: ",usd);
  }
//+------------------------------------------------------------------+
string eco_calender_new(string country_code)
  {
   string _code=country_code;
   MqlCalendarEvent events[];
   int events_count=CalendarEventByCountry(_code,events);
   ArrayResize(events,2);
   Print("events_count:  ",events_count);
   return events[0].name;
  }
//+------------------------------------------------------------------+


this is output:


   

 
You miss to evaluate _LastError. This would probably tell you why.
 
emad koosha:

I write a function that correct work in script (onstart) but dont work in expert in ontick and ontimer event.

The original calendar does not work in Tester. You need to use other solutions.

Reason: