Help with iBarShift

 

Hi. I was hoping someone could help me to understand iBarShift as it should do exactly what I'm looking for based on the documentation, but it's actual results are not what I'm expecting.

In particular, I had understood by setting the parameter exact to true that the given time must line up to the open of a candle, rather than just somewhere within it. Have I misunderstood iBarShift?

Here is a very simple example to demonstrate.

    PrintFormat("Time now? %s", TimeToString(TimeCurrent()));
    PrintFormat("iBarShift for 08:40 on M1? %d", iBarShift(Symbol(), PERIOD_M1, D'2023.10.22 08:40', true));
    PrintFormat("iBarShift for 08:40 on M2? %d", iBarShift(Symbol(), PERIOD_M2, D'2023.10.22 08:40', true));
    PrintFormat("iBarShift for 08:41 on M2? %d", iBarShift(Symbol(), PERIOD_M2, D'2023.10.22 08:41', true));

Running this, I get the following result.

2023.10.22 16:49:30.306 LQv2 (BTCUSD.a,M2)      Time now? 2023.10.22 08:49
2023.10.22 16:49:30.306 LQv2 (BTCUSD.a,M2)      iBarShift for 08:40 on M1? 9
2023.10.22 16:49:30.306 LQv2 (BTCUSD.a,M2)      iBarShift for 08:40 on M2? 4
2023.10.22 16:49:30.306 LQv2 (BTCUSD.a,M2)      iBarShift for 08:41 on M2? 4

Now, the first two results (iBarShift returning 9 for the M1 chart and 4 for the M2 chart) are what I'd expect; as 08:40 is the open time for a candle on both of these charts (at the time, the shifts were 9 and 4 respectively).

However, it's the final result that I don't understand. I've asked for the shift for the candle at 08:41 on the M2 chart. No candle on this start opens at 08:41. Because of this, I thought iBarShift would return -1 as I had set the exact parameter to true.

Can someone please explain why I'm not getting -1, or how I've misunderstood iBarShift?

Thanks very much!

 
Bill M: No candle on this start opens at 08:41. Because of this, I thought iBarShift would return -1 as I had set the exact parameter to true.

M2 8:40 bar includes [8:40:00 – 8:41:59]

[in]  A return value, in case the bar with the specified time is not found.

There was a bar with that specified time.
Reason: