'Create a new label when the condition is met

In this code the high and low label are updated each time a new bar is opened. I would like the High and Low labels between the 10 candles to be updated only when the last bar crosses the high or low level of the ten candles, and delete old label.

Is it possible?

var lastLabel1 = label.new(bar_index-numOfCandles,high[numOfCandles],text="")
var lastLabel2 = label.new(bar_index-numOfCandles,high[numOfCandles],text="")
var lastLabel3 = label.new(bar_index-numOfCandles,high[numOfCandles],text="")
var lastLabel5 = label.new(bar_index-numOfCandles,high[numOfCandles],text="")



if barstate.islast == true

var HH = high[0]
var LL = low[0]
var HHcandleNum = 0
var LLcandleNum = 0

for i = 0 to numOfCandles
    if high[i] > high
        HH:=high[i],HHcandleNum := i
    if low[i] < LL
        LL:=low[i],LLcandleNum := i


if label.get_text(lastLabel1)!=""
    label.delete(lastLabel1)
if label.get_text(lastLabel2)!=""
    label.delete(lastLabel2)
if label.get_text(lastLabel3)!=""
    label.delete(lastLabel3)
if label.get_text(lastLabel5)!=""
    label.delete(lastLabel5)


lastLabel1 := label.new(bar_index-numOfCandles,high[numOfCandles],text="▩", textcolor=color.yellow,style=label.style_none,size=size.normal) // print the start of the lookback period
lastLabel2 := label.new(bar_index-HHcandleNum,high[HHcandleNum],textcolor=color.yellow,style=label.style_label_down,color= Colordella10,size=size.small,text="High") // print the HH
lastLabel3 := label.new(bar_index-LLcandleNum,low[LLcandleNum],textcolor=color.yellow,style=label.style_label_up,color= Colordella10,size=size.small,text="Low") // print the LL
int num = abs(HHcandleNum-LLcandleNum)[0]


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source