Strategy Tester News Filter with Corrected Calendar Export

Strategy Tester News Filter with Corrected Calendar Export
Download ALL MT5 experts (1731)
YouTube Video Thumbnail



Similar MetaTrader Tools

Strategy Tester News Filter with Corrected Calendar Export

Info

The Strategy Tester News Filter with Corrected Calendar Export is a Expert Advisor for MetaTrader 5 that the problemmany news filters rely on the mql5 economic calendar (calendarvaluehistory), which is unavailable in the strategy tester. This causes filters to be ineffective during backtests, leading Expert Advisors (EAs) to trade through significant news events like Non-Farm Payrolls (NFP), CPI, or rate decisions.

Usage

This tool is typically used for automated trading on major forex pairs and gold.

Platform

This Expert Advisor works exclusively on MetaTrader 5 (both build 600+ and newer versions).

Setup

Place the downloaded file in MQL5/Experts folder via File ? Open Data Folder in MetaTrader 5.


How to Install and Use Strategy Tester News Filter with Corrected Calendar Export

1. Installation: Open the "File" menu, select "Open Data Folder," navigate to MQL/Experts, paste your file, and restart the terminal.

2. Activation: Drag the EA from the Navigator onto a chart, ensure "Allow live trading" is checked in the Common tab, and verify the AutoTrading button is green.

3. Optimization: Right-click your chart, choose "Expert List," click "Properties" to adjust inputs, and save your preferred setup as a set file for future use.

4. Maintenance: Regularly check the "Experts" tab in the terminal window to monitor trade logs and potential execution errors.

Frequently Asked Questions

Q: Why is my EA not opening trades? A: Check the "AutoTrading" button, ensure "Allow live trading" is enabled, and verify your broker allows automated trading on your account type.

Q: Can I run multiple EAs on one chart? A: No, each chart can only host one active EA; however, you can open multiple charts for different currency pairs to run several EAs.

Q: What does the "smiley face" icon mean? A: A smiley face in the top-right corner of the chart indicates the EA is successfully running; a frowny face means it is disabled.

What this tool does

THE PROBLEMMany news filters rely on the MQL5 economic calendar (CalendarValueHistory), which is unavailable in the Strategy Tester.

Typical Use Case

This Expert Advisor excels in automated trading and technical analysis on MetaTrader 5.

Compatible Platform & Setup

This Expert Advisor works on MetaTrader 5. Place the file in the MQL5/Experts folder and restart the terminal.

Description & Settings

Related: Strategy Tester Report Parser for Deals and Trades - another powerful expert for MetaTrader 5 traders.

THE PROBLEM

Many news filters rely on the MQL5 economic calendar (CalendarValueHistory), which is unavailable in the Strategy Tester. This causes filters to be ineffective during backtests, leading Expert Advisors (EAs) to trade through significant news events like Non-Farm Payrolls (NFP), CPI, or rate decisions. This disparity can result in backtests that do not accurately reflect live trading behavior. In one instance, a pending order on XAUUSD was filled just before a US CPI release, resulting in a stop-loss execution with substantial slippage, causing a single trade to lose 6.5 times the planned risk.

Also recommended: Fx Craft Example EA: MA Cross Strategy Explained - similar expert with strong performance on MetaTrader 5.


A secondary, less recognized issue involves how the MetaTrader terminal adjusts historical calendar event times using the *current* server offset. When exporting data during summer in a GMT+2 or GMT+3 timezone, winter events are inaccurately shifted by one hour later (e.g., NFP at 16:30 instead of 15:30 server time). A filter based on these adjusted times would consequently block trading during the incorrect hour for half of the year.

THE SOLUTION - THREE FILES

NewsCalendarExport.mq5 (Script): This script exports economic calendar events to Terminal\Common\Files\NewsCalendar.csv. It recalculates each event's time using the specific server offset applicable to the event's original date. Upon completion, it performs a self-check, verifying that the most frequent time for USD events remains consistent across both winter and summer time adjustments.

NewsFilter.mqh (Class CNewsFilter): This class provides a unified news filter for both live trading and backtesting. In live trading, it reads the economic calendar directly (queried at most once per minute). Within the Strategy Tester, it reads the data from the generated CSV file. The filter uses only the currencies associated with the trading symbol (e.g., for EURUSD, it considers EUR and USD; for XAUUSD, it considers USD; for index CFDs quoted in USD, it considers USD).

