I have trouble deleting/removing boxes from a box array when the price crosses into the box. I am printing a box on the bar as bullish engulfing occurs and it must be deleted when the price cross into the box again. Currently, I am getting an error exclamation mark that says "Array index out of bounds".
How do I delete these specific boxes?
Here is my code:
study("Test Box", overlay = true)
var float[] boxTop = array.new_float()
var box[] boxes = array.new_box()
bullish_Engulfing = (open[1] > close[1]) and (open < close) and (open <= close[1]) and (close > open[1]) and (high >= high[1])
if (bullish_Engulfing)
array.push(boxes, box.new(bar_index[1], high[1], bar_index[1], low[1], color.black, 1, line.style_solid, extend.right, xloc.bar_index, color.new(color.blue, 80)))
array.push(boxTop, high[1])
if (array.size(boxTop) != 0)
for i = 0 to array.size(boxTop)
float box_top_price = array.get(boxTop, i)
float actual_price = close[0]
if (actual_price < box_top_price)
array.remove(boxTop, i)
array.remove(boxes, i)
The array index is zero based so you have to iterate your for loop between 0 and array.size(boxTop) - 1
Also, you should iterate in reverse, starting at the last index to 0. As elements are removed the size of the array will shrink and the for loop will still generate an out of bounds error as it will now end up trying to reference an index that no longer exists.
for i = array.size(boxTop) - 1 to 0