Hi,
I’m so confused over the meaning of (BarIndex = 0)?
Can we rewrite the code below without having BarIndex?
Thanks!
IF BarIndex = 0 THEN
XClose = Open
XOpen = (Open + Close) / 2
ELSE
XClose = Open
XOpen = (Open + Close + high + low) / 4
ENDIF
No, BarIndex is needed.
BarIndex is the very first bar when you load an indicator or a strategy. In your case you need it to start the HA sequence.
If you have instructions to be executed only when your code is loaded and not the next candles, well… BarIndex is what you need.
IntraDayBarIndex is another constant that returns 0 when it’s the first intraday bar of the day.
IntraDayBarIndex restarts from zero each new day, while BarIndex starts from 0 when you first execute your code and ALWAYS increases.
BarIndex starts from 0 when you first execute your code
Is above true even allowing for preload bars (default preload bars = 2000 in a strategy?) or would we need to use defparam preloadbars = 0 in a strategy?
Yes, it’s true no matter how many PRELOADBARS. If PreLOadBars > 0 then BARINDEX 0 will be that very first preloaded bar.
If you create a simple indicator:
RETURN BarIndex
and go back to the very first displayed bar on the chart, you’ll see what’s its BARINDEX value.