iBarShift returning invalid value

 

I sometimes (thought rarely) receive "array out of range error" in the following code:

focusedIndex = iBarShift(NULL, 0, focusedDate);
if (focusedIndex>-1) {
   datetime realTime = Time[focusedIndex];
}

which leads me to a question, whether someone has explanation. Perhaps Time[focusedIndex] and iBarShift() work with different chart buffers, and may go out of sync, who knows.

And perhaps iTime() is the only one consistent with the iBarShift(). If there is a difference between Time[] and iTime(), I may be in need of large fixes throughout my entire code. 

 
The only case that I can think of your code returning the error would be if it was altered so that iBarShift calculates with a different timeframe.
 

Not sure if I got your idea properly - what have been altered?

I forgot to mention, that the code was served from the OnChartEvent() function, which might be the weak point.

 

I meant if 

iBarShift(NULL, 0, focusedDate)

 was altered to

iBarShift(NULL, PERIOD_H4, focusedDate)

or any other timeframe where the returned shift is larger than the Bars on the current chart 

 
I see... no, it is not related to this problem, in my case it appears on M1 every time. More likely it is because the i-functions are using different data sets than the arrays (snapshots from different times). I just like to avoid problems caused by mixing them, but any information about data management is lacking.
 
Ovo:
More likely it is because the i-functions are using different data sets than the arrays (snapshots from different times).
While I can't offer you any insight, I did have strong suspicions of this when exploring the problem I posted about the other day (connected and history updated)... results were inconclusive, but it seemed iTime() took longer to update than Time[] even when passing the current chart period to iTime().
Reason: