Edgoro
Compile error · Syntax and indentation

Pine Script error: Undeclared identifier (fix)

What "Undeclared identifier" means in Pine Script, the usual causes (typos, scope, old v4 names) and how to fix each one in v6.

Pine Editor console · line 4
Undeclared identifier "lenght"

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.

Pine Script v6
//@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.

Pine Script v6
//@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 an if, 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: tickerid is now syminfo.tickerid, period is timeframe.period, and input.integer gave way to input.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.

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.