Calendar-Based Backtesting: an Event-Driven Trading EA

Calendar-Based Backtesting: an Event-Driven Trading EA
DOWNLOAD all MT5 librarys (393)
YouTube Video Thumbnail

Similar MetaTrader Tools

Calendar-Based Backtesting: an Event-Driven Trading EA

Info

The Calendar-Based Backtesting: an Event-Driven Trading EA is a Library for MetaTrader 5 that the code is full of comments for better understanding. This Description explains the main idea, and the ways to use/reuse the code.

Usage

This tool is typically used for enhancing chart analysis and decision making.

Platform

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

Setup

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


How to Install and Use Calendar-Based Backtesting: an Event-Driven Trading EA

1. Storage: Place library files in the MQL/Libraries directory to ensure they are accessible to your projects.

2. Implementation: Include the library in your code using the #import directive, ensuring you match the exact function names and parameters.

3. Compilation: Ensure the library is present in the directory before you compile your main EA or script, as the compiler links them during this phase.

4. Management: Keep libraries organized in sub-folders if you manage many custom functions to maintain a clean project structure.

Frequently Asked Questions

Q: What is a library file used for? A: Libraries store reusable code modules, allowing you to centralize common logic used by multiple EAs or indicators.

Q: Is a library executable? A: No, libraries are non-executable files containing functions; they must be imported into an EA, indicator, or script to function.

Q: Can I update a library while the platform is running? A: You should compile your EA or script after updating a library to ensure the latest code changes are integrated.

What this tool does

The code is full of comments for better understanding.

Typical Use Case

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

Compatible Platform & Setup

This Library works on MetaTrader 5. Place the file in the MQL5/Libraries folder and restart the terminal.

Description & Settings

Related: CHistory Position Info Class - another powerful library for MetaTrader 5 traders.


The code is full of comments for better understanding. This Description explains the main idea, and the ways to use/reuse the code. This framework consists of three key components:1. CalendarRetriever.mq5 (Script) – This script fetches past economic news events and saves them to files in the "Common" folder of your terminal. These files will later be read by the expert advisor during backtesting. The script requires two input parameters:

Also recommended: Inc Momentum On Array - similar library with strong performance on MetaTrader 5.


Start Date – The date from which past news events should be retrieved.

End Date – The last date for which news events should be stored After running this script, it will generate a "Calendar" folder in the "Common" directory of the terminal the following text files will be then created:

Countries.txt – Contains information about each MQLCalendarCountry

Events.txt – Stores event details such as name, impact and countryid.

News.bin – Holds historical news event values, including timestamps. 2. CalendarFile.mq (Include File) – This file provides a structured way to handle news data in the expert advisor. It contains two main classes:

CalendarFileWriter – Used internally by the script to store news data.

CalendarFileReader – Used in the expert advisor to read and process news data. It includes functions to load event data and track upcoming news in the OnTick function. 3. NewsBacktest.mq5 (Expert Advisor) – This EA uses the stored data to simulate real-time news conditions in the strategy tester. It reads the saved news data using the Load functions and executes trades accordingly. How to Use the Framework:

Run the CalendarRetriever Script: Select the Symbol you'd like to backtest and run the CalendarRetriever.mq5 script, with the specified start and end date.

The Expert Advisor

The EA uses the CalendarFileReader class to load the data using these functions:

LoadEvents() – Loads event details.

LoadCountries() – Loads country information.

LoadValues() – Loads historical news values.

It then processes the news events in the OnTick function to determine when a news event is happening. Use Propagate() to predict, Track() to react.

The CalendarFileReader.Propagate() function will set the next index to the next upcoming event, this should be called on each tick.

The CalendarFileReader.Track() function returns true when a news event has passed and sets the last index to that particular event. Also called in the OnTick() function, but the result should be checked.

Backtest the Strategy Open the Strategy Tester in MetaTrader 5, Select NewsBacktest.mq5 and Run the Backtest in Visual Mode

How to Adapt It to Your Own News-Based Strategies:
This framework is designed to be flexible, allowing traders to develop their own strategies around economic news events. Here are some ways you can modify it:

Adjust the event filtering logic in OnTick() to react to specific news types (e.g., only trade after NFP or CPI releases).

Modify the order execution logic to implement different strategies, such as fading the initial move instead of breakout trading.

Use additional indicators alongside news events to refine entry signals.

Change the expiration time of pending orders based on event impact level.
This solution makes backtesting news-based strategies as realistic as possible within the limitations of MetaTrader 5. By first retrieving historical news data and then simulating its impact on the market, traders can gain valuable insights into how their strategies would perform in live conditions.

You may also like: Inc AD X On Array - excellent alternative for library users on MetaTrader 5.

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 does not own any of the code provided on this platform. All tools are freely available on the internet; we simply index and re-offer them for download. We are not responsible for any financial losses that may occur. Trading responsibilities rely solely on the traders downloading and using the displayed Expert Advisors, indicators, and scripts. These tools are provided for educational purposes only and may require modification or optimization to align with a trader's specific strategy or needs.
© ROBOTFX - Best MetaTrader Expert Advisors & Indicators