TimeGMT Library for Strategy Tester - MetaTrader 5 Library - MT4/MT5 Resources


TimeGMT() function
Returns GMT calculated taking into account the DST switch based on the local time on the computer running the client. There are 2 variants of this function.
Call without parameters
DateTimeTimeGMT ( );
Call using MqlDateTime type parameters
date time time GMT ( Mql date time & dt_struct // structure type variable );
However, During testing in the strategy tester, timeGMT() Always equal to TimeTradeServer() simulated server time.
Time GMT library
The library will fix the TimeGMT() function to provide real GMT during testing in the strategy tester. This library will install global API hooks (via macro replacement) for both versions of TimeGMT.
Class C time GMT { people : staticDateTimeTimeGMT ( blank ) ; static datetimetimeGMT ( Mqldatetime &dt_struct) ; }
usage:
Before testing an Expert Advisor using a news filter or any time limit in the strategy tester, just include this line at the beginning of the code and recompile the source code in MetaEditor.
#include "timeGMT.mqh"
- (Optional) To see debug messages, add this line before the #include directive:
#define PRINT_GMT_DETAILSExpert advisors or indicators do not require additional modifications or any function calls.
Recalculation of the broker's offset is done only when the Expert Advisor is launched and every Sunday and Monday at midnight. The calculated proxy offset is cached in memory and will be used in subsequent calls to TimeGMT.
By default, the library will search for and load the XAUUSD symbol to estimate the server's time zone offset. XAUUSD can provide more reliable results (especially for brokers that follow the EU Daylight Savings Time schedule) The weeks during US DST are out of sync with the EU DST schedule (March and late October). Alternatively, if your broker follows the US DST schedule, or has no schedule at all, then using chart symbols will work too. Call CTimeGMT::SetUsingGoldSymbol() with "false" to use the current chart's symbol instead of XAUUSD.
To determine a broker's Daylight Saving Time (DST) schedule, you can use this script https://www.mql5.com/en/code/48650
//+------------------------------------------------------------------+ //|Set the option to use the XAUUSD (gold) symbol to estimate | //|Determine the server's TZ/DST by analyzing H1 quote history. | //| TRUE: Search and load gold symbols (default behavior). | //| FALSE: Use the symbol of the current chart. | //+------------------------------------------------------------------+ void CTimeGMT::SetUsingGoldSymbol( const boolean enabled= true );
notes :
As a side effect XAUUSD starts one hour after forex trading, so the dst switch in the strategy tester will happen after one hour.
To explore the libraries in the Strategy Tester, test the demo Expert Advisor in the Strategy Tester
These are the results of running the Expert Advisor in the Strategy Tester:
AUD/USD, M1 (ICMarketsSC - Demo): 1 minute OHLC scale generated AUDUSD, M1: TestExperts\TimeGMT_TestEA.ex5 from 2023.03 . 05:00 to 2023.05 . 01 00 : 00 starting from input: timer time = 24 2023.03 . 05 00:00:00 time current ( ) = sun, 2023.03 . 05 00:00:00 | timeTradingServer () = sun, 2023.03 . 05 00:00:00 | Time GMT () = Saturday, 2023.03 . 04 22:00:00 | Broker Offset = 7200 ( GMT + 2 ) 2023.03 . 06 00:00:00 time current ( ) = Monday, 2023.03 . 06 00:00:00 | timeTradingServer () = Monday, 2023.03 . 06 00:00:00 | Time GMT () = Sun, 2023.03 . 05 22:00:00 | Broker Offset = 7200 ( GMT + 2 ) 2023.03 . 07 00:00:00 time current ( ) = Tuesday, 2023.03 . 07 00:00:00 | timeTradeServer () = Tuesday, 2023.03 . 07 00:00:00 | Time GMT () = Monday, 2023.03 . 06 22:00:00 | Broker Offset = 7200 ( GMT+ 2 ) 2023.03 . 08 00:00:00 time current ( ) = Wednesday, 2023.03 . 08 00:00:00 | timeTradingServer () = Wednesday, 2023.03 . 08 00:00:00 | Time GMT () = Tuesday, 2023.03 . 07 22:00:00 | Broker Offset = 7200 ( GMT+ 2 ) 2023.03 . 09 00:00:00 time current ( ) = Thursday, 2023.03 . 09 00:00:00 | timeTradeServer () = Thursday, 2023.03 . 09 00:00:00 | Time GMT () = Wednesday,number " > 2023.03. 08 22:00:00 | Broker Offset = 7200 (GMT+ 2 ) 2023.03 . 10 00:00:00 time current () = Friday, 2023.03 . 10 00:00:00 | time trade server () = Friday, 2023.03 . 10:00:00 | Time GMT () = Thursday , 2023.03 . 09:00:00 | Broker Offset = 7200 ( GMT + 2 ) 2023.03 . Time Current () = Friday , 2023.03.10 : 56 : 59 | TimeTradeServer () = Saturday , 2023.03.11 :00 : 00 | TimeGMT () = Friday, 2023.03.10 : 00 :00 | Broker Offset = 7200 (GMT+2 ) 2023.03.12:00:00 time current() = Friday, 2023.03.10.23:56:59 | timeTradingServer() = sun, 2023.03.12:00 : 00 | timeGMT ( ) = Saturday 2023.03.11 22:00:00 | Broker Offset = 7200 ( GMT + 2 ) 2023.03.13 00:00:00 time current ( ) = Monday , 2023.03 . 13 00:00:00 | time trading server() = Monday, 2023.03.13:00:00 | Time GMT ( ) = Sun , 2023.03.12 : 00 : 00 | Broker Offset = 10800 (GMT + 3 ) 2023.03.Time Current ( ) = Tuesday , 2023.03.14 : 00 : 00 | TimeTradingServer () = Tuesday , 2023.03.14 : 00 : 00 | TimeGMT ( ) = Monday , 2023.03.13 : 00 : 00 | Broker Offset = 10800 ( GMT+s="number">3) 2023.03 . 15 00:00:00 time current ( ) = Wednesday, 2023.03 . 15 00:00:00 | timeTradeServer () = Wednesday, 2023.03 . 15 00:00:00 | Time GMT () = Tuesday, 2023.03 . 14 21:00:00 | Broker Offset = 10800 ( GMT + 3 ) 2023.03 . 16 00:00:00 time current ( ) = Thursday, 2023.03 . 16 00:00:00 | timeTradingServer () = Thursday, 2023.03 . 16 00:00:00 | Time GMT () = Wednesday, 2023.03 . 15 21:00:00 | Broker Offset = 10800 ( GMT + 3 ) 2023.03 . 17th 00:00:00 time current ( ) = Friday, 2023.03 . 17th 00:00:00 | timeTradingServer () = Friday, 2023.03 . 17th 00:00:00 | Time GMT () = Thursday, 2023.03 . 16 21:00:00 | Broker Offset = 10800 ( GMT + 3 ) 2023.03 . 18 00:00:00 time current ( ) = Friday, 2023.03 . 17th 23:56:59 | timeTradeServer () = Saturday, 2023.03 . 18 00:00:00 | Time GMT () = Friday, 2023.03 . 17th 21:00:00 | Broker Offset = 10800 ( GMT+ 3 ) 2023.03 . 19 00:00:00 time current ( ) = Friday, 2023.03 . 17: 23 : 56 : 59 | timeTradingServer () = sun, 2023.03 . 19 00:00:00 | Time GMT () = Saturday, 2023.03 . 18 21:00:00 | Broker Offset = 10800 ( GMT+ 3 )
2024.03.30 - v.1.25: Fix broker GMT offset. Currently, the library only scans the H1 bar on the GOLD chart since it has the most accurate start time.
2024.04.09 - v.1.33: Fixed potential issue with calculating broker GMT offset for GMT+0 brokers during the Christmas holidays.
2024.04.12 - v.1.35: Fixed an issue in broker GMT offset calculation for a small number of brokers that do not offer gold trading.
2024.07.07 - v.1.36: Fixed an issue with correct detection of gold symbols on some brokers.
2024.10.21 - v.1.40: Improved detection of GOLD symbol and fallback to EURUSD symbol.
2024.10.26 - v.1.47: Added better error handling and debugging code. Renamed the HistoryBrokerOffset() method to TimeServerGMTOffset().
2024.10.28 - v.1.49: Convert all processing time macros to functions to avoid double calculation of parameters within the macro body. More code cleanup in other lines.
2024.10.30 - v.1.50: Fixed an issue with incorrect GMT offset estimation in XAUEUR quotes for some brokers.
2024.11.23 - v.1.55: Added an option to turn off default loading of gold symbols for estimating the server's TZ/DST. Call CTimeGMT::SetUsingGoldSymbol() with "false" to use the current chart's symbol.
2024.12.12 - v.1.60: Improved performance of HistoryServerGMTOffset() function and other minor code changes.
2024.12.14 - v.1.61: Improved the performance of FindSymbol() function.
2024.12.17 - v.1.62: Further optimize the HistoryServerGMTOffset() function.
2024.12.24 - v.1.63: Fixed potential issue in HistoryServerGMTOffset() function.
Broker’s time zone and DST schedule
Greenwich Mean Time (GMT), also known as Coordinated Universal Time (UTC), is used as the world's official time reference. You'll often see time zones expressed as UTC - 3h or GMT - 3h. In this example, (-3h) refers to a time zone that is three hours behind UTC or GMT. UTC+3h or GMT +3h means that the time zone is 3 hours ahead of UTC. The time difference between our local time and GMT (UTC) is called the GMT offset.
The foreign exchange market opens at 17:00 New York time on Sunday (GMT-5:00 in winter and GMT-4:00 in summer) and closes at the same time on Friday. The Forex market starts at 17:00 New York Sun Time, which corresponds to 10:00 Sunday evening Universal Time (UTC) in winter (09:00 Sunday UTC time in summer). Forex closes on Friday at 10:00 PM UTC in winter (09:00 PM UTC in summer). Gold and silver spot markets typically open one hour later. association
Each Forex broker has its own time zone and server time. Therefore, the start of the trading week (H1 candle) is variable between brokers and may vary Server time Sunday 02:00 PM For brokers in San Francisco (GMT-8), the highest Server time Monday 09:00 AM Available to brokers in Sydney (GMT+11). Trading week end times also vary Friday 02:00 PM server time , until Server time Saturday morning 09:00.
Every broker is free to choose Daylight Saving Time (DST). And DST is not necessarily the same as that time zone. Sometimes they use a mix of EU time zones and US Daylight Savings Time instead of EU Daylight Savings Time. For brokers that do not follow the US schedule, there will be a +/- 1 hour difference in server times at the start (and end) of each week for the same broker throughout the year. To handle these changes, recalculation of broker offsets must be done on a weekly basis to enable timely detection of broker DST changes.
Therefore, using the +2 time zone (+3 in the US time zone during the summer) means that the weekly candles start at midnight on Monday, there are 5 D1 candles per week, and the start of the daily candle (and H4) is the start of the new Forex day. Each week ends before midnight on Saturday. There are no candles on Saturday and Sunday. Simply, the time on these servers is always 7 hours ahead of New York and is expressed as NY+7. This is by far the most common setup, but there are many less common variations.
Details of calculation of true GMT
When a call to TimeGMT() is intercepted by the library, it first checks if it is not running in the strategy tester and then returns the original function. If a strategy tester is detected, the code starts calculating the broker offset (see below), and then the return value is simply the current time of the trading server in the client minus this offset.
True GMT = TimeTradingServer () - ServerGMTOffset (of the current trading week)
This ' server GMT offset ' itself can be estimated as the difference between two known times by analyzing H1 quote history:
1) The time when the first bar of the trading week appears on the chart gold chart (FstBarWk),
2) The starting time of New York GOLD trading corresponds to 18:00 UTC time on Sunday.
ServerGMTOffset = FstBarWk - UTC (New York Sun, 18:00 )
BrokerOffset is calculated using the optimized modified processing time(2) function of the algorithm published by Carl Schreiber here
According to https://www.timeanddate.com/time/change/usa/new-york
For example, if FstBarWk on the GOLD chart starts at 01:00 on Monday, and the bar is winter in New York, then the BrokerOffset can be calculated as:
ServerGMTOffset = (FstBarWK, Monday, 01:00 ) - ( Sun, 23:00 UTC ) = 2 hours (GMT + 2 )
And, if it's summer in New York, then BrokerOffset:
ServerGMTOffset = (FstBarWK, Monday, 01:00 ) - ( Sun, 22:00 UTC ) = 3 hours (GMT + 3 )
Convert session time to broker's server time
Traders can use TimeGMT() to convert the session time to the corresponding server time to determine the opening or closing time of a specific market. For example, if a trader wants to trade during the Asian (Tokyo) trading session, they must adjust their trading hours accordingly
1] Determine the offset of the broker’s trading server
Integer server GMT offset = time transaction server () - time GMT ();
2] Convert the local time of a specific market to the server time of the corresponding broker
Date and time Server_time = Tokyo_time – Tokyo_GMTOffset + ServerGMTOffsetIf the destination time zone is currently in daylight saving time, the second term in the equation (xxx_GMTOffset) should be increased by +1 hour.
When testing in the strategy tester, TimeGMT() is always equal to TimeTradeServer() simulated server time, so the conversion of the session time is wrong. The library will fix the TimeGMT() function to provide true GMT so that the converted session time becomes accurate during testing in the strategy tester.
Attachment download
📎 TimeGMT.mqh (40.52 KB)
📎 TimeGMT_TestEA.mq5 (18.97 KB)
Source: MQL5 #48291
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •