Hello
I have been struggling with BARINDEX and its interpretation, as far as I am aware it will tell me how many bars are loaded since the beginning of an automated strategy so the very first bar in a strategy should be 0.
I have a piece of code below where I would like to enter the position under certain circumstances and when BARINDEX = 0. But it never enters the initial position so the automated strategy wont get going.
Am I misunderstanding BARINDEX or is the code below wrong?
ARC = 3* averagetruerange[7](close)
IF BARINDEX = 0 THEN
inithigh = highest[7](close)
initlow = lowest[7](close)
STARB1 = inithigh - ARC[0]
STARS1 = initlow + ARC[0]
IF close[0] > STARB1 then
buy 3 contracts at market
ELSIF close[0] < STARS1 then
sellshort 3 contracts at market
ENDIF
ENDIF
The online prorealtime documentation of the site :
BARINDEX : Return the number of bars since the beginning of data loaded (in a chart in the case of a ProBuilder indicator of for a trading system in the case of ProBacktest or ProOrder)
In your code, if barindex=0, there are no way to calculate 7 previous bar of the very first one 🙂
Hello Nicolas
Thanks but you have lost me.
Regards
Ian
In your first condition you are trying to find the highest and lowest values from the 7 previous periods, but it’s not possible since you are trying to do this at the first bar (barindex=0). So you need to wait until you are at least at the 7th bar of loaded history to find these values.
Thanks Nicolas
Makes perfect sense, I did not see the woods for the trees.
Regards
Ian