I want to create a box around the last 5 bars. I only want to draw one box on the chart.
I have written this code, but it doesnt work.
// @version=5
indicator("One Box", overlay=true)
var int left = na
if barstate.islast
left := bar_index-5
top = ta.highest(5)
bottom = ta.lowest(5)
box.new(left= left ,top= top, right= bar_index, bottom= bottom)
Many thanks
After you reate your box, you need to delete the previous instance. No need for any if check.
top = ta.highest(5)
bottom = ta.lowest(5)
bx = box.new(left=bar_index - 4, top=top, right=bar_index, bottom=bottom)
box.delete(bx[1])
Or declare your box with the var
keyword and just move it on every bar. This will be more performance efficient.
top = ta.highest(5)
bottom = ta.lowest(5)
var bx = box.new(na, na, na, na)
box.set_lefttop(bx, bar_index - 4, top)
box.set_rightbottom(bx, bar_index, bottom)