Edgoro
Compiler warning · Scope

Pine Script warning: Shadowing variable in parent scope

The Pine Script shadowing warning means = inside a block created a new variable instead of updating the outer one. Use := so the change survives the block.

Pine Editor console · line 5
Shadowing variable "stopLevel" which exists in parent scope. Did you want to use the ":=" operator instead of "=" ?

Cause

Inside the if block, stopLevel = low uses =, which declares a new local variable with the same name. The outer stopLevel is never updated, so the plot stays empty.

Broken code

This script compiles, but TradingView shows the warning on line 5.

Pine Script v6
//@version=6
indicator("Stop level", overlay = true)
var float stopLevel = na
if ta.crossover(close, ta.sma(close, 20))
    stopLevel = low
plot(stopLevel, "Stop", color.red, style = plot.style_linebr)

Fixed code

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

Pine Script v6
//@version=6
indicator("Stop level", overlay = true)
var float stopLevel = na
if ta.crossover(close, ta.sma(close, 20))
    stopLevel := low
plot(stopLevel, "Stop", color.red, style = plot.style_linebr)

Why it works

:= assigns a new value to the existing variable instead of declaring another one. Because stopLevel is declared with var, it keeps its value from bar to bar, so the plot shows the low of the bar where the last crossover happened.

When the warning is harmless

If you really wanted a separate variable that exists only inside the block, give it a different name. The warning disappears and the code says what it does. Reusing the outer name is almost always a missing :.

Questions

What is the difference between = and := in Pine Script?

= declares a new variable; := assigns a new value to a variable that already exists. Inside a local block, = creates a separate variable that disappears at the end of the block.

Can I ignore the shadowing warning?

Only if you really meant a separate local variable. In that case, rename it: the warning goes away and the code is clearer.

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.