How a news filter works in a MetaTrader 5 Expert Advisor
Around big releases such as US jobs numbers or a central bank decision, spreads widen and prices can jump past a stop. A news filter keeps a robot out of the market for those minutes.
Where the news times come from
MetaTrader 5 has an economic calendar built in. It is maintained by MetaQuotes and every MT5 terminal receives it, whatever the broker. An Expert Advisor can read it with the calendar functions of MQL5: it asks for the events of the coming days, and each event comes with a time in trade-server time, a country, a currency and an importance level (low, moderate or high).
No website scraping and no outside feed are needed, which matters on the MQL5 Market: products there may not depend on downloads from third-party servers.
What FxBotLab does with it
- Every ten minutes it loads the events of the next seven days for the currencies of the chart: for EURUSD that is EUR and USD, for gold it is USD.
- By default only high-impact events count. Medium-impact events can be added with the
NewsMediuminput. - No new trade is opened from 30 minutes before an event until 30 minutes after it (
NewsBeforeandNewsAfter). - Open trades are kept, with their stop in place, unless
NewsCloseTradesis switched on. Then they are closed when the window starts. - The chart panel shows the next event and the time left, for example "USD Non-Farm Payrolls, in 2 h 10 min", and the reason when a trade is skipped.
Why the filter is off in the Strategy Tester
The calendar functions return no history in the Strategy Tester, so a backtest cannot know when the news was. FxBotLab switches the filter off there and says so on the panel. In practice this means a backtest includes trades that the live robot would have skipped. Keep that in mind when you compare a backtest with a demo account.
Settings worth changing
On 15-minute and 1-hour charts the defaults (30 minutes either side, high impact only) are a reasonable start. On daily charts a news window of an hour hardly matters, because the decision is made once a day on the closed bar. If you trade gold, keep the filter on: US data moves it as much as it moves the dollar pairs.