TimeZoneInfo/server_tz_changes.mq5
amrali 407eabbda2
2025-10-13 16:14:10 +03:00

98 lines
7.9 KiB
MQL5

//+------------------------------------------------------------------+
//| server_tz_changes |
//| Copyright © 2018, Amr Ali |
//| https://www.mql5.com/en/users/amrali |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2018, Amr Ali"
#property link "https://www.mql5.com/en/users/amrali"
#property version "2.17"
#property description "Estimation of the server's TZ and DST changes based on week opening hours in history."
#property script_show_inputs
// Uncomment the following line to see debug messages
// #define PRINT_TZ_DETAILS
#include "TimeZoneInfo.mqh"
//--- input variables
input datetime StartDate = D'2008.03.01';
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
if((SymbolInfoInteger(_Symbol, SYMBOL_TRADE_CALC_MODE) != SYMBOL_CALC_MODE_FOREX
&& SymbolInfoInteger(_Symbol, SYMBOL_TRADE_CALC_MODE) != SYMBOL_CALC_MODE_FOREX_NO_LEVERAGE
&& SymbolInfoInteger(_Symbol, SYMBOL_INDUSTRY) != INDUSTRY_COMMODITIES_PRECIOUS)
|| !(SymbolInfoString(_Symbol, SYMBOL_CURRENCY_BASE) == "USD" || SymbolInfoString(_Symbol, SYMBOL_CURRENCY_PROFIT) == "USD"))
{
Print("It's recommended to apply this script to a major Forex currency pair or precious Metal");
}
PrintFormat("%s ~ %s", AccountInfoString(ACCOUNT_COMPANY), AccountInfoString(ACCOUNT_SERVER));
const int hour = 3600;
const datetime from = fmax(StartDate, (datetime)SeriesInfoInteger(_Symbol, PERIOD_H1, SERIES_FIRSTDATE) + 5 * 24 * hour);
const datetime to = (datetime)SeriesInfoInteger(_Symbol, PERIOD_H1, SERIES_LASTBAR_DATE);
datetime array[];
datetime prev_srvtime = -1;
datetime prev_utctime = -1;
int prev_utcOffset = -1;
int prev_DST = -1;
ResetLastError();
const int n = CopyTime(_Symbol, PERIOD_H1, from, to, array);
if(n<=0)
{
Print("Unable to copy time. Error = ",GetLastError());
return;
}
PrintFormat("Got %d H1 bars on %s, ~%d days [%s - %s]", n, _Symbol, n / 24, t2s(array[0]), t2s(array[n - 1]));
//--- use the current chart's symbol for estimation of the server's TZ/DST
CTimeZoneInfo::SetUsingGoldSymbol(false);
CTimeZoneInfo broker(ZONE_ID_BROKER);
Print("Server TZ Changes: ");
for(int i = 0; i < n; ++i)
{
broker.SetLocalTime(array[i]);
int utcOffset = broker.TimeGMTOffset();
int DST = broker.TimeDaylightSavings();
datetime srvtime = array[i];
datetime utctime = array[i] - utcOffset;
// Only show times when the offset change is detected
if(utcOffset != prev_utcOffset)
{
PrintFormat("Server time = %s | GMT = %s | Server time zone = GMT%+g %s",
t2s(srvtime),
t2s(utctime),
utcOffset/(double)hour,
DST ? "(DST)" : "");
}
// Save previous data
prev_srvtime = srvtime;
prev_utctime = utctime;
prev_utcOffset = utcOffset;
prev_DST = DST;
}
Print("done.");
}
//+------------------------------------------------------------------+
//| Formats time with the weekday name => "Wed, 2023.02.14 01:59" |
//+------------------------------------------------------------------+
string t2s(const datetime t, const int mode = TIME_DATE | TIME_MINUTES)
{
const string days[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
const int i = ((int)((uint)t / 86400 + 4) % 7); // i = DayOfWeek(t)
return days[i % 7] + ", " + TimeToString(t, mode);
}
//+------------------------------------------------------------------+