detecting new trading week

 

This is my try to solve the problem of broker timezone differences.
I'm trying to find the Hour of Day, based on the real forex market timings, independent of any time settings (Client, Broker, or anything related to time zones and DST).
So hour Zero is exactly when the forex market gets open (which is based on NY time 8:00 AM) and hour 119 is the last hour market is open, and @ 119:59:59+1second market gets closed.
and I need it to work in backtest.

This method seemingly works when I test on charts of my broker, But it assumes that the timeseries and chart bars are formatted as such:

  • the market is forex with 5 days being open, 2 days being closed, and the broker following same pattern/schedule for providing bars data.
  • except for the 2 days closure, there's no time-gap between daily bars formation. (well not more than 47 hours !)
  • this means there will be 120 H1 bars and 5 D1 bars in each trading week.


datetime WEEK_START_TIME = 0;   // global variable

void OnTick()
{
        static bool first_tick = true;
        static datetime last_D1_time = 0;

        if(first_tick)  // search for the most recent D1 after a 47 hour or more time-gap
        {
                datetime DTimes[8];             // Daily bars open times array
                if(CopyTime(_Symbol, PERIOD_D1, 0, 8, DTimes) != 8) return;
                for(int d=7; d>0; d--) if(DTimes[d]-DTimes[d-1] > 47*3600) WEEK_START_TIME = DTimes[d]; // 47 hours time-gap found
                last_D1_time = DTimes[7];
                first_tick=false;
        }
        
        datetime current_D1_time = iTime(_Symbol, PERIOD_D1, 0);
        if(current_D1_time>last_D1_time)        // new daily bar, check if it's start of a new week
        {
                if(current_D1_time - last_D1 > 47*3600) WEEK_START_TIME = current_D1_time;
                last_D1_time = current_D1_time;
        }

        //... rest of OnTick...

}

// When needed to get the current hour info based on forex market time:
datetime H1OT = iTime(_Symbol, PERIOD_H1, 0);                   // current H1 bar Open Time
int HourOfFxDay = ((H1OT - WEEK_START_TIME) % 86400) / 3600;    // in range 0 to 23
int HourOfFxWeek = (H1OT - WEEK_START_TIME) / 3600;             // in range 0 to 119


Have I assumed too much ? what else can I do to make sure this procedure doesn't return erroneous results?
I feel it's not robust enough for different situations, but don't know what checkups makes it less prone to failure.

 

Sorry, here updating the code above.
the timegap between D1 bar open times, one before weekend, and one after market opening and start of new trading week, is actually 72 hours.
So the 47 hour above should be changed to 71 hours. much better.

Plus, the for loop in the first tick block, should be breaked if the most recent week start day is found.

so :

for(int d=7; d>0; d--) if(DTimes[d]-DTimes[d-1] > 71*3600) { WEEK_START_TIME = DTimes[d]; break; }  // 71 hours time-gap found

and

if(current_D1_time - last_D1 > 71*3600) WEEK_START_TIME = current_D1_time;
Reason: