Edgoro
Compile error · Scope

Pine Script error: Cannot use plot in local scope (fix)

plot() can't be called inside if, for or a function in Pine Script. Plot a conditional value with na instead. The same fix works for plotshape and bgcolor.

Pine Editor console · line 6
Cannot use "plot" in local scope

Cause

plot() and the other plot functions must be called at the top level of the script. They can't be placed inside an if, a loop or a function body (a local scope).

Broken code

TradingView reports the error on line 6.

Pine Script v6
//@version=6
indicator("EMA cross", overlay = true)
fast = ta.ema(close, 9)
slow = ta.ema(close, 21)
if ta.crossover(fast, slow)
    plot(fast, color = color.green)

Fixed code

The same script, corrected for Pine Script v6. Paste it into the Pine Editor and save.

Pine Script v6
//@version=6
indicator("EMA cross", overlay = true)
fast = ta.ema(close, 9)
slow = ta.ema(close, 21)
crossUp = ta.crossover(fast, slow)
plot(fast, "Fast EMA", color.teal)
plot(crossUp ? fast : na, "Cross", color.green, 4, plot.style_circles)

Why it works

Plots are set up when the script compiles, one per call, so their number and order are fixed. What each plot shows on each bar is controlled through its value or color: on bars where the condition is false, plot na and nothing is drawn. plot.style_circles shows isolated points, since a line can't connect values that exist on single bars.

The same fix for other functions

Inside a block (fails)At the top level (works)
plot(x)plot(cond ? x : na)
plotshape(...)plotshape(cond, ...)
bgcolor(color.red)bgcolor(cond ? color.new(color.red, 85) : na)
barcolor(color.orange)barcolor(cond ? color.orange : na)
alertcondition(...)alertcondition(cond, ...)
hline(50)hline(50) with a fixed or input level

The message names the function: Cannot use "hline" in local scope, Cannot use "indicator" in local scope and so on. Drawings (label.new(), line.new(), box.new()), alert() and strategy orders such as strategy.entry() are allowed inside blocks.

Questions

How do I plot only when a condition is true in Pine Script?

Put the condition in the value: plot(condition ? value : na). With plotshape(), pass the condition as the first argument. Nothing is drawn on bars where the value is na or the condition is false.

Can I call plot() inside a function?

No. Return the value from the function and plot the result at the top level of the script.

Which functions can be used inside an if block?

Calculations, assignments with :=, drawing functions such as label.new(), line.new() and box.new(), alert(), and strategy order functions such as strategy.entry(). The plot family, alertcondition(), hline() and the indicator() or strategy() declaration must stay at the top level.

The scripts on this page are minimal examples written to show the error and its fix, not trading systems. Trading involves substantial risk of loss and is not suitable for every investor. Edgoro is a software tool, not a broker or financial adviser: nothing it produces is investment advice, and past or backtested performance does not guarantee future results.

Still getting an error?

Paste your script and the message into the free Pine Script error fixer: it explains what went wrong and returns a corrected v6 version. Or describe the indicator in plain English and let Edgoro write the code.