This script introduces the function TimeServerDaylightSavings() that is missing among the built-in ones, which provide only TimeDaylightSavings() for local computer. In addition the attached header mqh-file includes some other helpful server-bound time-related functions, in particular allowing you to know if your broker uses DST switches in general.
All this is based on empirical analysis of history of quotes from your broker. The whole idea is described in the algotrading book in the section about Daylight saving time (DST). Actually this script is a refined and extended version of the script presented in the book.
In brief, the method analyzes statistics of week opening hours and deduce GMT offsets of your broker. Two distinct maximums in the statistics of the offsets, if they map into adjacent hours, most likely correspond to standard (“winter”) and daylight-saving (“summer”) time.
Please, note that in the Northern and Southern Hemispheres, time zones are adjusted in the opposite direction: in the Northern Hemisphere, 1 hour is added in the “spring” (March or April) and subtracted in the “fall” (October or November), while in the Southern Hemisphere it is the other way around (because they have all the seasons swapped).
Due to specificity of the analysis it’s recommended to run the code for the most liquid Forex ticker, which is usually EURUSD.
Here is the API:
// Server time zone and DST current information struct ServerTimeZone // according to history analysis of week opening hours { int offsetGMT; // time zone offset in seconds against UTC/GMT for current week int offsetDST; // DST correction in seconds (included in offsetGMT, as per MQL5) bool supportDST; // DST changes are detected in the quotes }; // Estimate server time zone and DST mode from H1 quotes history ServerTimeZone TimeServerZone( const uint lookupYears = 0, // by default, all available bars, otherwise 3 year seems enough const string symbol = NULL) // by default, symbol of current chart // Estimate server time DST mode correction (in seconds) for current week int TimeServerDaylightSavings(const uint lookupYears = 0, const string symbol = NULL); // Estimate server time zone offset (in seconds) int TimeServerGMTOffsetEmpiric(const uint lookupYears = 0, const string symbol = NULL); // Estimate if server is DST-enabled (true/false) bool TimeServerDaylightSavingsSupported(const uint lookupYears = 0, const string symbol = NULL); // Analogue of TimeGMTOffset() function for trade server, difference in seconds int TimeServerGMTOffset(); // TimeGMT() - TimeTradeServer()
The functions TimeServerDaylightSavings(), TimeServerGMTOffsetEmpiric(), TimeServerDaylightSavingsSupported() are just wrappers for TimeServerZone()so if you need more than one characteristic it’s preferable to use the later one and read values from the struct ServerTimeZone.
The function TimeServerGMTOffset() does not use history analysis but rather calculate offset directly via MQL5 functions (as TimeGMT() – TimeTradeServer()).
The test script outputs all acquired data into the log, for example:
¹ ⁞ Built-in functions ⁞ TimeLocal()=2024.10.05 00:39:01 / ok TimeCurrent()=2024.10.05 00:38:59 / ok TimeTradeServer()=2024.10.05 00:39:01 / ok TimeGMT()=2024.10.04 21:39:01 / ok TimeGMTOffset()=-10800 / ok TimeDaylightSavings()=0 / ok ² ⁞ Add-on over built-in functions ⁞ TimeServerGMTOffset()=-10800 / ok ³ ⁞ Estimation of server TZ with DST based on week opening hours in history ⁞ TimeServerDaylightSavings()=-3600 / ok [offsetGMT] [offsetDST] [supportDST] [0] -10800 -3600 true
In this case it’s detected that the server is currently in DST mode, whereas the local computer is not.
Please, remember that system clocks of the local computer and the server can normally show slightly different times (seconds and even minutes), even if they are in the same time zone. Also note that built-in TimeTradeServer() function returns a synthetic datetime: it’s the server’s time with hourly accuracy, but it inherits intra-hour fractions from the local clock. This is done so in MQL5 to simplify conversions between time zones – TimeLocal(), TimeGMT() returned in the “local format” as well, and the trade server time.
You can enable detailed print out of the data being analyzed by preprocessor directive:
#define PRINT_DST_DETAILS
which should be placed in your code before include:
#include "TimeServerDST.mqh"
Here is an example of details in the log:
Got 20023 H1 bars, ~834 days Week opening hours stats: 30 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 83 54 0 Time Zone changes (UTC±X before/after weekstart): [weekstart] [before] [DSTb] [after] [DSTa] [0] 2021.07.25 21:00:00 -2147483648 false -1 false [1] 2021.11.07 22:00:00 0 true 0 false [2] 2022.03.13 21:00:00 0 false 0 true [3] 2022.11.06 22:00:00 0 true 0 false [4] 2023.03.12 21:00:00 0 false 0 true [5] 2023.11.05 22:00:00 0 true 0 false [6] 2024.03.11 00:00:00 0 false 2 false 3 different timezones detected in quotes, 1 DST candidates Server time offset: UTC+2 STD Possible time offset: UTC+3 DST TimeServerDaylightSavings()=-3600 / ok