I have one more bug that I've come across. Not sure if it is only on Sundays (which is usually when get some time to code and troubleshoot), but my broker (Active Trades) seems to be detected as a Timezone of GMT-21 which is incorrect (should be GMT+1). Any idea what could be causing this? I am using BTCUSD pair so it is trading on Sundays.
In my code, I pass a candle datetime and from there try to determine whether the sessions was open at that time (i.e. I need to be able to throw any candle at it and it should tell me whether it was printed during the session, with weekends always being closed), so I am doing this:
I have one more bug that I've come across. Not sure if it is only on Sundays (which is usually when get some time to code and troubleshoot), but my broker (Active Trades) seems to be detected as a Timezone of GMT-21 which is incorrect (should be GMT+1). Any idea what could be causing this? I am using BTCUSD pair so it is trading on Sundays.
In my code, I pass a candle datetime and from there try to determine whether the sessions was open at that time (i.e. I need to be able to throw any candle at it and it should tell me whether it was printed during the session, with weekends always being closed), so I am doing this:
Thanks
Update: Using Gold fixed the above problem for me. :)
Great work on all your libraries (upvote from me)!
I have a minor issue and perhaps you could point me out to the correct library (or function) if I haven't seen it.
I am located at UTC+3 DST (Athens time zone or Cyprus). I run the BrokerDaylightSchedule.mq5 and I get the following for the Nasdaq symbol:
Server : PepperstoneUK-Demo
Time : 2025.05.17 16:22:46
Offset : GMT+3
DST_US : server dst begins on the second Sunday of March (+1) and ends on the first Sunday of November (-1)
Nasdaq opens always at 16:30 at my time zone except for a couple of weeks where NY has switched to DST but Athens hasn't. In this case Nasdaq opens at 15:30 my local (Greek) time but Metatrader still displays 16:30 (since it switches DST on US times). As such, there is no problem with my experts since they always know that Nasdaq bar with time 16:30 is the first one of the market open.
The problem arrives when I want to make my expert run on FTSE (UK time zone). FTSE opens at 10:00 local time but when US enters DST and UK hasn't entered it yet, the open bar at Metatrader arrives at 11:00 (for almost 2-3 weeks until UK switches to DST). How do you handle that issue? Do you have any ready made library to handle these kind of issues or you do it manually, eg pseudocode below:
// remove 7 hours (from current bar time) to match the correct US timedatetime realUSTime = TIME_NOW - 7 * 3600; // or time[0] - 7 * 3600int utcDiff = (IsDaylightSavingTime(ZONE_ID_NEWYORK, realUSTime) ? 1 : 0) -
(IsDaylightSavingTime(ZONE_ID_LONDON, realUSTime) ? 1 : 0); // Maybe we have to add 5 hours here for the London time?
if (utcDiff != 0) {
// One of the 2 times (US or UK) has switched to DST or exited from it. Either add or subtract one hour on the Local time of the target (the UK one). eg. something like below:// datetime realUKTime = realUSTime + 5 * 3600 + utcDiff * 3600;
}
The problem arrives when I want to make my expert run on FTSE (UK time zone). FTSE opens at 10:00 local time but when US enters DST and UK hasn't entered it yet, the open bar at Metatrader arrives at 11:00 (for almost 2-3 weeks until UK switches to DST). How do you handle that issue? Do you have any ready made library to handle these kind of issues or you do it manually, eg pseudocode below:
It is better to use a session object to handle dst changes behind the scenes and to focus on the task at hand. (The lib objects will take care of UTC/DST discrepancies between your server's and London time "FTSE").
#include "SessionHours.mqh"voidOnStart()
{
Print("\n========== Working with FTSE session hours ==========");
const ENUM_ZONE_ID FTSE_zone_id = ZONE_ID_LONDON;
CSessionHours FTSE_tz(FTSE_zone_id);
//--- FTSE session hours are set to 8:00 am - 4:30 pm local London time
FTSE_tz.BeginLocalTime(8, 00); // override the local session begin time using a begin local hour and minute (08:00 am London time)
FTSE_tz.EndLocalTime(16, 30); // override the local session end time using an end local hour and minute (04:30 pm London time)//--- Refresh the current local time and print session hours for the day
FTSE_tz.RefreshTime();
Print("TimeLocal() : ", FTSE_tz.TimeLocal());
Print("ToString() : ", FTSE_tz.ToString());
Print("BeginLocalTime() : ", FTSE_tz.BeginLocalTime());
Print("EndLocalTime() : ", FTSE_tz.EndLocalTime());
Print("CheckLocalSession() : ", FTSE_tz.CheckLocalSession());
Print("SecRemainingSession() : ", FTSE_tz.SecRemainingSession());
Print("SecondsToString() : ", CSessionHours::SecondsToString(FTSE_tz.SecRemainingSession()));
string state_str = FTSE_tz.CheckLocalSession() ? "open, ends in " + CSessionHours::SecondsToString(FTSE_tz.SecRemainingSession()) : "closed";
PrintFormat("FTSE session is %s.", state_str);
//--- conversion to broker timesdatetime beginlocal = FTSE_tz.BeginLocalTime();
datetime endlocal = FTSE_tz.EndLocalTime();
datetime beginbroker= CTimeZoneInfo::ConvertTimeForPlace(beginlocal, FTSE_zone_id, ZONE_ID_BROKER);
datetime endbroker = CTimeZoneInfo::ConvertTimeForPlace(endlocal, FTSE_zone_id, ZONE_ID_BROKER);
Print("-----------------------------------");
Print("broker begin : ", CTimeZoneInfo::FormatTimeForPlace(beginbroker, ZONE_ID_BROKER));
Print("broker end : ", CTimeZoneInfo::FormatTimeForPlace(endbroker, ZONE_ID_BROKER));
Print("broker time : ", TimeTradeServer());
Print("broker time : ", CTimeZoneInfo::FormatTimeForPlace(TimeTradeServer(), ZONE_ID_BROKER));
}
Example output on my broker (ICMarkets GMT+3):
// example output:/*
========== Working with FTSE session hours ==========
TimeLocal() : 2025.05.21 21:50:21
ToString() : Wed, 2025.05.21 21:50:21 GMT+1 DST [London]
BeginLocalTime() : 2025.05.21 08:00:00
EndLocalTime() : 2025.05.21 16:30:00
CheckLocalSession() : false
SecRemainingSession() : 0
SecondsToString() : 00:00:00
FTSE session is closed.
-----------------------------------
broker begin : Wed, 2025.05.21 10:00:00 GMT+3 DST [ICMarketsSC-Demo]
broker end : Wed, 2025.05.21 18:30:00 GMT+3 DST [ICMarketsSC-Demo]
broker time : 2025.05.21 23:50:21
broker time : Wed, 2025.05.21 23:50:21 GMT+3 DST [ICMarketsSC-Demo]
*/
It is better to use a session object to handle dst changes behind the scenes and to focus on the task at hand. (The lib objects will take care of UTC/DST discrepancies between your server's and London time "FTSE").
To trade the FTSE session in your EA, the above code can be rearranged into something like this:
#include "SessionHours.mqh"//--- global variablesconst ENUM_ZONE_ID FTSE_zone_id = ZONE_ID_LONDON;
CSessionHours FTSE_tz(FTSE_zone_id);
intOnInit()
{
//--- FTSE session hours are set to 8:00 am - 4:30 pm local London time
FTSE_tz.BeginLocalTime(8, 00); // override the local session begin time using a begin local hour and minute (08:00 am London time)
FTSE_tz.EndLocalTime(16, 30); // override the local session end time using an end local hour and minute (04:30 pm London time)return (INIT_SUCCEEDED);
}
voidOnTick()
{
//--- Refresh the current local time and session hours for the day
FTSE_tz.RefreshTime();
//--- Trade the FTSE session when it is open, and until 2 hours before close. if(FTSE_tz.CheckLocalSession() == true && FTSE_tz.SecRemainingSession() > 2 * 3600)
{
// TradingBlock();
}
}
I am trying to find some error in the `CTimeZoneInfo` class. Running the following on history data (debugging):
#include "SessionHours.mqh"//+------------------------------------------------------------------+//| Expert initialization function |//+------------------------------------------------------------------+intOnInit()
{
//---Print("\n========== Working with FTSE session hours ==========");
const ENUM_ZONE_ID FTSE_zone_id = ZONE_ID_LONDON;
CSessionHours FTSE_tz(FTSE_zone_id);
//--- FTSE session hours are set to 8:00 am - 4:30 pm local London time
FTSE_tz.BeginLocalTime(8, 00); // override the local session begin time using a begin local hour and minute (08:00 am London time)
FTSE_tz.EndLocalTime(16, 30); // override the local session end time using an end local hour and minute (04:30 pm London time)//--- Refresh the current local time and print session hours for the day
FTSE_tz.RefreshTime();
Print("TimeLocal() : ", FTSE_tz.TimeLocal());
Print("ToString() : ", FTSE_tz.ToString());
Print("BeginLocalTime() : ", FTSE_tz.BeginLocalTime());
Print("EndLocalTime() : ", FTSE_tz.EndLocalTime());
Print("CheckLocalSession() : ", FTSE_tz.CheckLocalSession());
Print("SecRemainingSession() : ", FTSE_tz.SecRemainingSession());
Print("SecondsToString() : ", CSessionHours::SecondsToString(FTSE_tz.SecRemainingSession()));
string state_str = FTSE_tz.CheckLocalSession() ? "open, ends in " + CSessionHours::SecondsToString(FTSE_tz.SecRemainingSession()) : "closed";
PrintFormat("FTSE session is %s.", state_str);
//--- conversion to broker timesdatetime beginlocal = FTSE_tz.BeginLocalTime();
datetime endlocal = FTSE_tz.EndLocalTime();
datetime beginbroker= CTimeZoneInfo::ConvertTimeForPlace(beginlocal, FTSE_zone_id, ZONE_ID_BROKER);
datetime endbroker = CTimeZoneInfo::ConvertTimeForPlace(endlocal, FTSE_zone_id, ZONE_ID_BROKER);
Print("-----------------------------------");
Print("broker begin : ", CTimeZoneInfo::FormatTimeForPlace(beginbroker, ZONE_ID_BROKER));
Print("broker end : ", CTimeZoneInfo::FormatTimeForPlace(endbroker, ZONE_ID_BROKER));
Print("broker time : ", TimeTradeServer());
Print("broker time : ", CTimeZoneInfo::FormatTimeForPlace(TimeTradeServer(), ZONE_ID_BROKER));
//---return(INIT_SUCCEEDED);
}
I get different outputs from the app (especially in the TimeLocal and broker time).
2025.09.1213:57:57.947 XAUUSD: load 27bytes of history data to synchronize in0:00:00.0012025.09.1213:57:57.947 XAUUSD: history synchronized from2016.01.04 to 2025.09.052025.09.1213:57:57.967 XAUUSD,H1: history cache allocated for10205 bars and contains 5935 bars from2024.01.0201:00 to 2024.12.3121:002025.09.1213:57:57.967 XAUUSD,H1: history begins from2024.01.0201:002025.09.1213:57:57.9672025.01.0904:42:00 TimeLocal() : 2025.01.0902:42:002025.09.1213:57:57.9672025.01.0904:42:00 ToString() : Thu, 2025.01.0902:42:00 GMT+0 STD [London]
2025.09.1213:57:57.9672025.01.0904:42:00 BeginLocalTime() : 2025.01.0908:00:002025.09.1213:57:57.9672025.01.0904:42:00 EndLocalTime() : 2025.01.0916:30:002025.09.1213:57:57.9672025.01.0904:42:00 CheckLocalSession() : false
2025.09.1213:57:57.9672025.01.0904:42:00 SecRemainingSession() : 496792025.09.1213:57:57.9672025.01.0904:42:00 SecondsToString() : 13:47:592025.09.1213:57:57.9672025.01.0904:42:00 FTSE session is closed.
2025.09.1213:57:57.9672025.01.0904:42:00 -----------------------------------
2025.09.1213:57:57.9672025.01.0904:42:00 broker begin : Thu, 2025.01.0910:00:00 GMT+2 STD [PepperstoneUK-Demo]
2025.09.1213:57:57.9672025.01.0904:42:00 broker end : Thu, 2025.01.0918:30:00 GMT+2 STD [PepperstoneUK-Demo]
2025.09.1213:57:57.9672025.01.0904:42:00 broker time : 2025.01.0904:42:002025.09.1213:57:57.9672025.01.0904:42:00 broker time : Thu, 2025.01.0904:42:00 GMT+2 STD [PepperstoneUK-Demo]
The below is the same exact options to the above...
2025.09.1213:59:20.369 XAUUSD: load 27bytes of history data to synchronize in0:00:00.0002025.09.1213:59:20.369 XAUUSD: history synchronized from2016.01.04 to 2025.09.052025.09.1213:59:20.389 XAUUSD,H1: history cache allocated for10205 bars and contains 5935 bars from2024.01.0201:00 to 2024.12.3121:002025.09.1213:59:20.389 XAUUSD,H1: history begins from2024.01.0201:002025.09.1213:59:20.3892025.01.0900:03:45 TimeLocal() : 2025.01.0822:03:452025.09.1213:59:20.3892025.01.0900:03:45 ToString() : Wed, 2025.01.0822:03:45 GMT+0 STD [London]
2025.09.1213:59:20.3892025.01.0900:03:45 BeginLocalTime() : 2025.01.0808:00:002025.09.1213:59:20.3892025.01.0900:03:45 EndLocalTime() : 2025.01.0816:30:002025.09.1213:59:20.3892025.01.0900:03:45 CheckLocalSession() : false
2025.09.1213:59:20.3892025.01.0900:03:45 SecRemainingSession() : 02025.09.1213:59:20.3892025.01.0900:03:45 SecondsToString() : 00:00:002025.09.1213:59:20.3892025.01.0900:03:45 FTSE session is closed.
2025.09.1213:59:20.3892025.01.0900:03:45 -----------------------------------
2025.09.1213:59:20.3892025.01.0900:03:45 broker begin : Wed, 2025.01.0810:00:00 GMT+2 STD [PepperstoneUK-Demo]
2025.09.1213:59:20.3892025.01.0900:03:45 broker end : Wed, 2025.01.0818:30:00 GMT+2 STD [PepperstoneUK-Demo]
2025.09.1213:59:20.3892025.01.0900:03:45 broker time : 2025.01.0900:03:452025.09.1213:59:20.3892025.01.0900:03:45 broker time : Thu, 2025.01.0900:03:45 GMT+2 STD [PepperstoneUK-Demo]
The options are:
Observe something. None of the 2 runs have a TimeLocal() of 2025.01.01 or at least the first next working day after new year.
Also, I observed that the TimeLocal() returns constantly different time but it is around 5 hours difference every time it is run (I don't know if it does have to do anything).
Finally, I have one more question if you can answer. Inside the `HistoryServerDaylightSavings` you have the following code:
//--- lets analyze the first H1 bar of the trading week (the first bar after weekend)//--- now, check if the server time falls in the US Daylight Savings Timeif(CTimeZoneInfo::IsDaylightSavingsTime(ZONE_ID_NEWYORK, FstBarWk))
{
//--- compensate the effect of US switch on the server time by adding +1 hour
FstBarWk += 1*HOURSECS;
}
Can't this be replaced to something like:
int iYear = GetYear(weekend);
datetime dst_start = Date(iYear, 03, 08); // the second Sunday of March for the US switchdatetime dst_end = Date(iYear, 11, 01); // the first Sunday of November for the US switchif (weekend>=dst_start && weekend<dst_end) {
FstBarWk += 1*HOURSECS;
}
You use that 'if' statement on the `HistoryServerGMTOffset` method and it is more simple than calling the `IsDaylightSavingsTime(...)` method which repeatedly instantiates a new `CTimeZoneInfo` class and then re-calls the `SetLocalTime` method resulting in big call stack (from the recursion).
Maybe it could be simplified or am I missing something?
Thanks for the hard work! You have developed a truly amazing library!
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
Hi,
I have one more bug that I've come across. Not sure if it is only on Sundays (which is usually when get some time to code and troubleshoot), but my broker (Active Trades) seems to be detected as a Timezone of GMT-21 which is incorrect (should be GMT+1). Any idea what could be causing this? I am using BTCUSD pair so it is trading on Sundays.
In my code, I pass a candle datetime and from there try to determine whether the sessions was open at that time (i.e. I need to be able to throw any candle at it and it should tell me whether it was printed during the session, with weekends always being closed), so I am doing this:
Thanks
Hi,
I have one more bug that I've come across. Not sure if it is only on Sundays (which is usually when get some time to code and troubleshoot), but my broker (Active Trades) seems to be detected as a Timezone of GMT-21 which is incorrect (should be GMT+1). Any idea what could be causing this? I am using BTCUSD pair so it is trading on Sundays.
In my code, I pass a candle datetime and from there try to determine whether the sessions was open at that time (i.e. I need to be able to throw any candle at it and it should tell me whether it was printed during the session, with weekends always being closed), so I am doing this:
Thanks
Hi @amrali.
Great work on all your libraries (upvote from me)!
I have a minor issue and perhaps you could point me out to the correct library (or function) if I haven't seen it.
I am located at UTC+3 DST (Athens time zone or Cyprus). I run the BrokerDaylightSchedule.mq5 and I get the following for the Nasdaq symbol:
Server : PepperstoneUK-Demo
Time : 2025.05.17 16:22:46
Offset : GMT+3
DST_US : server dst begins on the second Sunday of March (+1) and ends on the first Sunday of November (-1)
Nasdaq opens always at 16:30 at my time zone except for a couple of weeks where NY has switched to DST but Athens hasn't. In this case Nasdaq opens at 15:30 my local (Greek) time but Metatrader still displays 16:30 (since it switches DST on US times). As such, there is no problem with my experts since they always know that Nasdaq bar with time 16:30 is the first one of the market open.
The problem arrives when I want to make my expert run on FTSE (UK time zone). FTSE opens at 10:00 local time but when US enters DST and UK hasn't entered it yet, the open bar at Metatrader arrives at 11:00 (for almost 2-3 weeks until UK switches to DST). How do you handle that issue? Do you have any ready made library to handle these kind of issues or you do it manually, eg pseudocode below:
Thanks in advance!
The problem arrives when I want to make my expert run on FTSE (UK time zone). FTSE opens at 10:00 local time but when US enters DST and UK hasn't entered it yet, the open bar at Metatrader arrives at 11:00 (for almost 2-3 weeks until UK switches to DST). How do you handle that issue? Do you have any ready made library to handle these kind of issues or you do it manually, eg pseudocode below:
It is better to use a session object to handle dst changes behind the scenes and to focus on the task at hand. (The lib objects will take care of UTC/DST discrepancies between your server's and London time "FTSE").
Example output on my broker (ICMarkets GMT+3):
It is better to use a session object to handle dst changes behind the scenes and to focus on the task at hand. (The lib objects will take care of UTC/DST discrepancies between your server's and London time "FTSE").
Example output on my broker (ICMarkets GMT+3):
Thank you very much!
Thank you very much!
To trade the FTSE session in your EA, the above code can be rearranged into something like this:
Hi @amrali.
I am trying to find some error in the `CTimeZoneInfo` class. Running the following on history data (debugging):
I get different outputs from the app (especially in the TimeLocal and broker time).
The below is the same exact options to the above...
The options are:
Observe something. None of the 2 runs have a TimeLocal() of 2025.01.01 or at least the first next working day after new year.
Also, I observed that the TimeLocal() returns constantly different time but it is around 5 hours difference every time it is run (I don't know if it does have to do anything).
Finally, I have one more question if you can answer. Inside the `HistoryServerDaylightSavings` you have the following code:
Can't this be replaced to something like:
You use that 'if' statement on the `HistoryServerGMTOffset` method and it is more simple than calling the `IsDaylightSavingsTime(...)` method which repeatedly instantiates a new `CTimeZoneInfo` class and then re-calls the `SetLocalTime` method resulting in big call stack (from the recursion).
Maybe it could be simplified or am I missing something?
Thanks for the hard work! You have developed a truly amazing library!
I am trying to find some error in the `CTimeZoneInfo` class. Running the following on history data (debugging):
I get different outputs from the app (especially in the TimeLocal and broker time).
The below is the same exact options to the above...
Just a guess: try to disable the visual mode and/or choose real ticks.