Detect automatically summer/winter time switch period between America and Europe? - page 2

 

snelle_moda:

Hi 

When I would live in the USA, you're correct with your statement and my pc time would automatically change to the new summer time at (10-03-2013). BUT!!! I live the the Netherlands. In Europe, the summer time will be adjusted on the last Sunday on March 31-03-2013, thats 2.5 weeks from now. So between 10-03-2013 till 31-03-2013 there is a 1 hour time shift. All events in the USA are one hour earlier in Europe. My model needs to check this.    


I see. All you need is US time (say NY time) from your computer. I read the code somewhere, but it require some Win API :(. 
 

I have carefully read this topic, interesting. But I still do not understand why you need to do this calculation.

Between these periods, there are 2/3 weeks in March and October that there is a time difference of 1 hour between Europe and America. My EA needs to detect this time change to adjust some trading time variables that are used for the event calendar (US: Jobless claims, PPI, CPI etc.).

As I understand, Economic calendar time is adjust to server time. If you have some trading time parameters in your EA, why don't adapt these parameters with server time of your broker ? Unless you want a universal solution for the distribution of your EA or I miss something.



 

I use my local computer time for the "date and time" variables because of convenience. Using the server time of the broker makes things overcomplicated because each broker has its own server time, I have 2 live accounts at two different bokers and I use the demo simulation account of MetaQuotes. 

So I want a universal solution for this problem because there are only two periods in a year when this situation occurs, in the month march and october/november. For the other months, everything is working fine.

 
snelle_moda:

I use my local computer time for the "date and time" variables because of convenience. Using the server time of the broker makes things overcomplicated because each broker has its own server time, I have 2 live accounts at two different bokers and I use the demo simulation account of MetaQuotes. 

So I want a universal solution for this problem because there are only two periods in a year when this situation occurs, in the month march and october/november. For the other months, everything is working fine.

Thank you for explanation.

For an universal solution, I think what phi.nuts propose is better. I'll do a search and I'll let you know if I find something.

 

Hi snelle_moda,

I don't observe DST, but I coded something similar like DST (though I did not finished it, but still remember the logic), you know something like "Find date of last third Sunday November 2009"..

You just wait I'll try to code that again.

 

 
onewithzachy:

Hi snelle_moda,

I don't observe DST, but I coded something similar like DST (though I did not finished it, but still remember the logic), you know something like "Find date of last third Sunday November 2009"..

You just wait I'll try to code that again.

Like this ?  https://www.mql5.com/en/forum/11120#comment_449811
Detect automatically summer/winter time switch period between America and Europe?
Detect automatically summer/winter time switch period between America and Europe?
  • www.mql5.com
My EA needs to detect this time change to adjust some trading time variables that are used for the event calendar (US: Jobless claims, PPI, CPI etc.
 

Hi guy's.

 

I have finished the script for both periods (March and October/November).

Maybe there are a few small bugs left but the principle is clear. 

The script checks if the current date is between the 2e Sunday till the last Sunday of March or between the last Sunday of October and the first Sunday of November.

 

The script uses two loops: 

The first loop goes back in time to 01-01-20XX to check the first day of the year (day of the week number). It also count the number of sunday's that has elapsed  since the end of the month.

The second loop goes from 01-01-20XX to the present to count the number of sunday's that has elapsed since the start of the month.

  

I know the code is a little bit crappy but I'm a novice programmer. I think a good programmer can make this code more simple.

 

 

bool DateCheck(bool &SwitchTimePeriod,
               bool &FunctionDateCheck) export
   {

// Time/Date structure.
   MqlDateTime DatePeriod;        
   TimeGMT(DatePeriod);
          
// Check if the current date is in the time switch period.    
   if(DatePeriod.mon == 3 || DatePeriod.mon == 10 || DatePeriod.mon == 11) // In this example only march is now used.
         {

// Day of the year since 01-01-20XX, first day is 0.  
         int nDayOfYear = DatePeriod.day_of_year;  

         int nMonthStartNumber = 0; // Start day of the month.
         int nMonthEndNumber = 0;   // End day of the month.
            
// Find the current month.            
         if(DatePeriod.mon == 3) // March
               {
               nMonthStartNumber = 59;
               nMonthEndNumber = 90;
               } 
               else if(DatePeriod.mon == 10) // October
               {
               nMonthStartNumber = 273;
               nMonthEndNumber = 304;                  
               }     
               else if(DatePeriod.mon == 11) // November
               {
               nMonthStartNumber = 304;
               nMonthEndNumber = 334;                  
               }

// Set the week number of the current day.      
         int nDayNumberInThePast = DatePeriod.day_of_week;

// Counter for the number of Sunday's since the end of the month.            
         int nSundaySinceEndOfMonthCounter = 0; 
         
// Date confirmation boolean for March.         
         static bool LastSunDayDetection = false;                       

// Date confirmation boolean for October.            
         static bool bLastSundayOctober = true;
            
// Go back to the past (01-01-20XX + (five weeks) and check how many sunday's have passed since the end of the month (march)
// and check on which day 01-01-20XX has started. For 2013, this is on Tuesday.             
         for(int i = nDayOfYear + 35; i > 0; i--) // five addition weeks are added, to start the loop check beyond the end of the month.
               {

// Reset day number after one week has passed.           
               if(nDayNumberInThePast <= 0) 
                     {
                     nDayNumberInThePast = 6;                       
                     }
                     else
                     {
                     nDayNumberInThePast--;
                     }                 

//--------------------------------------------------------------------------------
// 1e: Check for the month March.            
// 2e: The check starts from the current date.
// 3e: The check starts from the end of the month.             
               if(DatePeriod.mon == 3 && i >= nDayOfYear && i <= nMonthEndNumber)
                     {                    

// Check if the day number in the past is equal to sunday.                        
                     if(nDayNumberInThePast == 0) // Sunday detected  
                           {
                           nSundaySinceEndOfMonthCounter++; 
                           }
                              
// Activate the boolean if the last Sunday of the month is detected.                          
                     if(nSundaySinceEndOfMonthCounter >= 1)
                           {
                           LastSunDayDetection = true;                                                         
                           }
                     }                  

//--------------------------------------------------------------------------------
// 1e: Check for the month October.            
// 2e: The check starts from the current date.
// 3e: The check starts from the end of the month.  
               if(DatePeriod.mon == 10 && i >= nDayOfYear && i <= nMonthEndNumber)
                     {
                     
// Check if the day number in the past is equal to sunday.                        
                     if(nDayNumberInThePast == 0) // Sunday detected  
                           {
                           nSundaySinceEndOfMonthCounter++; 
                           }

// Deactivate the boolean if the current date is before last Sunday of the month.                          
                     if(nSundaySinceEndOfMonthCounter >= 1)
                           {
                           bLastSundayOctober = false;                                                         
                           }                                         
                     }
               }
       
// Counter for the number of Sunday's since the start of the month.           
         int nSundaySinceStartOfMonthCounter = 0;  
         
// Date confirmation boolean for March.         
         static bool StartSunDayDetected = false;

// Date confirmation boolean for November. 
         static bool bFirstSundayNovember = true;  
            
// Go back to the present and check how many sunday's have passed since the start of the month.            
         for(int i = 0; i < nDayOfYear; i++)
               {

// Reset day number after one week has passed.           
               if(nDayNumberInThePast >= 6) 
                     {
                     nDayNumberInThePast = 0;
                     }
                     else
                     {
                     nDayNumberInThePast++;
                     }  

//---------------------------------------------------------------------
// 1e: Check for the month March.            
// 2e: Check runs till the current date.
// 3e: Check starts from the begin of the month. 
               if(DatePeriod.mon == 3 && i <= nDayOfYear && i > nMonthStartNumber)
                     {
                        
// Check if the day number in the past is equal to sunday.                        
                     if(nDayNumberInThePast == 0) // Sunday detected 
                           {
                           nSundaySinceStartOfMonthCounter++;
                           }

// Activate the boolean if the 2e Sunday of the month is detected.                   
                     if(nSundaySinceStartOfMonthCounter >= 2)
                           {
                           StartSunDayDetected = true;
                           }
                     }                      

//---------------------------------------------------------------------
// 1e: Check for the month November.            
// 2e: Check runs till the current date.
// 3e: Check starts from the begin of the month.                              
               if(DatePeriod.mon == 11 && i <= nDayOfYear && i > nMonthStartNumber)
                     {
                     
// Check if the day number in the past is equal to sunday.                        
                     if(nDayNumberInThePast == 0) // Sunday detected 
                           {
                           nSundaySinceStartOfMonthCounter++;
                           }                     

// Deactivate the boolean if the current date is after first Sunday of the month.                                                
                     if(nSundaySinceStartOfMonthCounter >= 1)
                           {
                           bFirstSundayNovember = false;                                                         
                           }                                          
                     }                             
               }    

// Check if both conditions for the time periods are valid.            
         if(DatePeriod.mon == 3) // Check March
               {
               if(StartSunDayDetected == true && LastSunDayDetection == true)
                     {
                     SwitchTimePeriod = true; // ADJUST THE TIME VARIABLES!!!!
                     }
               }
               else if(DatePeriod.mon == 10) // Check October
               { 
               if(bLastSundayOctober == true)
                     {
                     SwitchTimePeriod = true; // ADJUST THE TIME VARIABLES!!!!
                     }               
               }        
               else if(DatePeriod.mon == 11) // Check November
               { 
               if(bFirstSundayNovember == true)
                     {
                     SwitchTimePeriod = true; // ADJUST THE TIME VARIABLES!!!!
                     }              
               }                         
         }

// Function needs only to be calculated one time.
   FunctionDateCheck = true;
   return FunctionDateCheck;
   }     

void OnStart()
   {
  
   }
 
// Zeit-Differennz-Korrektur      
       int    Sommerzeit     =   87;  // DayOfYear Beginn Sommerzeit
       int    Winterzeit     =  297;  // DayOfYear Beginn Winterzeit

Here is an example, maybe that helps you further.

earlyBird1.mq4 - MQL4 Code Base

 

Hi

  

This method is also a possibility but you have to manually adjust the summer/winter time dates for each new year.

So this solution is not desirable.