SessionTime/SessionTime.mqh
184 lines
7.8 KiB
MQL5
//+------------------------------------------------------------------+
//| SessionTime.mqh |
//| Session hours in their own time zone -> broker server time. |
//| Broker model: server = UTC + winter offset (+1 h in its DST). |
//+------------------------------------------------------------------+
#property copyright "Ghebanoaei Ciprian"
#property link "https://www.mql5.com/en/users/ciprian252"
#property strict
enum ENUM_ST_ZONE
{
ST_ZONE_GMT = 0, // GMT / UTC
ST_ZONE_LONDON = 1, // London (GMT+0 / BST +1, EU rule)
ST_ZONE_NEWYORK = 2, // New York (EST -5 / EDT -4, US rule)
ST_ZONE_TOKYO = 3, // Tokyo (+9, no DST)
ST_ZONE_SYDNEY = 4 // Sydney (AEST +10 / AEDT +11, southern rule)
};
enum ENUM_ST_DST
{
ST_DST_NONE = 0, // no daylight saving
ST_DST_US = 1, // US: 2nd Sunday March 02:00 local -> 1st Sunday November 02:00 local
ST_DST_EU = 2 // EU: last Sunday March 01:00 UTC -> last Sunday October 01:00 UTC
};
//+------------------------------------------------------------------+
//| nth Sunday of a month at 00:00 (nth >= 1), last Sunday (nth = 0) |
//+------------------------------------------------------------------+
datetime ST_SundayOf(const int year, const int month, const int nth)
{
MqlDateTime m;
ZeroMemory(m);
m.year = year; m.mon = month; m.day = 1;
TimeToStruct(StructToTime(m), m); // fills day_of_week
int d = 1 + (7 - m.day_of_week) % 7; // first Sunday
if(nth > 0)
d += 7 * (nth - 1);
else
{
int dim = (month == 2) ? ((year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) ? 29 : 28)
: (month == 4 || month == 6 || month == 9 || month == 11) ? 30 : 31;
while(d + 7 <= dim) d += 7;
}
m.day = d; m.hour = 0; m.min = 0; m.sec = 0;
return StructToTime(m);
}
//+------------------------------------------------------------------+
//| US daylight saving in force at this UTC moment (rules since 2007)|
//+------------------------------------------------------------------+
bool ST_UsDst(const datetime utc)
{
MqlDateTime m;
TimeToStruct(utc, m);
datetime a = ST_SundayOf(m.year, 3, 2) + 7 * 3600; // 02:00 EST = 07:00 UTC
datetime b = ST_SundayOf(m.year, 11, 1) + 6 * 3600; // 02:00 EDT = 06:00 UTC
return utc >= a && utc < b;
}
//+------------------------------------------------------------------+
//| EU/UK summer time in force at this UTC moment (01:00 UTC switch) |
//+------------------------------------------------------------------+
bool ST_EuDst(const datetime utc)
{
MqlDateTime m;
TimeToStruct(utc, m);
datetime a = ST_SundayOf(m.year, 3, 0) + 3600;
datetime b = ST_SundayOf(m.year, 10, 0) + 3600;
return utc >= a && utc < b;
}
//+------------------------------------------------------------------+
//| Sydney summer time: 1st Sunday Oct 02:00 -> 1st Sunday Apr 03:00 |
//+------------------------------------------------------------------+
bool ST_AuDst(const datetime utc)
{
MqlDateTime m;
TimeToStruct(utc, m);
datetime end = ST_SundayOf(m.year, 4, 1) - 8 * 3600; // Sunday 03:00 AEDT (UTC+11) = Saturday 16:00 UTC
datetime start = ST_SundayOf(m.year, 10, 1) - 8 * 3600; // Sunday 02:00 AEST (UTC+10) = Saturday 16:00 UTC
return utc < end || utc >= start;
}
//+------------------------------------------------------------------+
//| Zone offset from UTC in hours at this UTC moment |
//+------------------------------------------------------------------+
int ST_ZoneOffset(const ENUM_ST_ZONE zone, const datetime utc)
{
switch(zone)
{
case ST_ZONE_LONDON: return ST_EuDst(utc) ? 1 : 0;
case ST_ZONE_NEWYORK: return ST_UsDst(utc) ? -4 : -5;
case ST_ZONE_TOKYO: return 9;
case ST_ZONE_SYDNEY: return ST_AuDst(utc) ? 11 : 10;
}
return 0;
}
//+------------------------------------------------------------------+
//| Zone wall-clock time -> UTC. The first pass treats the zone time |
//| as UTC; the second corrects the DST state near a clock change. |
//+------------------------------------------------------------------+
datetime ST_ZoneToUtc(const ENUM_ST_ZONE zone, const datetime zoneTime)
{
datetime utc = zoneTime - ST_ZoneOffset(zone, zoneTime) * 3600;
return zoneTime - ST_ZoneOffset(zone, utc) * 3600;
}
//+------------------------------------------------------------------+
//| Broker server time |
//+------------------------------------------------------------------+
struct ST_Broker
{
int winter; // offset in hours outside its DST period
ENUM_ST_DST dst; // DST rule the server follows
};
//+------------------------------------------------------------------+
//| Broker offset from UTC in hours at this UTC moment |
//+------------------------------------------------------------------+
int ST_BrokerOffset(const ST_Broker &b, const datetime utc)
{
if(b.dst == ST_DST_US && ST_UsDst(utc)) return b.winter + 1;
if(b.dst == ST_DST_EU && ST_EuDst(utc)) return b.winter + 1;
return b.winter;
}
//+------------------------------------------------------------------+
//| UTC -> broker server time |
//+------------------------------------------------------------------+
datetime ST_UtcToServer(const ST_Broker &b, const datetime utc)
{
return utc + ST_BrokerOffset(b, utc) * 3600;
}
//+------------------------------------------------------------------+
//| Server time -> UTC. server - winter is UTC, or UTC + 1 h in DST; |
//| that hour only matters on the switch night, when FX is closed. |
//+------------------------------------------------------------------+
datetime ST_ServerToUtc(const ST_Broker &b, const datetime server)
{
return server - ST_BrokerOffset(b, server - b.winter * 3600) * 3600;
}
//+------------------------------------------------------------------+
//| Zone wall-clock time -> broker server time |
//+------------------------------------------------------------------+
datetime ST_ZoneToServer(const ST_Broker &b, const ENUM_ST_ZONE zone, const datetime zoneTime)
{
return ST_UtcToServer(b, ST_ZoneToUtc(zone, zoneTime));
}
//+------------------------------------------------------------------+
//| Live charts only: winter offset from TimeTradeServer()-TimeGMT() |
//| minus the DST hour. Returns false while not connected, and in |
//| the Strategy Tester, where TimeGMT() equals the server time. |
//+------------------------------------------------------------------+
bool ST_DetectWinterOffset(const ENUM_ST_DST dst, int &winter)
{
if(MQLInfoInteger(MQL_TESTER)) return false;
if(!TerminalInfoInteger(TERMINAL_CONNECTED)) return false;
datetime gmt = TimeGMT();
datetime srv = TimeTradeServer();
if(MathAbs((double)(srv - TimeCurrent())) > 7 * 86400) return false; // server time not received yet
int cur = (int)MathRound((double)(srv - gmt) / 3600.0);
if(cur < -12 || cur > 14) return false;
bool inDst = (dst == ST_DST_US && ST_UsDst(gmt)) || (dst == ST_DST_EU && ST_EuDst(gmt));
winter = cur - (inDst ? 1 : 0);
return true;
}
//+------------------------------------------------------------------+
//| "HH:MM" -> minutes after midnight, -1 if invalid |
//+------------------------------------------------------------------+
int ST_ParseHM(const string s)
{
string p[];
if(StringSplit(s, ':', p) < 1) return -1;
int h = (int)StringToInteger(p[0]);
int m = ArraySize(p) > 1 ? (int)StringToInteger(p[1]) : 0;
if(h < 0 || h > 24 || m < 0 || m > 59) return -1;
return h * 60 + m;
}
//+------------------------------------------------------------------+