xarc
xarc

Reputation: 225

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

Upvotes: 0

Views: 396

Answers (2)

Oleg Shpagin
Oleg Shpagin

Reputation: 1

Try this:

//@version=5
indicator("Switch using an expression", "", true)

string maType = input.string("EMA", "MA type", options = ["EMA", "SMA", "RMA", "WMA"])
int maLength = input.int(10, "MA length", minval = 2)

float ma = switch maType
    "EMA" => ta.ema(close, maLength)
    "SMA" => ta.sma(close, maLength)
    "RMA" => ta.rma(close, maLength)
    "WMA" => ta.wma(close, maLength)
    =>
        runtime.error("No matching MA type found.")
        float(na)

plot(ma)

Upvotes: 0

vitruvius
vitruvius

Reputation: 21342

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

Upvotes: 1

Related Questions