Hello, I am trying to integrate the Economic Calendar into my MT5 EA !

 
Hello
I am trying to integrate an economic calendar into my MT5 EA so that the robot temporarily blocks trading before important news events. I am using the stub function EconomicNewsGetNext(), but I keep getting compilation errors like:

- Undeclared identifier
- Implicit conversion from unknown to string
- [array required]

I tried the examples from the MQL5 articles ("Trading with the MQL5 Economic Calendar"), but I still get errors.

Here is the relevant EA code snippet I use to handle news:

Economic calendar parameters
input bool UseEconomicCalendar = true;
enter int NewsBlockMinutes = 5; Block trades ± 5 minutes around news
input bool ClosePositionsOnHighNews = true;

News stub functions (currently placeholders)
bool IsNewsWindowActive() { return(false); }
bool EconomicNewsGetNext(datetime & newsTime, string & currency, int & importance, string & text) { return(false); }
void CloseUnfavorablePositionsAfter News(datetime newsTime, double atr) { }

Using OnTick():
if(UseEconomicCalendar & IsNewsWindowActive()) return;

if(UseEconomicCalendar & ClosePositionsOnHighNews)
{
datetime newsTime; string currency; int importance; string text;
if(EconomicNewsGetNext( newsTime, currency, importance, text))
{
CloseAdverse positionsAfter news(newsTime, atr);
}
}

My goals are as follows:
1. Get real-time news timing, importance and currency.
2. Block the EA ±N minutes before/after the news event.
3. Close existing positions if the news is very important.

Question:
How to properly connect the economic calendar to the MT5 EA so that it works in real time without compilation errors? Is there a simple code example that can be integrated into the EA?

Thanks for your help!

Economic Calendar | Financial & Forex News | World Economy Events in Real-Time
Economic Calendar | Financial & Forex News | World Economy Events in Real-Time
  • 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.
 
82106578:
Hello
I am trying to integrate an economic calendar into my MT5 EA so that the robot temporarily blocks trading before important news events. I am using the stub function EconomicNewsGetNext(), but I keep getting compilation errors like:

When you post code please use the CODE button (Alt-S) !  

Traders and coders are working for free:

  • if it is interesting for them personally, or
  • if it is interesting for many members on this forum.

Freelance section of the forum should be used in most of the cases.

Trading applications for MetaTrader 5 to order
Trading applications for MetaTrader 5 to order
  • 2025.11.17
  • www.mql5.com
The largest freelance service with MQL5 application developers
 
82106578:
Hello
I am trying to integrate an economic calendar into my MT5 EA so that the robot temporarily blocks trading before important news events. I am using the stub function EconomicNewsGetNext(), but I keep getting compilation errors like:

Hello colleague,

Did you solve the issue? I am having same issue here. Please share if any solution was reached.

 
Alectrading11 #:

Hello colleague,

Did you solve the issue? I am having same issue here. Please share if any solution was reached.

Here is some code as an example.

//+------------------------------------------------------------------+
//| Check news events and block trading                              |
//+------------------------------------------------------------------+
void CheckNewsAndBlockTrading()
{
   datetime current_time = TimeCurrent();
   news_trading_allowed = true;
   next_important_news = 0;
   next_news_name = "";
   
   // Find the nearest high-impact news
   datetime nearest_news = 0;
   string nearest_name = "";
   int nearest_importance = 0;
   
   for(int i = 0; i < calendar_events_count; i++)
   {
      if(calendar_events[i].processed) continue;
      
      if(FilterByCurrency && base_currency != "" && quote_currency != "")
      {
         if(calendar_events[i].currency != base_currency && 
            calendar_events[i].currency != quote_currency)
         {
            continue;
         }
      }
      
      // Check importance level
      if(calendar_events[i].importance < MinImportance) continue;
      
      datetime news_time = calendar_events[i].time;
      int seconds_before = AlertMinutesBefore * 60;
      
      // Search for the closest upcoming news event
      if(news_time > current_time && (nearest_news == 0 || news_time < nearest_news))
      {
         nearest_news = news_time;
         nearest_name = calendar_events[i].name;
         nearest_importance = calendar_events[i].importance;
      }
      
      // Check if we are currently in the "danger zone" (restricted period)
      if(current_time >= (news_time - seconds_before) && 
         current_time <= (news_time + 3600)) // +1 hour after news release
      {
         news_trading_allowed = false;
         
         // Close positions if the option is enabled
         if(ClosePositionsBeforeNews && current_time >= (news_time - 60) && 
            current_time < news_time && CountPositions() > 0)
         {
            CloseAll();
            Print("Positions closed before news: ", calendar_events[i].name);
         }
         
         // Update status message
         if(current_time < news_time)
         {
            int minutes_left = (int)((news_time - current_time) / 60);
            news_status = "NEWS IN " + IntegerToString(minutes_left) + " MIN";
         }
         else
         {
            int minutes_passed = (int)((current_time - news_time) / 60);
            news_status = "NEWS WAS " + IntegerToString(minutes_passed) + " MIN AGO";
         }
         
         news_status_color = (calendar_events[i].importance >= 3) ? clrRed : 
                           (calendar_events[i].importance == 2) ? clrOrange : clrYellow;
         
         break;
      }
   }
   
   if(news_trading_allowed)
   {
      news_status = "TRADING ALLOWED";
      news_status_color = clrLime;
      
      if(nearest_news > 0)
      {
         next_important_news = nearest_news;
         next_news_name = nearest_name;
         
         int hours_left = (int)((next_important_news - current_time) / 3600);
         int minutes_left = (int)((next_important_news - current_time) / 60) % 60;
         
         news_status += "\nNext news in: " + 
                       IntegerToString(hours_left) + "h " + 
                       IntegerToString(minutes_left) + "m";
      }
   }
}