BrokerDST/BrokerDaylightSchedule.mq5

188 lines
16 KiB
MQL5
Raw Permalink Normal View History

2025-09-19 20:13:39 +00:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| BrokerDaylightSchedule.mq5 |
//| Copyright <EFBFBD> 2018, Amr Ali |
//| https://www.mql5.com/en/users/amrali |
//+------------------------------------------------------------------+
#property copyright "Copyright <00> 2018, Amr Ali"
#property link "https://www.mql5.com/en/users/amrali"
#property version "1.400"
#property description "Script to determine whether your Broker follows the US, UK or AU daylight saving time (DST) schedule."
//+------------------------------------------------------------------+
//| Time functions |
//+------------------------------------------------------------------+
int TimeYear(const datetime t) {MqlDateTime st; TimeToStruct(t, st); return(st.year); }
int TimeHour(const datetime t) {MqlDateTime st; TimeToStruct(t, st); return(st.hour); }
//+------------------------------------------------------------------+
//| Return the date for the "Nth" Sunday for the iYear and iMonth. |
//+------------------------------------------------------------------+
datetime GetNthSunday(int iYear, int iMonth, int Nth)
{
// datetime dt=StringToTime((string)iYear+"."+(string)iMonth+".01"); // get date of first of month
MqlDateTime st= {};
st.year=iYear;
st.mon=iMonth;
st.day=1;
datetime dt=StructToTime(st); // get date of first of month
if(Nth<1)
return(0);
if(Nth>5)
Nth=5;
TimeToStruct(dt,st);
int SundayDOM=(7-st.day_of_week)%7; // 1st Sunday Day of Month
dt+=(SundayDOM+7*(Nth-1))*86400;
TimeToStruct(dt,st);
if(st.mon!=iMonth)
dt-=7*86400;
return(dt);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
#define WEEKSECS (7*24*60*60) // seconds in a week
//+------------------------------------------------------------------+
//| determine whether broker follows the US, UK or AU DST schedule |
//+------------------------------------------------------------------+
int BrokerDaylightSchedule(void)
{
string symbol = Symbol();
//--- if the chart symbol is not forex or gold, then use EURUSD symbol.
long CalcMode = SymbolInfoInteger(symbol,SYMBOL_TRADE_CALC_MODE);
if(!(CalcMode == SYMBOL_CALC_MODE_FOREX || CalcMode == SYMBOL_CALC_MODE_FOREX_NO_LEVERAGE
|| StringSubstr(symbol,0,3) == "XAU" || StringSubstr(symbol,0,4) == "GOLD"))
{
symbol = "EURUSD";
bool is_custom = false;
if(!SymbolExist(symbol,is_custom))
for(int i=0; i < SymbolsTotal(0); i++)
if(StringFind(SymbolName(i,0),symbol) == 0)
{
symbol=SymbolName(i,0); // symbol may have suffix
break;
}
SymbolSelect(symbol,true);
}
datetime lastbar = iTime(symbol, PERIOD_H1, 0);
int iYear = TimeYear(lastbar);
datetime dst_switch_au = GetNthSunday(iYear,4,1); // the first Sunday of April for the AU switch
datetime dst_switch_uk = GetNthSunday(iYear,3,5); // the last Sunday of March for the UK switch
datetime dst_switch_us = GetNthSunday(iYear,3,2); // the second Sunday of March for the US switch
if(lastbar < dst_switch_au + WEEKSECS)
{
iYear--;
dst_switch_au = GetNthSunday(iYear,10,1); // the first Sunday of October for the AU switch
dst_switch_uk = GetNthSunday(iYear,10,5); // the last Sunday of October for the UK switch
dst_switch_us = GetNthSunday(iYear,11,1); // the first Sunday of November for the US switch
}
#define GETBAR(var, time) \
var = iTime(symbol,PERIOD_H1, iBarShift(symbol,PERIOD_H1,(time),false)); \
/* Print("bartime = ", var); */ \
if(var == 0) \
{ \
PrintFormat(">> error: history for '%s' is not available.", symbol); \
return(-1); \
}
datetime LastBarWk, LastBarPrevWk;
//--- compare bar times at the time of AU switch
GETBAR(LastBarWk, dst_switch_au + WEEKSECS);
GETBAR(LastBarPrevWk, dst_switch_au);
if(TimeHour(LastBarWk) != TimeHour(LastBarPrevWk)) // server time changed at the time of AU switch
{
Print("DST_AU : server dst begins on the first Sunday of October (+1) and ends on the first Sunday of April (-1)");
return(1);
}
//--- compare bar times at the time of UK switch
GETBAR(LastBarWk, dst_switch_uk + WEEKSECS);
GETBAR(LastBarPrevWk, dst_switch_uk);
if(TimeHour(LastBarWk) != TimeHour(LastBarPrevWk)) // server time changed at the time of UK switch
{
Print("DST_UK : server dst begins on the last Sunday of March (+1) and ends on the last Sunday of October (-1)");
return(2);
}
//--- compare bar times at the time of US switch
GETBAR(LastBarWk, dst_switch_us + WEEKSECS);
GETBAR(LastBarPrevWk, dst_switch_us);
if(TimeHour(LastBarWk) == TimeHour(LastBarPrevWk)) // server time DID NOT change at the time of US switch
{
Print("DST_US : server dst begins on the second Sunday of March (+1) and ends on the first Sunday of November (-1)");
return(3);
}
//---
Print("DST_NONE");
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnStart()
{
Print("Server : ", AccountInfoString(ACCOUNT_SERVER));
Print("Time : ", TimeTradeServer());
PrintFormat("Offset : GMT%+g", (TimeTradeServer() - TimeGMT()) / 3600.0);
BrokerDaylightSchedule();
}
//+------------------------------------------------------------------+
// sample output on different fx brokers
/*
Server : ICMarketsSC-Demo
Time : 2024.03.30 02:36:09
Offset : GMT+3
DST_US : server dst begins on the second Sunday of March (+1) and ends on the first Sunday of November (-1)
Server : Exness-MT5Trial
Time : 2024.03.29 23:37:05
Offset : GMT+0
DST_NONE
Server : OctaFX-Demo
Time : 2024.03.30 01:37:14
Offset : GMT+2
DST_UK : server dst begins on the last Sunday of March (+1) and ends on the last Sunday of October (-1)
Server : AdmiralsSC-Demo
Time : 2024.03.30 01:37:52
Offset : GMT+2
DST_UK : server dst begins on the last Sunday of March (+1) and ends on the last Sunday of October (-1)
Server : RannForex-Server
Time : 2024.03.30 01:37:32
Offset : GMT+2
DST_UK : server dst begins on the last Sunday of March (+1) and ends on the last Sunday of October (-1)
Server : XMGlobal-MT5 7
Time : 2024.03.30 02:48:16
Offset : GMT+2
DST_UK : server dst begins on the last Sunday of March (+1) and ends on the last Sunday of October (-1)
Server : FxPro-MT5
Time : 2024.04.12 04:00:12
Offset : GMT+3
DST_UK : server dst begins on the last Sunday of March (+1) and ends on the last Sunday of October (-1)
Server : FXOpen-MT5
Time : 2024.04.12 04:01:11
Offset : GMT+3
DST_US : server dst begins on the second Sunday of March (+1) and ends on the first Sunday of November (-1)
Server : Tickmill-Demo
Time : 2024.04.12 04:02:13
Offset : GMT+3
DST_US : server dst begins on the second Sunday of March (+1) and ends on the first Sunday of November (-1)
Server : Trading.comMarkets-MT5
Time : 2024.04.13 20:58:36
Offset : GMT-4
DST_US : server dst begins on the second Sunday of March (+1) and ends on the first Sunday of November (-1)
*/