NewsFilter_Example.mq5 (Example EA): This is a minimal Expert Advisor designed to demonstrate the news filter's functionality. It prints messages when a news event window begins and ends, and visually highlights the blocked periods on the chart when running in the Strategy Tester's visual mode.

HOW TO USE

1. Place the NewsFilter.mqh file into your MQL5\Include directory.
2. Place the NewsCalendarExport.mq5 file into your MQL5\Scripts directory.
3. Place the NewsFilter_Example.mq5 file into your MQL5\Experts directory.
4. Compile the script and the example EA. The NewsFilter.mqh class will be compiled automatically with them.

Run the NewsCalendarExport script once from any chart in a connected MetaTrader terminal. Check the Experts tab for a confirmation message such as: "OK: same time in winter and summer".

In your custom EA, include the following code snippet:

#include <NewsFilter.mqh>

CNewsFilter news;

int OnInit()
{
news.Init(_Symbol, 30, 30); // Blocks 30 minutes before and after each event
return(INIT_SUCCEEDED);
}

void OnTick()
{
if(news.IsBlocked())
return; // Skip trade logic during blocked news windows

// ... your trade entry logic here ...
}

Proceed with your backtesting as usual. The terminal's journal will indicate the number of news events loaded for the specific symbol.

SCRIPT INPUTS

Export events from: Specifies the start date for exporting calendar data.

Also export scheduled events N days ahead: Includes upcoming events in the export.

Currencies: A comma-separated list of currencies to include (default: USD,EUR,GBP,JPY,CAD,AUD,NZD,CHF).

Importance: Filters events by impact level (high impact only, or medium + high impact).

Broker server DST rule: Determines how Daylight Saving Time is handled. Options include "US rule" (default, typical for most GMT+2/+3 brokers), "EU rule", or "no DST".

File name: The name of the CSV file where the data is stored (located in Terminal\Common\Files).

CNewsFilter::Init Parameters:
symbol: The trading symbol.
minutesBefore: Number of minutes before an event to block.
minutesAfter: Number of minutes after an event to block.
mediumToo: Boolean, set to true to include medium-impact news (default is false).
csvFile: The name of the CSV file to read (default is "NewsCalendar.csv").

NOTES AND LIMITATIONS

This tool has been tested on FTMO (GMT+2 / GMT+3, US DST rule) and IC Markets EU. An example output shows 5645 events exported, with 1897 requiring a one-hour shift. Prior to the correction, the most frequent USD event time was 16:30 in winter and 15:30 in summer. After applying the correction, this time became consistently 15:30 in both seasons.

If the script's self-check indicates discrepancies between winter and summer event times, try selecting a different "Broker server DST rule".

The MQL5 economic calendar data has limitations and is dependent on the MetaTrader terminal. Data on my terminal begins in 2020, meaning older backtests may not contain any news events.

For indices and commodities, the filter uses the quote currency to determine relevant news events, as these instruments may not have direct currency associations.

Remote and cloud-based testing agents cannot access the Terminal\Common\Files directory. For optimal performance, utilize local testing agents.

Some currencies, such as AUD, have a limited number of high-impact events in the MQL5 calendar. Consider using the "Medium + high impact" option for such currencies.

This filter is designed to prevent new trades from opening during news events. The management of already open positions remains the responsibility of your EA.

To ensure your data is up-to-date, re-run the NewsCalendarExport script periodically to include newly added events.

Version 1.00 (News Filter) - Initial release. Version 1.01 (Example EA) adds functionality to draw blocked news windows on the chart.

You may also like: Manual Trade Pad for MetaTrader 5 Strategy Tester - excellent alternative for expert users on MetaTrader 5.

Source Code

#property copyright "RobotFX"
#property link "https://robotfx.org"
#property version     "1.00"
#property description "This code is educational. Feel free to use, modify and upgrade as you wish. Visit ROBOTFX.ORG for professional trading tools"
#property script_show_inputs
enum ENUM_NEWS_IMPORTANCE
  {
   NEWS_HIGH        = 0,
   NEWS_MEDIUM_HIGH = 1
  };

.......

Leave your opinion, ask a question, share some knowledge

⚠ Limitations & Risk Warning

  • This tool is provided for educational and testing purposes only.
  • Past performance does not guarantee future results.
  • Trading involves substantial risk of loss. Use on a demo account first.
  • Results may vary depending on market conditions, broker, and settings.
  • We recommend thorough backtesting and forward testing before using with real funds.
© ROBOTFX Free educational tools by RobotFX. Use entirely at your own risk; we are not liable for any financial losses incurred.