Discussion of article "Dealing with Time (Part 2): The Functions"

 

New article Dealing with Time (Part 2): The Functions has been published:

Determing the broker offset and GMT automatically. Instead of asking the support of your broker, from whom you will probably receive an insufficient answer (who would be willing to explain a missing hour), we simply look ourselves how they time their prices in the weeks of the time changes — but not cumbersome by hand, we let a program do it — why do we have a PC after all.

Before the functions in the include file DealingWithTime.mqh (and after the macro substitution) the required variables are declared as global variables:

//--- global variables for time switches
int      DST_USD=0,                             // act time shift USD
         DST_EUR=0,                             // act time shift EU
         DST_AUD=0,                             // act time shift Australia
         DST_RUS=0;                             // D'2014.10.26 02:00', -10800,

These variables DST_USD, DST_EUR,.. will have the actual time shift of the USA, the EU,.. They will be updated and set by our functions. In the winter time which is the normal time they are zero: the time is not shifted at that period.

After that we have the variables with the next time the time changeover will take place. They are is mainly needed to know when a new calculation is required in order to save the CPU resources:

datetime nxtSwitch_USD,                         // date of next switch
         nxtSwitch_EUR,                         // date of next switch
         nxtSwitch_AUD,                         // date of next switch
         nxtSwitch_RUB = D'2014.10.26 02:00';   // Russia s different :(

We will consider the Russian situation later in this article.

This structure and its global variable is the heart of all. :)

struct _OffsetBroker
  {
   int   USwinEUwin,                            // US=Winter & EU=Winter
         USsumEUsum,                            // US=Summer & EU=Summer
         USsumEUwin,                            // US=Summer & EU=Winter
         actOffset,                             // actual time offset of the broker
         secFxWiWi,                             // duration of FX in sec
         secFxSuSu,                             // duration of FX in sec
         secFxSuWi,                             // duration of FX in sec
         actSecFX;                              // actual duration of FX in sec
   bool  set;                                   // are all set?
  };
_OffsetBroker OffsetBroker;

We will assign the broker offsets for the three relevant periods and duration of the forex market is open in these periods, for both the actual value and for an easy check set if the values has been assigned. The global variable is named OffsetBroker, we will meet it several times.

Author: Carl Schreiber

 
We are willing to introduce the actual values to all our fox traders to display 
 
We are willing to introduce the actual value to all our fox trade to display 
 

Hello,

As I understand from the article, the function "setBokerOffset ()" should work in the strategy tester as well, but it doesn't work.

void OnTick()
  {
//---
   bool isTimeSet = setBokerOffset();
   if(!isTimeSet)
     {
      Alert("setBokerOffset failed");
      return;
     }
  }
array out of range in 'DealingWithTime.mqh' (201,21)


Does the "The Alternative using it via Input Variables" the only way to get correct times in the strategy tester?

 
Nauris Zukas #:

Hello,

As I understand from the article, the function "setBokerOffset ()" should work in the strategy tester as well, but it doesn't work.


Does the "The Alternative using it via Input Variables" the only way to get correct times in the strategy tester?

I guess that the Strategy Tester hasn't got the quotes and then prev. CopyTime() failed. Check whether the requested and needed data are already locally available.
 
Carl Schreiber #:
I guess that the Strategy Tester hasn't got the quotes and then prev. CopyTime() failed. Check whether the requested and needed data are already locally available.

Ok, I will watch closer CopyTime() and will try to resolve it.

And thank you, very useful article!

 

Hi @Carl Schreiber Happy New Year.

I know this article has been up for a while, but I just found it.  Thank you for sharing this work.  It requires me to do some testing with it to really understand it.  But, I have a simple question for now:

I can see that you have a different day of week calculation rather than relying on the MqlDateTime struct .day_of_week.  Why are you using this other calculation method, is there an accuracy benefit?  Or is it just to avoid the conversion to the struct?

 
I started to code some time basics like HoD()  and then I challenged myself to code other things too as it looks easier to write DoW(..) instead of assign the time to a struct and request a other values as well - feel free to use what you like.
 

This code calculates the DST automatically for European and US brokers:

https://www.mql5.com/en/code/27860

//+------------------------------------------------------------------+
//| Compute the daylight saving time changes in London, UK           |
//| Validated to https://www.timeanddate.com/time/change/uk/london   |
//+------------------------------------------------------------------+
void DST_Europe(int iYear, datetime &dst_start, datetime &dst_end)
  {
   datetime dt1,dt2;
   MqlDateTime st1,st2;
   /* UK DST begins at 01:00 local time on the last Sunday of March
      and ends at 02:00 local time on the last Sunday of October */
   dt1=StringToTime((string)iYear+".03.31 01:00");
   dt2=StringToTime((string)iYear+".10.31 02:00");
   TimeToStruct(dt1,st1);
   TimeToStruct(dt2,st2);
   dst_start=dt1-(st1.day_of_week*86400);
   dst_end  =dt2-(st2.day_of_week*86400);
  }
//+------------------------------------------------------------------+
//| Compute the daylight saving time changes in New York, USA        |
//| Validated to https://www.timeanddate.com/time/change/usa/new-york|
//+------------------------------------------------------------------+
void DST_USA(int iYear, datetime &dst_start, datetime &dst_end)
  {
   datetime dt1,dt2;
   MqlDateTime st1,st2;
   /* US DST begins at 02:00 local time on the second Sunday of March
      and ends at 02:00 local time on the first Sunday of November */
   dt1=StringToTime((string)iYear+".03.14 02:00");
   dt2=StringToTime((string)iYear+".11.07 02:00");
   TimeToStruct(dt1,st1);
   TimeToStruct(dt2,st2);
   dst_start=dt1-(st1.day_of_week*86400);
   dst_end  =dt2-(st2.day_of_week*86400);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnStart()
  {
   datetime dst_start,dst_end;
   dst_start=dst_end=0;

//--- In the European Union, Summer Time begins on the last Sunday in
//--- March and ends on the last Sunday in October.                    |

   Print("========= European Summer Time (DST) =========");
   for(int year=2010; year<=2029; year++)
     {
      DST_Europe(year,dst_start,dst_end);
      Print("DST starts on ",dst_start," and ends on ",dst_end);
     }

//--- In the United States, Summer Time begins on the second Sunday in |
//--- March and ends on the first Sunday in November.                  |

   Print("========= American Summer Time (DST) =========");
   for(int year=2010; year<=2029; year++)
     {
      DST_USA(year,dst_start,dst_end);
      Print("DST starts on ",dst_start," and ends on ",dst_end);
     }
  }
//+------------------------------------------------------------------+

/*
output:
========= European Summer Time (DST) =========
DST starts on 2010.03.28 01:00:00 and ends on 2010.10.31 02:00:00
DST starts on 2011.03.27 01:00:00 and ends on 2011.10.30 02:00:00
DST starts on 2012.03.25 01:00:00 and ends on 2012.10.28 02:00:00
DST starts on 2013.03.31 01:00:00 and ends on 2013.10.27 02:00:00
DST starts on 2014.03.30 01:00:00 and ends on 2014.10.26 02:00:00
DST starts on 2015.03.29 01:00:00 and ends on 2015.10.25 02:00:00
DST starts on 2016.03.27 01:00:00 and ends on 2016.10.30 02:00:00
DST starts on 2017.03.26 01:00:00 and ends on 2017.10.29 02:00:00
DST starts on 2018.03.25 01:00:00 and ends on 2018.10.28 02:00:00
DST starts on 2019.03.31 01:00:00 and ends on 2019.10.27 02:00:00
DST starts on 2020.03.29 01:00:00 and ends on 2020.10.25 02:00:00
DST starts on 2021.03.28 01:00:00 and ends on 2021.10.31 02:00:00
DST starts on 2022.03.27 01:00:00 and ends on 2022.10.30 02:00:00
DST starts on 2023.03.26 01:00:00 and ends on 2023.10.29 02:00:00
DST starts on 2024.03.31 01:00:00 and ends on 2024.10.27 02:00:00
DST starts on 2025.03.30 01:00:00 and ends on 2025.10.26 02:00:00
DST starts on 2026.03.29 01:00:00 and ends on 2026.10.25 02:00:00
DST starts on 2027.03.28 01:00:00 and ends on 2027.10.31 02:00:00
DST starts on 2028.03.26 01:00:00 and ends on 2028.10.29 02:00:00
DST starts on 2029.03.25 01:00:00 and ends on 2029.10.28 02:00:00
========= American Summer Time (DST) =========
DST starts on 2010.03.14 02:00:00 and ends on 2010.11.07 02:00:00
DST starts on 2011.03.13 02:00:00 and ends on 2011.11.06 02:00:00
DST starts on 2012.03.11 02:00:00 and ends on 2012.11.04 02:00:00
DST starts on 2013.03.10 02:00:00 and ends on 2013.11.03 02:00:00
DST starts on 2014.03.09 02:00:00 and ends on 2014.11.02 02:00:00
DST starts on 2015.03.08 02:00:00 and ends on 2015.11.01 02:00:00
DST starts on 2016.03.13 02:00:00 and ends on 2016.11.06 02:00:00
DST starts on 2017.03.12 02:00:00 and ends on 2017.11.05 02:00:00
DST starts on 2018.03.11 02:00:00 and ends on 2018.11.04 02:00:00
DST starts on 2019.03.10 02:00:00 and ends on 2019.11.03 02:00:00
DST starts on 2020.03.08 02:00:00 and ends on 2020.11.01 02:00:00
DST starts on 2021.03.14 02:00:00 and ends on 2021.11.07 02:00:00
DST starts on 2022.03.13 02:00:00 and ends on 2022.11.06 02:00:00
DST starts on 2023.03.12 02:00:00 and ends on 2023.11.05 02:00:00
DST starts on 2024.03.10 02:00:00 and ends on 2024.11.03 02:00:00
DST starts on 2025.03.09 02:00:00 and ends on 2025.11.02 02:00:00
DST starts on 2026.03.08 02:00:00 and ends on 2026.11.01 02:00:00
DST starts on 2027.03.14 02:00:00 and ends on 2027.11.07 02:00:00
DST starts on 2028.03.12 02:00:00 and ends on 2028.11.05 02:00:00
DST starts on 2029.03.11 02:00:00 and ends on 2029.11.04 02:00:00
*/

The above code was used in Forex Market Hours https://www.mql5.com/en/code/27771 to calculate the day-light saving time changes.

Similar functions can be constructed for different areas of the world.

Daylight changes (summer time)
Daylight changes (summer time)
  • www.mql5.com
Compute the daylight saving time changes (start/end of the summer time).
 

Hi @Carl Schreiber,

Great article you've made, it helps a lot. Just want to tell you what I found after using your code.

I'm using eightcap broker and tried to use a simple script to show what the actual time in the respective country and found that Tokyo time is 1 hour late from the actual time. Here's the script that I use,

void OnStart()
  {
   bool isTimeSet = setBokerOffset();
   if(!isTimeSet)
     {
      Alert("setBokerOffset failed");
      return;
     }

   Alert("+++++++++++++++");
   checkTimeOffset(TimeCurrent());
   datetime tGMT  = TimeCurrent() + OffsetBroker.actOffset;    // GMT
   datetime tNY   = tGMT - (NYShift+DST_USD);                  // time in New York
   datetime tLon  = tGMT - (LondonShift+DST_EUR);              // time in London
   datetime tSyd  = tGMT - (SidneyShift+DST_AUD);              // time in Sidney
   datetime tMosc = tGMT - (MoskwaShift+DST_RUS);              // time in Moscow
   datetime tTok  = tGMT - (TokyoShift);                       // time in Tokyo - no DST

   Alert("time NY: ", tNY);
   Alert("time Lon: ", tLon);
   Alert("time Syd: ", tSyd);
   Alert("time Tok: ", tTok);
  }

the code is copy and paste from the article and I just return the value from it. I don't think that the problem from how I used the code because the other session is correct.

Carl Schreiber
Carl Schreiber
  • 2023.03.22
  • www.mql5.com
Trader's profile
 
Luandre Ezra #:

Hi @Carl Schreiber,

Great article you've made, it helps a lot. Just want to tell you what I found after using your code.

I'm using eightcap broker and tried to use a simple script to show what the actual time in the respective country and found that Tokyo time is 1 hour late from the actual time. Here's the script that I use,

the code is copy and paste from the article and I just return the value from it. I don't think that the problem from how I used the code because the other session is correct.

From GMT (=UTC) the time diff is 0900 hours (from https://www.worldtimeserver.com/current_time_in_JP.aspx?city=Tokyo)

= 9*-3600=32400 seconds:

And this is is set:

#define TokyoShift   -32400                           // always 9h
Current local time in Tokyo, Japan
  • www.worldtimeserver.com
Current local time and date in Tokyo, Japan from a trusted independent resource