SessionTime/SessionTimeCheck.mq5
85 lines
5.5 KiB
MQL5
//+------------------------------------------------------------------+
//| SessionTimeCheck.mq5 |
//| Prints session opens in server time around the DST switches and |
//| checks the conversions against hand-calculated values. |
//+------------------------------------------------------------------+
#property copyright "Ghebanoaei Ciprian"
#property link "https://www.mql5.com/en/users/ciprian252"
#property script_show_inputs
#include <SessionTime.mqh>
input int InpWinter = 2; // Broker winter offset (hours)
input ENUM_ST_DST InpDst = ST_DST_US; // Broker DST rule
input int InpYear = 2026; // Year for the table
int g_fail = 0;
//+------------------------------------------------------------------+
//| Compare a result with the expected value and print it |
//+------------------------------------------------------------------+
void Check(const string what, const datetime got, const datetime expected)
{
bool ok = (got == expected);
if(!ok) g_fail++;
PrintFormat("%-48s %s expected %s %s", what, TimeToString(got), TimeToString(expected), ok ? "OK" : "FAIL");
}
//+------------------------------------------------------------------+
//| Short helper: string -> datetime |
//+------------------------------------------------------------------+
datetime D(const string s) { return StringToTime(s); }
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
ST_Broker b;
b.winter = 2; b.dst = ST_DST_US; // the common "New York close = 00:00 server" broker
// --- fixed checks (2026: US DST 8 Mar - 1 Nov, EU summer time 29 Mar - 25 Oct)
Check("London 08:00, 12 Jan (both winter)", ST_ZoneToServer(b, ST_ZONE_LONDON, D("2026.01.12 08:00")), D("2026.01.12 10:00"));
Check("London 08:00, 16 Mar (US DST, UK winter)", ST_ZoneToServer(b, ST_ZONE_LONDON, D("2026.03.16 08:00")), D("2026.03.16 11:00"));
Check("London 08:00, 13 Apr (both summer)", ST_ZoneToServer(b, ST_ZONE_LONDON, D("2026.04.13 08:00")), D("2026.04.13 10:00"));
Check("London 08:00, 26 Oct (UK winter, US DST)", ST_ZoneToServer(b, ST_ZONE_LONDON, D("2026.10.26 08:00")), D("2026.10.26 11:00"));
Check("New York 09:30, 16 Mar", ST_ZoneToServer(b, ST_ZONE_NEWYORK, D("2026.03.16 09:30")), D("2026.03.16 16:30"));
Check("New York 09:30, 12 Jan", ST_ZoneToServer(b, ST_ZONE_NEWYORK, D("2026.01.12 09:30")), D("2026.01.12 16:30"));
Check("Tokyo 09:00, 12 Jan", ST_ZoneToServer(b, ST_ZONE_TOKYO, D("2026.01.12 09:00")), D("2026.01.12 02:00"));
Check("Tokyo 09:00, 13 Apr", ST_ZoneToServer(b, ST_ZONE_TOKYO, D("2026.04.13 09:00")), D("2026.04.13 03:00"));
Check("New York 17:00 Fri 16 Jan = server 00:00", ST_ZoneToServer(b, ST_ZONE_NEWYORK, D("2026.01.16 17:00")), D("2026.01.17 00:00"));
Check("New York 17:00 Fri 17 Jul = server 00:00", ST_ZoneToServer(b, ST_ZONE_NEWYORK, D("2026.07.17 17:00")), D("2026.07.18 00:00"));
Check("Server 11:00 16 Mar -> UTC", ST_ServerToUtc(b, D("2026.03.16 11:00")), D("2026.03.16 08:00"));
ST_Broker eu;
eu.winter = 2; eu.dst = ST_DST_EU; // a broker on Eastern European time
Check("EU-DST broker: London 08:00, 16 Mar", ST_ZoneToServer(eu, ST_ZONE_LONDON, D("2026.03.16 08:00")), D("2026.03.16 10:00"));
Check("EU-DST broker: New York 09:30, 16 Mar", ST_ZoneToServer(eu, ST_ZONE_NEWYORK, D("2026.03.16 09:30")), D("2026.03.16 15:30"));
Check("Sydney 10:00, 12 Jan (AEDT)", ST_ZoneToServer(b, ST_ZONE_SYDNEY, D("2026.01.12 10:00")), D("2026.01.12 01:00"));
Check("Sydney 10:00, 13 Jul (AEST)", ST_ZoneToServer(b, ST_ZONE_SYDNEY, D("2026.07.13 10:00")), D("2026.07.13 03:00"));
PrintFormat("Checks failed: %d", g_fail);
// --- table for the article: Monday session opens, weeks around the switches, user's broker
ST_Broker u;
u.winter = InpWinter; u.dst = InpDst;
PrintFormat("Broker GMT%+d, DST rule %s. Monday opens in server time:", InpWinter, EnumToString(InpDst));
PrintFormat("%-12s %-8s %-8s %-8s %-8s", "Week of", "Tokyo", "London", "NewYork", "NY-LDN");
datetime starts[] = {0, 0};
starts[0] = ST_SundayOf(InpYear, 3, 1) - 14 * 86400 + 86400; // two weeks before 1st Sunday of March, Monday
starts[1] = ST_SundayOf(InpYear, 10, 1) + 86400; // Monday after 1st Sunday of October
for(int s = 0; s < 2; s++)
for(int w = 0; w < 8; w++)
{
datetime day = starts[s] + w * 7 * 86400;
datetime tk = ST_ZoneToServer(u, ST_ZONE_TOKYO, day + 9 * 3600);
datetime ld = ST_ZoneToServer(u, ST_ZONE_LONDON, day + 8 * 3600);
datetime ny = ST_ZoneToServer(u, ST_ZONE_NEWYORK, day + 9 * 3600 + 30 * 60);
PrintFormat("%-12s %-8s %-8s %-8s %dh%02d", TimeToString(day, TIME_DATE),
TimeToString(tk, TIME_MINUTES), TimeToString(ld, TIME_MINUTES), TimeToString(ny, TIME_MINUTES),
(int)((ny - ld) / 3600), (int)((ny - ld) % 3600) / 60);
}
int det;
if(ST_DetectWinterOffset(InpDst, det))
PrintFormat("This terminal: detected winter offset GMT%+d (input %d)", det, InpWinter);
}
//+------------------------------------------------------------------+