Cause
The script uses a name the compiler doesn't know at that point: a typo, a variable declared further down or inside a local block, or a built-in name that no longer exists in Pine Script v6.
Broken code
TradingView reports the error on line 4.
//@version=6
indicator("SMA", overlay = true)
length = input.int(20, "Length")
plot(ta.sma(close, lenght))Fixed code
The same script, corrected for Pine Script v6. Paste it into the Pine Editor and save.
//@version=6
indicator("SMA", overlay = true)
length = input.int(20, "Length")
plot(ta.sma(close, length))Why it works
The variable is declared as length on line 3 but used as lenght on line 4. Pine Script names are case-sensitive and must match exactly, so once the spelling matches, the compiler finds the declaration.
The quoted name in the message is the one Pine couldn't find. Search the script for it: if it appears only once, it's a typo or a missing declaration.
Other ways to get this error
- Used before it's declared. Pine reads the script from top to bottom. A variable must be declared on a line above the first line that uses it.
- Declared inside a block. A variable declared with
=inside anif, a loop or a function exists only inside that block. A loop counter is the classic case:
for i = 0 to 9
total += close[i]
plot(i) // Undeclared identifier "i": the counter only exists inside the loop
Declare the variable at the top level and update it inside the block with :=.
- Old built-in names. Scripts written for v3 or v4 use names that moved into namespaces:
tickeridis nowsyminfo.tickerid,periodistimeframe.period, andinput.integergave way toinput.int(). Functions renamed the same way give Could not find function or function reference instead.
Questions
Why does Pine Script say undeclared identifier when the variable exists?
Usually because it is declared below the line that uses it, inside a local block (if, for or a function) that the line can't see, or with different capitalization. Pine names are case-sensitive: Length and length are two different identifiers.
Can the Pine Script version cause an undeclared identifier error?
Yes. Scripts written for v3 or v4 use names such as tickerid, period or input.integer that don't exist in v5 and v6. Replace them with syminfo.tickerid, timeframe.period and input.int(), and update the //@version line.
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.