Search code examples
pine-scriptpine-script-v5tradingview-api

Lines cannot be deleted


I have some lines and labels on my last candle indicating certain price levels. I tried to delete all labels and lines and then plot them again. But for some reason the same code works fine in case of the labels but the first line drawn on the chart stays there.

Code:

if barstate.islast or bar_index == 0
    while array.size(label.all) > 0
        label.delete(array.first(label.all))
        
    while array.size(line.all) > 0
        line.delete(array.first(line.all))

    if showPullingLines == true
        PullUpPriceLine := line.new(bar_index-2, PullUpPrice, bar_index, PullUpPrice, color=color.purple, width=1)
        lblPullUpPriceLine := label.new(bar_index-2, PullUpPrice, "Pull up price:\n" + str.tostring(Truncate2(PullUpPrice)), color = color.purple, textcolor = color.white,style = label.style_label_lower_right)
        PullDownPriceLine := line.new(bar_index-2, PullDownPrice, bar_index, PullDownPrice, color=color.purple, width=1)
        lblPullDownPriceLine := label.new(bar_index-2, PullDownPrice, "Pull down price:\n" + str.tostring(Truncate2(PullDownPrice)), color = color.purple, textcolor = color.white,style = label.style_label_upper_right)

See screenshot:

screenshot


Solution

  • Try this :

    a_allLines = line.all
    if array.size(a_allLines) > 0
        for i = 0 to array.size(a_allLines) - 1
            line.delete(array.get(a_allLines, i))