Economic Calendar Monitor and Cache for Historical Backtesting - MetaTrader 5 Script | Forex Indicators Download - MT4/MT5 Resources

Quotes are timestamped according to the time zone valid on the server at the time each corresponding bar was formed.
Once the bars are formed, they remain unchanged, including their timestamps. The economic calendar, on the other hand, provides information about events (past, present, and future) relative to the server's current time zone. Since many brokers adhere to specific time zone schedules, including turning daylight saving time mode on and off, the timestamps of historical events may shift 1 hour relative to the relevant bar, approximately half the time each year.
Additionally, brokers sometimes change time zones more drastically than just changing to daylight saving time. Historical quotes may be shifted left or right by a few hours relative to the time when the economic event originally occurred, but are now reported by the calendar in the time zone updated by the server.
Considering that news comes from different countries with their own daylight saving time schedules, and your server may be located in a region with other schedules, the time of news release can even "jump" back and forth on the chart in more peculiar ways (e.g., weeks in spring and fall).
All of this may not seem that important online, but what if we want to test a news-based strategy?
Yes, you can say that the MetaTrader beta program itself does not support calendaring, but many traders enjoy trading news, while others who don't like trading should pay attention to the news and simply exit the market before the market goes crazy during the news. So it’s important to use a calendar for backtesting. That's why it is very logical to export the calendar to external storage (file, database) and then import it into the tester. An archiving tool for testers’ calendar experiences in the Algorithmic Trading Book .
Here we have the problem of historical references being out of sync with historical events. For the sake of simplicity, this issue is not addressed in the book.
Now it has been solved, thanks to the extension version CalendarCache.mqh and the presentation indicator CalendarMonitorCachedTZ.mq5 . This is just a slightly changed version of CalendarMonitorCached.mq5 from the book.
This indicator monitors news events and dynamically updates several past and upcoming events on the chart.
All time correction related work is done behind the scenes - in another public library time server DST.mqh . To better understand how time correction works, you can use the script CalendarCSVForDates.mq5 Compare corrected and uncorrected CSV files side by side.
Here's how to embed the library into the source code of two programs - the script and this indicator.
#include <TimeServerDST.mqh> // Included in calendar cache before time zone fix support is enabled #include#include
As with the original indicator, there is a string input calendar cache file where you can provide the cal file name for writing or reading.
When an indicator is attached to an empty online chart calendar cache file , it works instantly with the built-in calendar.
When the indicator is executed with a specific name in the calendar cache file and this file does not exist, the indicator exports the calendar records to the cache file (creates the file) and exits. This is the stage at which timestamps should/could be corrected (see Fix cache times by symbol history below).
When executing indicators using the name of an existing cache file calendar cache file , it loads the cache and works with this copy in the same way as with built-in calendar. This is specifically useful for the tester.

Please don't forget that the tester needs to specify additional files in the directive, in our case - prepared online calibration file #property-tester_file or you should put the cal file into the public folder C:/Users/
Of course, caches can also be loaded into the EA during backtesting and optimization.
Input string fix cache time by symbol history is handled as follows.
If empty, the indicator will be cached without time correction.
To enable time correction during export, you should specify a symbol that will be used for empirical detection of the server's historical time zone. It is based on H1 quote history, preferably "XAUUSD" or "EURUSD".
With this input, only a few lines were added to the new version of the indicator:
if ( stringlength (fix cache time via symbol history)) Cache[].Adjust TZonHistory(FixCachedTimesBySymbolHistory, true );
Method Adjust TZon History was introduced specifically in the calendar cache. The class for timestamp adjustment and its implementation use the internal structure Time Server DST.mqh .
This method can only be called online (not in the tester).
Normally, this method should be called on cache objects populated from the built-in calendar immediately after filling. Otherwise, if the cache was loaded from a cal file, or if the method has been called before, the cache contents may have been adjusted. Then you will apply the fix and get wrong timestamps.
The second parameter ( true ) instructs the method to write the bounds of applied changes to the log. Something like this:
Time correction starts at 2021.07.19 00:30:00 2021.07.19 00 : 30 :00 : 148786 - 10800 difference = - 3600 2021.11.08 01 : 50 : 00 : 135918 - 7200 OK 2022.03.14 04 : 30 : 00 : 161085 - 10800 Difference = - 3600 2022.11.07 04 : 00 :00 : 165962 - 7200 OK 2023.03.13 01 : 50 : 00 : 168500 - 10800 Difference = - 3600 2023.11.06 01 : 50 : 00 : 169270 - 7200 OK 2024.03.11 01 : 50 : 00 : 181258 - 10800 Difference = - 3600 2024.11.04 02 : 30 :00 : 208469-7200 OK
Each row contains the time and ID of the event where the new difference was detected, the server time offset of the event, and what difference must be applied to all subsequent timestamps to eliminate the skew in server time when the calendar is cached.
The attached mqh files (CalendarFilter.mqh, CalendarCache.mqh, QuickSortStructT(Ref).mqh) contain bug fixes and improvements compared to the original version in this book.
November 11, 2024 - Minor bug fixes and updates in CalendarFilter.mqh, CalendarCache.mqh;
November 22, 2024 - Minor bug fixes and improvements in CalendarCache.mqh.
Attachment download
📎 autoptr.mqh (1.86 KB)
📎 calendardefines.mqh (4.66 KB)
📎 calendarfiltercached.mqh (2.38 KB)
📎 datetime.mqh (2.56 KB)
📎 defines.mqh (0.71 KB)
📎 is.mqh (0.98 KB)
📎 mqlerror.mqh (8.53 KB)
📎quicksortstructtref.mqh (7.86 KB)
📎 stringutils.mqh (1.91 KB)
📎 tableau.mqh (8.79 KB)
📎 prtf.mqh (1.49 KB)
📎 calendarfilter.mqh (28.8 KB)
📎 timeserverdst.mqh (16.98 KB)
📎 calendarcache.mqh (28.54 KB)
📎 calendarmonitorcachedtz.mq5 (8.41 KB)
Source: MQL5 #53393
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •