merging two expression: GMT offset by DayOfYear

 

I'm trying to merge the DST gmt filter rules(using DayOfYear filter), but can't get it to work; this is my coding:

link to "Day of Year" calendar: http://www.soils.wisc.edu/cgi-bin/asig/doyCal.rb

/// I preferred this shorter version, but not working correctly in backtest ///

int    StartHour             = 8;        // Start Hour of Trade Session 
int    EndHour               = 17;       // End Hour of Trade Session
int    closeALLhour          = 18;
int    marchGMT              = 85;       // 25 March 2012 (DST start)
int    octoberGMT           = 302;      // 28 October 2012 (DST end)

bool TimeCondition()
{

if (DayOfYear() >= marchGMT && DayOfYear() <= octoberGMT){
   StartHour      = 7;
   EndHour        = 16;
   closeALLhour   = 17;
   }


/// this longer version is working correctly in backtest ///


int    StartHour             = 8;        // Start Hour of Trade Session 
int    EndHour               = 17;       // End Hour of Trade Session
int    closeALLhour          = 18;
int    marchGMT              = 85;       // 25 March 2012 (DST start)
int    octoberGMT           = 302;      // 28 October 2012 (DST end)

bool TimeCondition()
{
     
   if (DayOfYear() >= marchGMT){
   StartHour      = 7;
   EndHour        = 16;
   closeALLhour   = 17;
   }
   
   if (DayOfYear() >= octoberGMT){
   StartHour      = 8;
   EndHour        = 17;
   closeALLhour   = 18;
   }
 

What about this:

int marchGMT = 85, octoberGMT = 302;
int StartHour = 8, EndHour = 17, closeALLhour = 18;

if (DayOfYear() >= marchGMT && DayOfYear() <= octoberGMT) {
   StartHour = 7;
   EndHour = 16;
   closeALLhour = 17;
}

This (untested) code assumes GMT and adjusts when in DST. 

 
https://www.mql5.com/en/forum/127483 reports DayOfWeek() always returns 5 in tester.
try using TimeDayOfWeek(Time[0]))
 
WHRoeder:
https://www.mql5.com/en/forum/127483 reports DayOfWeek() always returns 5 in tester.
try using TimeDayOfWeek(Time[0]))

I just ran a real small test (see below), and DayOfWeek() did not always return 5 in the tester.

DayOfWeek() in tester 

Maybe that limitation has been fixed. 

EDIT #1:

The following code:

int day_of_week = 0;

int start() {
   if (day_of_week != DayOfWeek()) {
      Print ("DayOfWeek is ", DayOfWeek());
      day_of_week = DayOfWeek();
   }
   return(0);
}

 produced the following output in the tester:

DayOfWeek() test #2 in tester 

 
getting:

I'm trying to merge the DST gmt filter rules(using DayOfYear filter), but can't get it to work; this is my coding:

link to "Day of Year" calendar: http://www.soils.wisc.edu/cgi-bin/asig/doyCal.rb

What about using something similar to this:

int DST_offset = 0;
if (Month() > 3 && Month() < 10)
   DST_offset = 1;
else if (Month() == 3 && Day() - DayOfWeek() >= 25) {
   DST_offset = 1;
}
else if (Month() == 10 && Day() - DayOfWeek() < 25) {
   DST_offset = 1;
}

int StartHour = 8 - DST_offset, 
    EndHour = 17 - DST_offset, 
    closeALLhour = 18 - DST_offset;

 This code determines European DST without relying on DayOfYear() and makes the adjustments to your times (StartHour, EndHour, and closeALLhour).

 

Thirteen & WH - appreciate your help, but since each year DST is different would be simpler for me to stick with DayofYear.   I'm no expert at coding; business major here, but I have this mindset of smaller or shorter is always better :)  And I only have to adjust those March and Oct day once a year, plus during backtesting I can manually adjust it.

DST UK starting on 31 March 2013...for those using standard GMT.

UK DST: http://www.timeanddate.com/worldclock/timezone.html?n=136

WHRoeder: I wish https://www.mql5.com/en/forum/134641/page2 I could understand your example better, but I just couldn't comprehend it, If you don't mind or have time could you minimized it down to a fewer lines if it still keep the intended logic.

much appreciated.

 

https://www.mql5.com/en/forum/109854

see logic inside of this

i have use it also inside

News events and market times on your chart  

Every market has its own timezone not all regios have DST

your broker can use also DST time or doesn't

and your pc has also its time with your pc you can get GMT time

if your broker is changing time same period as the period you wanna trade then it might be you don't have to change 

 
getting:

I'm no expert at coding...

I understand...I'm not an expert at coding yet either, but I'm working toward that goal (slowly, it seems). :)  There are many others here that are light years ahead of me, but I learn from watching and experimenting (and asking questions).

... since each year DST is different would be simpler for me to stick with DayofYear.   And I only have to adjust those March and Oct day once a year, plus during backtesting I can manually adjust it.

Why not use code that you don't have to change each year?  While the date DST begins/ends changes from one year to the next, it occurs on the same day--European DST begins on the last Sunday of March and ends on the last Sunday of October.  If you encode the rule, then it should work from year to year without change (until/unless the rules changes).  The code I posted above encodes the rule.  The code has four parts.

First:

int DST_offset = 0;

This code block assumes DST has not begun yet.  This will be true for any day in November, December, January, and February.

Second:

if (Month() > 3 && Month() < 10)
   DST_offset = 1;

This code looks at the current month, and if the month is April, May, June, July, August, or September, DST has already begun.  Accordingly, DST_offset is set to 1.

Third:

else if (Month() == 3 && Day() - DayOfWeek() >= 25) {
   DST_offset = 1;
}

This code determines whether the month is March, and if so, determines whether the day is, or is after, the last Sunday.  This is important because (a) any day before the last Sunday is not DST and (b) the last Sunday and all the rest of the days in March are DST.  Since March has 31 days, the date of the last Sunday in March cannot be less than 25.  Using DayOfWeek(), the code able to determine if the day is, or is after, the last Sunday of March.  If it is the last Sunday of March or it is after the last Sunday, DST_offset is set to 1.

Fourth:

else if (Month() == 10 && Day() - DayOfWeek() < 25) {
   DST_offset = 1;
}

This code determines whether the month is October, and if so, determines whether the day is before the last Sunday.  Any day in October that is before the last Sunday is DST, and the last Sunday and all the days thereafter in October are not DST.  This calculation is very similar to the one applied in March--except that it determining if the day is before the last Sunday in October.  If the day is before the last Sunday of October, DST_offset is set to 1.

I hope this explanation helps you some. :)

 
Thirteen: March cannot be less than 25.  Using DayOfWeek(), the code able to determine if the day is, or is after, the last Sunday of March.  If it is the last Sunday of March or it is after the last Sunday, DST_offset is set to 1.

You're right!! I totally forgot about March can't be less than 25 ....thanks for pointing it out ;-)

Your method would have no negative effect on backtesting either...great!!!

Much appreciation on the step-by-step outline!, sure would be much beneficial for many members to come on this forum.

 

After reviewing my notes and doing a little more research, I found equations that will calculate (a) the day in March that European DST starts and (b) the day in October that European DST ends.  See here and Summer Time in Europe

The equation (in code) for March is:

int DST_start_day = 31 - MathMod((4 + MathFloor(5*Year()/4)), 7);

The equation (in code) for October is:

int DST_end_day = 31 - MathMod((1 + MathFloor(5*Year()/4)), 7);

The following code calculates the starting and ending days for European DST for 2000 to 2025 and prints them to the log:

int DST_start_day = 0, DST_end_day = 0;
for (int yr = 2000; yr <= 2025; yr++) {
   DST_start_day = 31 - MathMod((4 + MathFloor(5*yr/4)), 7);
   DST_end_day = 31 - MathMod((1 + MathFloor(5*yr/4)), 7);
   Print ("DST starts on March ", DST_start_day, ", ", yr, " and ends on October ", DST_end_day, ", ", yr);
}

and that code produces the following output:

European DST test #1 

I verified the above European DST start/end dates using www.timeanddate.com

Substituting the new equations into the code I posted yesterday produces the following (compiled but untested):

int DST_start_day = 31 - MathMod((4 + MathFloor(5*Year()/4)), 7);
int DST_end_day = 31 - MathMod((1 + MathFloor(5*Year()/4)), 7);

int DST_offset = 0;
if (Month() > 3 && Month() < 10)
   DST_offset = 1;
else if (Month() == 3 && Day() >= DST_start_day) {
   DST_offset = 1;
}
else if (Month() == 10 && Day() < DST_end_day) {
   DST_offset = 1;
}

int StartHour = 8 - DST_offset, 
    EndHour = 17 - DST_offset, 
    closeALLhour = 18 - DST_offset;
 

Here are the equations (in code) for American DST (for those who might be interested):

int DST_start_day = 0, DST_end_day = 0;   
if (Year() >= 1987 && Year() <= 2006) {
   DST_start_day = 1 + MathMod((2 + 6*Year() - Year()/4), 7);  // first Sunday in April
   DST_end_day = 31 - MathMod((1 + 5*Year()/4), 7);            // last Sunday in October
}
else if (Year() >= 2007) {
   DST_start_day = 14 - MathMod((1 + 5*Year()/4), 7);          // second Sunday in March
   DST_end_day = 7 - MathMod((1 + 5*Year()/4), 7);             // first Sunday in November
}

 I used those equations to produce the test code below:

int DST_start_day = 0, DST_end_day = 0, yr = 0;
for (yr = 1990; yr < 2007; yr++) {
   DST_start_day = 1 + MathMod((2 + 6*yr - yr/4), 7);
   DST_end_day = 31 - MathMod((1 + 5*yr/4), 7);
   Print ("DST starts on April ", DST_start_day, ", ", yr, " and ends on October ", DST_end_day, ", ", yr);
}
for (yr = 2007; yr <= 2020; yr++) {
   DST_start_day = 14 - MathMod((1 + 5*yr/4), 7);
   DST_end_day = 7 - MathMod((1 + 5*yr/4), 7);
   Print ("DST starts on March ", DST_start_day, ", ", yr, " and ends on November ", DST_end_day, ", ", yr);
}

 and that code produced the output below:

US DST test #1 

I verified the above start/end dates for American DST using www.timeanddate.com

Reason: