So usually it behaves as it expected: at 10 AM, with timeOffset I get the previous day's 5AM bar, which is correct. However at mondays, as you see in the last two lines:
I checked at candle 2024-06-03 10:00 AM
Expected result: 2024-05-31 05:00 AM
Result I got: 2024-05-31 21:45 AM
(If you want to double-check, please keep in mind that it's 2024 data. So in 2025, 06-03 is not monday, but in 2024 it is.)
Do you have any idea why is this wrong? Is it a bug, or I am missing something?
At monday (red zone) I am checking the previous day's 5AM bar.
Surprisingly there is a trading day between monday and friday: sunday evening (orange zone), just a few bars. (Of course it's not that big surprise, it's because of the timezone shift.)
...but there are no bars in sunday at 5 AM.
...so i get the bar at friday 19:45.
Please help me understand the rules of the timeOffset behavior. Why I got that bar exactly? It's not even the last bar of friday. As I see I need to make a custom search cycle to find the previous trading day's 5AM bar. It's OK. But then why do we have the built-in timeOffset function, if we can't use on mondays? Or how should I use it correctly?
int getBarFromPreviousDay(int h, int m, int tz = UTC){
const int currentDay = day(0);
for (int i = 1; i <= LookBack; i++) {
if (day(i) == currentDay) continue;
if (lhour(tz, i) == h && minute(i) == m) return i;
}
printf("No bar found for previous day at %02d:%02d within LookBack period.\n", h, m);
return -1;
}