plotpine-scriptpine-script-v5tradingtechnical-indicator

Check if a plot is displayed using pinescript


I have an indicator with 2 moving averages lines, and for each MA line I put a label with the price corresponding to that MA, the problem is the following:

I want to hide a MA line and it's price label, from the configuration window it hides the MA line, but It does not hide the label with the price.

Is there a way to check if the moving average is plotted or not using pinescript?

Is possible to know the value of that checkbox from the image corresponding to the MA red line?

Thanks in advance!

enter image description here


Solution

  • You cannot check that.

    As a workaround, you can set you plot's editable property to false and use a user input instead to enable/disable the plot. This way you can also display your label or not based on the user input.

    //@version=5
    indicator("My script", overlay=true)
    
    plot_sma = input.bool(true, "Plot SMA 20")
    
    sma_20 = ta.sma(close, 20)
    plot(sma_20, "SMA 20", color.white, editable=false, display=plot_sma ? display.all : display.none)
    
    if (plot_sma)
        lbl = label.new(bar_index + 1, high, str.tostring(sma_20, format.mintick), color=color.new(color.white, 100), textcolor=color.white)
        label.delete(lbl[1])
    

    enter image description here