Help with H4 timeframe calculations

 

Ok, this is from pivot indicator, problem is that it does not see the start of the day from H1 timeframe. M1,M5,M15,M30,H1 are good.

In H4 and Daily charts it does not pick up the first,last bar of the day. Can someone please help with that?

// let's find out which hour bars make today and yesterday
ComputeDayIndices(LocalTimeZone, DestTimeZone, idxfirstbaroftoday, idxfirstbarofyesterday, idxlastbarofyesterday);

startofday= Time[idxfirstbaroftoday]; // datetime (x-value) for labes on horizontal bars
startofyesterday= Time[idxfirstbarofyesterday]; // datetime (x-value) for labes on horizontal bars



//
// walk forward through yestday's start and collect high/lows within the same day
//
yesterday_high= -99999; // not high enough to remain alltime high
yesterday_low= +99999; // not low enough to remain alltime low

for (int idxbar= idxfirstbarofyesterday; idxbar>=idxlastbarofyesterday; idxbar--) {

if (yesterday_open==0) // grab first value for open
yesterday_open= Open[idxbar];

yesterday_high= MathMax(High[idxbar], yesterday_high);
yesterday_low= MathMin(Low[idxbar], yesterday_low);

// overwrite close in loop until we leave with the last iteration's value
yesterday_close= Close[idxbar];
}



//
// walk forward through today and collect high/lows within the same day
//
today_open= Open[idxfirstbaroftoday]; // should be open of today start trading hour

today_high= -99999; // not high enough to remain alltime high
today_low= +99999; // not low enough to remain alltime low
for (int j= idxfirstbaroftoday; j>=0; j--) {
today_high= MathMax(today_high, High[j]);
today_low= MathMin(today_low, Low[j]);
}

////////

void ComputeDayIndices(int tzlocal, int tzdest, int &idxfirstbaroftoday, int &idxfirstbarofyesterday, int &idxlastbarofyesterday)

{
int tzdiff= tzlocal - tzdest,
tzdiffsec= tzdiff*3600,
dayminutes= 24 * 60,
barsperday= dayminutes/Period();

int dayofweektoday= TimeDayOfWeek(Time[0] - tzdiffsec), // what day is today in the dest timezone?
dayofweektofind= -1;

//
// due to gaps in the data, and shift of time around weekends (due
// to time zone) it is not as easy as to just look back for a bar
// with 00:00 time
//

idxfirstbaroftoday= 0;
idxfirstbarofyesterday= 0;
idxlastbarofyesterday= 0;

switch (dayofweektoday) {
case 6: // sat
case 0: // sun
case 1: // mon
dayofweektofind= 5; // yesterday in terms of trading was previous friday
break;

default:
dayofweektofind= dayofweektoday -1;
break;
}

if (DebugLogger) {
Print("Dayofweektoday= ", dayofweektoday);
Print("Dayofweekyesterday= ", dayofweektofind);
}


// search backwards for the last occrrence (backwards) of the day today (today's first bar)
for (int i=1; i<=barsperday+1; i++) {
datetime timet= Time[i] - tzdiffsec;
if (TimeDayOfWeek(timet)!=dayofweektoday) {
idxfirstbaroftoday= i-1;
break;
}
}


// search backwards for the first occrrence (backwards) of the weekday we are looking for (yesterday's last bar)
for (int j= 0; j<=2*barsperday+1; j++) {
datetime timey= Time[i+j] - tzdiffsec;
if (TimeDayOfWeek(timey)==dayofweektofind) { // ignore saturdays (a Sa may happen due to TZ conversion)
idxlastbarofyesterday= i+j;
break;
}
}


// search backwards for the first occurrence of weekday before yesterday (to determine yesterday's first bar)
for (j= 1; j<=barsperday; j++) {
datetime timey2= Time[idxlastbarofyesterday+j] - tzdiffsec;
if (TimeDayOfWeek(timey2)!=dayofweektofind) { // ignore saturdays (a Sa may happen due to TZ conversion)
idxfirstbarofyesterday= idxlastbarofyesterday+j-1;
break;
}
}
Files: