Automation·

News Filter in MT5: the Built-in Calendar or an API?

MetaTrader 5 ships an economic calendar that Expert Advisors can read with no download. When it is enough, when it is not, and how to use a free calendar API instead, with MQL5 code for both.

If you write Expert Advisors for MetaTrader 5 you have a choice that MT4 users do not: the terminal ships an economic calendar, and MQL5 can read it with a handful of functions. No download, no URL to allow, nothing to parse.

So why would anyone use an external API? Because the built-in calendar has edges, and three of them matter for a news filter. This guide shows the native code, then the API code, and when to pick which.

⚖️ The Two Options Side by Side

Built-in MQL5 calendarShowMyTrades calendar API
SetupNoneAllow https://showmytrades.com under Tools → Options → Expert Advisors
Data sourceMetaQuotes, delivered by the broker's serverPublic calendar sources, assembled by ShowMyTrades
Available onBrokers that enable it (most do; some white-label servers do not)Any broker, any terminal with internet
Works in MT4 tooNoYes, same file
Works in the strategy testerYes, with historyNo (no network in the tester)
Time zoneTrade server timeUTC (ts in Unix seconds)
ValuesForecast, previous, actual, with revisionsForecast, previous, actual
CostFreeFree, no key

🧱 Option A: the Built-in Calendar

The function below says whether a high-impact release for either currency of the chart's symbol is within the pause window. Times are in trade server time, which is what CalendarValueHistory() expects.

NativeNewsFilter.mqh
//--- MQL5 built-in calendar: true while a high-impact release for the chart's
//--- currencies is within [-minutesBefore, +minutesAfter] of now
bool NativeNewsTime(const int minutesBefore, const int minutesAfter, string &reason)
{
   reason = "";
   datetime now  = TimeTradeServer();
   datetime from = now - minutesAfter * 60;
   datetime to   = now + minutesBefore * 60;

   string currencies[2];
   currencies[0] = SymbolInfoString(_Symbol, SYMBOL_CURRENCY_BASE);
   currencies[1] = SymbolInfoString(_Symbol, SYMBOL_CURRENCY_PROFIT);

   for(int c = 0; c < 2; c++)
   {
      if(c == 1 && currencies[1] == currencies[0]) break;
      MqlCalendarValue values[];
      if(CalendarValueHistory(values, from, to, NULL, currencies[c]) <= 0) continue;
      for(int i = 0; i < ArraySize(values); i++)
      {
         MqlCalendarEvent ev;
         if(!CalendarEventById(values[i].event_id, ev)) continue;
         if(ev.importance != CALENDAR_IMPORTANCE_HIGH) continue;
         reason = currencies[c] + ": " + ev.name;
         return true;
      }
   }
   return false;
}

Call it from OnTick() exactly like any other filter:

string news;
if(NativeNewsTime(30, 30, news)) { Comment("News filter: ", news); return; }

Three things to know before you rely on it:

  1. It depends on the broker. The calendar reaches the terminal through the trade server. If CalendarValueHistory() returns -1 with error 5402 on your broker, the calendar is not enabled there, and no code will fix that.
  2. It is server time, not UTC. Fine on a live chart. Painful when you compare with anything else.
  3. It is MT5 only. An EA you also ship for MT4 needs a second implementation.

🌐 Option B: the Calendar API

The alternative is a free HTTP endpoint that returns the next days of news for the currencies and impact you choose, as CSV or JSON:

https://showmytrades.com/api/public/calendar/v1?currency=EUR,USD&impact=high&days=2&format=csv

The include file SMT_NewsFilter.mqh that reads it is printed in full in the MT4 guide and in the API reference. It compiles in MQL5 unchanged: save it in MQL5\Include, allow https://showmytrades.com in Tools → Options → Expert Advisors, and call:

#include <SMT_NewsFilter.mqh>

void OnTick()
{
   string news;
   if(SMT_IsNewsTime(news)) { Comment("News filter active: ", news); return; }
   Comment("");
   // ... your entry logic ...
}

It downloads once every 15 minutes, compares in UTC with TimeGMT(), and keeps the inputs (SMT_Currencies, SMT_Impact, minutes before and after) in the EA's settings dialog.

🎯 Which One to Pick

Your situationPick
MT5 only, broker has the calendar, you backtest the filterBuilt-in
The same EA for MT4 and MT5API: one file, one behaviour
Broker without the calendar (5402)API
You also run cBots on cTraderAPI: the same data on cTrader
You want to log the news with the trade for later reviewEither; the API gives UTC timestamps that match the rest of your logs

Nothing stops you from using both: the built-in calendar when it answers, the API when it does not. In practice one is enough.

🧪 Backtesting a News Filter

The built-in calendar is the only one of the two that works in the strategy tester, because the tester has no internet. Even there, be careful with what a backtest of a news filter proves: the test uses the calendar as it is known today, with revised times and the events that were added after the fact. A live filter only knows what was scheduled at the time.

❓ FAQ

Is the built-in MT5 calendar free? Yes. It is part of the terminal, delivered through your broker's server, with no setting to enable on your side.

Why does CalendarValueHistory() return nothing on my broker? Some servers, often white-label ones, do not relay the calendar. Check GetLastError(): 5402 means the calendar is not available. Use the API in that case.

Can I filter by the currencies of a different symbol? Yes. Pass the currency codes you want instead of reading them from _Symbol. The API version takes them as an input string, EUR,USD,GBP.

Does the API need an account? No. No key, no login. The limit is 600 requests per minute per IP address, far above what any number of charts needs at one request every 15 minutes.


Whichever calendar you choose, the result shows in the track record: fewer trades opened into a spike, and a smoother drawdown. Track your MT5 account free and see it on the chart.