Real built-inPine Script v6

Cannot use "bgcolor" in local scope

plot, bgcolor, plotshape, fill, hline and barcolor can only be called at global scope.

Quick answer

Pine Script's drawing calls — plot(), bgcolor(), plotshape(), plotchar(), fill(), hline() and barcolor() — must be called at global scope. They cannot appear inside an if block, a for loop, a switch, or a user-defined function, because Pine has to register every plot once when the script compiles, not conditionally per bar. The fix is not to move the condition out; it is to call the function unconditionally at global scope and make its VALUE conditional, using na or a transparent colour to hide it on bars where you do not want it.

The exact error

Cannot use "bgcolor" in local scope

v5 → v6

Applies to v4, v5 and v6 identically — this is a core language rule, not a version change. The error text names whichever function you called, so you may also see it as "Cannot use plot in local scope" or "Cannot use plotshape in local scope".

Why this happens

Pine compiles a fixed set of plots once, then evaluates your script bar by bar. A plot that only exists inside an if branch could not be registered at compile time, so the language forbids it outright rather than failing unpredictably at runtime.

This trips up almost everyone coming from a normal imperative language, where wrapping a draw call in an if is the obvious way to draw conditionally. In Pine the call is always made — you vary what it draws, not whether it runs.

For a value, use na: plot(condition ? close : na) draws nothing on bars where the condition is false, because Pine does not render na.

For a colour, use a fully transparent colour or na: bgcolor(condition ? color.new(color.green, 85) : na) shades only the bars you want.

The same rule is why you cannot put plot() inside a function you wrote. Return the value from the function and plot the result at global scope instead.

The working code

//@version=6
cannot-use-in-local-scope.pine
//@version=6
indicator("Conditional drawing done correctly", overlay = true)

isBull = close > open

// WRONG - the compiler rejects this:
// if isBull
//     bgcolor(color.new(color.green, 85))    // Cannot use "bgcolor" in local scope

// RIGHT - call it at global scope, make the VALUE conditional.
bgcolor(isBull ? color.new(color.green, 85) : na)

// Same idea for plot: na hides the line on bars you do not want.
plot(isBull ? close : na, "Bullish close", color = color.green, style = plot.style_linebr)

// Same idea for plotshape.
plotshape(isBull, "Bull bar", style = shape.triangleup, location = location.belowbar, color = color.green)

// If you need logic in a function, return a VALUE and plot it outside.
pickLevel(useHigh) =>
    useHigh ? high : low

plot(pickLevel(isBull), "Level", color = color.blue)

Copy and paste into the TradingView Pine Editor, or let TradePilot adapt it to your exact case.

FAQ

Cannot use "bgcolor" in local scope — common questions

What does "Cannot use bgcolor in local scope" mean in Pine Script?

It means you called bgcolor() inside an if block, a loop, or a function. Pine requires drawing calls to be made at global scope so every plot can be registered once at compile time. Call bgcolor() at the top level and pass a conditional colour, using na for bars you want left alone.

How do I plot something only when a condition is true?

Call plot() unconditionally and make the value conditional: plot(condition ? close : na). Pine draws nothing where the value is na, which gives you exactly the conditional plot you wanted without an if block.

Can I call plot() inside a function in Pine Script?

No. plot() and the other drawing calls are global-scope only, and a user-defined function body is local scope. Return the value from your function and call plot() on the result at global scope.

Does this rule apply to label.new and line.new too?

No — the drawing OBJECTS (label.new, line.new, box.new, table.new) can be created inside if blocks and loops. The restriction applies to the plotting FUNCTIONS: plot, plotshape, plotchar, bgcolor, barcolor, fill and hline.

More Pine reference

Other functions and errors

Never hit this error again.

TradePilot writes Pine Script v6 that's verified to compile before it reaches you — and if your existing script has this error, Pine Studio fixes it, root cause and all, in one pass. Start with 7 days free on any plan; cancel any time before it ends.

7-day free trial · Cancel anytime · No charge until day 7