Search code examples
pine-scriptpine-script-v5indicator

pine script - modifying right side of rectangle doesn't work


this pine script code suppose to draw a rectangle everyday at 00:00 and at 01:00 change the right side position of rectangle to the bar index at 1:00 . but modifying the right side only work immediately after creating the box not even one bar after. so my question is how can i do this?

//@version=5
indicator("rectangle", overlay=false)

box box = na
int startindex=0
startindex := (hour== 0 and minute == 0) ? bar_index : startindex[1]

if(hour==0 and minute==0)    
    box := box.new(startindex,
                    2,
                    bar_index, 
                    4,
                    bgcolor=color.new(color.green, 95), 
                    border_width=2,
                    border_color = color.black,
                    border_style=line.style_solid,
                    text = "the rectangle",
                    text_color = color.white,
                    text_size = size.normal)

if(hour==1 and minute == 0)
    box.set_right(box,bar_index)

the second if statement triggered but modification of box doesn't work.


Solution

  • You need to use the var keyword on your box declaration. Otherwise, you will lose your reference.

    var box box = na
    

    enter image description here