News Filter in MT5: the Built-in Calendar or an API?
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 calendar | ShowMyTrades calendar API | |
|---|---|---|
| Setup | None | Allow https://showmytrades.com under Tools → Options → Expert Advisors |
| Data source | MetaQuotes, delivered by the broker's server | Public calendar sources, assembled by ShowMyTrades |
| Available on | Brokers that enable it (most do; some white-label servers do not) | Any broker, any terminal with internet |
| Works in MT4 too | No | Yes, same file |
| Works in the strategy tester | Yes, with history | No (no network in the tester) |
| Time zone | Trade server time | UTC (ts in Unix seconds) |
| Values | Forecast, previous, actual, with revisions | Forecast, previous, actual |
| Cost | Free | Free, 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.
//--- 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:
- It depends on the broker. The calendar reaches the terminal through the trade server. If
CalendarValueHistory()returns-1with error5402on your broker, the calendar is not enabled there, and no code will fix that. - It is server time, not UTC. Fine on a live chart. Painful when you compare with anything else.
- 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 situation | Pick |
|---|---|
| MT5 only, broker has the calendar, you backtest the filter | Built-in |
| The same EA for MT4 and MT5 | API: one file, one behaviour |
Broker without the calendar (5402) | API |
| You also run cBots on cTrader | API: the same data on cTrader |
| You want to log the news with the trade for later review | Either; 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.
🔗 Related
- How to add a news filter to your MT4 Expert Advisor: the full include file
- Economic calendar API reference: parameters, formats, limits
- News filter for a cTrader cBot
- What is an Expert Advisor
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.
How to Add a News Filter to Your MT4 Expert Advisor
MetaTrader 4 has no economic calendar for Expert Advisors. This guide adds one in ten minutes: a free calendar API, one include file, one function to call before every entry. Full MQL4 code, no API key.
How to Add a News Filter to a cTrader cBot (Cloud Included)
cTrader has no calendar API for cBots, and cTrader Cloud blocks HTTP requests. Here is a news filter that works on the desktop and in the cloud, with a free calendar over WebSocket and the full C# code.