Compilation error

Fix `plot()` in local scope

Move `plot()` to global scope and make its series conditional. Pine must register plots during compilation, so calls inside `if`, loops, functions, or methods are rejected. Passing `close > open ? close : na` keeps the plot declaration global while hiding its value on bars that do not meet the condition. The same pattern can use a conditional color when the series should remain visible.

This link opens the Pine Script AI Coding Agent overview. It does not run or change code on this page.

Before and after

Make the smallest reviewable change

Both artifacts include the version declaration and the code needed to inspect the stated problem. The diff lists the lines that change between them.

Broken code.pine
//@version=6
indicator("Local plot demo", overlay = true)
if close > open
    plot(close)
Fixed code.pine
//@version=6
indicator("Local plot demo", overlay = true)
plot(close > open ? close : na)
Diff.pine
--- broken.pine
+++ fixed.pine
@@
-if close > open
-    plot(close)
+plot(close > open ? close : na)

Root causes

  • `plot()` is called inside an `if`, loop, function, or method scope.
  • A helper function tries to create plot visuals instead of returning a series to global scope.
  • Conditional visibility is implemented by conditionally calling `plot()` instead of using `na` for its series or color.

How the fix was verified

Method
Pineify UAT grammar checker via check-pine-script-grammar.mjs
Broken result
Returned a local-scope plot diagnostic at line 4, column 5.
Fixed result
Returned no grammar diagnostics.
Limit
The check proves valid placement and syntax. Confirm the intended gaps and plot style on a chart.

Bring the actual script, not just the message

Open the Coding Agent overview to paste editable Pine Script, explain the intended behavior, and request a review. Compile and test the result in TradingView before using it.

Paste your code to fix this error