pine-scriptpine-script-v6

Labels duplicated at every level


I found a Manual Fib indicator on the Web. The indicator however duplicate the labels at every Fib level. Can someone please help.

I tried label,delete but are to inexperienced to make it work.

My code is as follows

//@version=6
indicator(title="Manual Fibonacci Levels", shorttitle="Manual Fib", overlay=true)

// Inputs for manual high and low points
pHigh = input.price(title="High Price",defval = 0.0000, confirm = true) 
pLow = input.price(title="Low Price", defval = 0.0000,confirm = true)


// Inputs for Fibonacci levels to show
show_000  = input.bool(title="0.000" , defval=true)
show_236  = input.bool(title="0.236" , defval=true)
show_382  = input.bool(title="0.382" , defval=true)
show_500  = input.bool(title="0.500" , defval=true)
show_618  = input.bool(title="0.618" , defval=true)
show_786  = input.bool(title="0.786" , defval=true)
show_100  = input.bool(title="1.000" , defval=true)
show_1618 = input.bool(title="1.618" , defval=true)
show_2618 = input.bool(title="2.618" , defval=true)

// Fibonacci ratios
f_000  = 0.000
f_236  = 0.236
f_382  = 0.382
f_500  = 0.500
f_618  = 0.618
f_786  = 0.786
f_100  = 1.000
f_1618 = 1.618
f_2618 = 2.618

// Function to calculate and plot Fibonacci levels
plotFibLevel(level, show, color, levelName) =>
    if show
        fibLevelPrice = pLow + (pHigh - pLow) * level
        line.new(bar_index[+100], fibLevelPrice, bar_index,          fibLevelPrice,color=color, width=1)
    label.new(bar_index, fibLevelPrice, levelName + " =  " + str.tostring(fibLevelPrice, "###.####"), color=color, textcolor=color.black,   style=label.style_label_left)
   

// Calculate and plot Fibonacci levels
plotFibLevel(f_000,  show_000, color.rgb(253, 254, 255), "00.0%")
plotFibLevel(f_236,  show_236, color.rgb(253, 254, 255), "23.6%")
plotFibLevel(f_382,  show_382, color.rgb(253, 254, 255), "38.2%")
plotFibLevel(f_500,  show_500, color.rgb(253, 254, 255), "50.0%")
plotFibLevel(f_618,  show_618, color.rgb(245, 201, 6), "61.8%")
plotFibLevel(f_786,  show_786, color.rgb(253, 254, 255), "78.6%")
plotFibLevel(f_100,  show_100, color.rgb(253, 254, 255), "100.0%")
plotFibLevel(f_1618, show_1618, color.rgb(253, 254, 255), "161.8%")
plotFibLevel(f_2618, show_2618, color.rgb(253, 254, 255), "261.8%")

Solution

  • Your plotFibLevel() function can create one label and update it via setter functions on each bar. It is more efficient and looks like so:

    // Function to calculate and plot Fibonacci levels
    plotFibLevel(level, show, color, levelName) =>
        // Create the line and label once
        var _line  = line.new(na, na, na, na, color=color, width=1)
        var _label = label.new(na, na, color=color, textcolor=color.black, style=label.style_label_left)
        if show
            fibLevelPrice = pLow + (pHigh - pLow) * level
            // Update the line and label properties
            _line.set_xy1(bar_index[+100], fibLevelPrice)
            _line.set_xy2(bar_index, fibLevelPrice)
            _label.set_xy(bar_index, fibLevelPrice)
            _label.set_text(levelName + " =  " + str.tostring(fibLevelPrice, "###.####"))