4

I am trying to write a simple if-then-else statement using the Pine language under Tradingview. What the code does is based upon user input.

If the box is checked, the plot the line.

If the box is not checked do not plot the line.

This is the code I have:

notPlot = -2000
var ch382= input(true, ".382")
if ch382
    plot( ch382? bottom + diff * .382:   noPlot, title="fib-.236", linewidth=3, color=color.orange )

How can I write this in a proper way?

If I try to run it, I get: “cannot use 'plot' in a local scope”

Any assistance would be greatly appreciated.

ETA: I found this thread below

How to put plot statement inside if statement

  • but -

what I need to do is to plot if the box is checked and ~not plot~ if the box is not checked.

Casey Harrils
  • 2,793
  • 12
  • 52
  • 93

1 Answers1

12

ETA: figured out the issue. One would use "na" (in the case of plotting) to note that the line should not be displayed - my mistake ...

var ch382 = input(true, ".382")
plot( ch382? bottom + diff * .382:  na, title="fib-.382", linewidth=3, color=color.orange )
Casey Harrils
  • 2,793
  • 12
  • 52
  • 93
  • Is it possible to remove na from indicator values? – Hemant Kaushik May 22 '21 at 08:38
  • 5
    Same problem and as usual hit SO. Wasn't expecting a logical solution, this being Pinescript and all. But this one really made me laugh. So many pooches got screwed in the design of this trainwreck language. – 0b101010 Jul 21 '21 at 14:56