Pine Script error: Undeclared identifier (causes and how to fix)
Fix "Undeclared identifier" in Pine Script: declare the variable at global scope, check spelling and case, and update old colour names such as red.
"Undeclared identifier '<name>'" means the script uses a variable name that Pine cannot see at that point. Usually the variable was created inside an if or for block and then used after the block ended. The fix is to declare it once at global scope, for example float up = na, and give it its value inside the block with :=.
What causes it
The name in quotes is the one Pine could not match to anything declared before that line. Four mistakes account for most cases.
Declared inside a block, used outside it
A variable created inside an if belongs to that block and is gone once the block ends. This code FAILS on TradingView:
//@version=6
indicator("Undeclared")
if close > open
up = close - open
plot(up)
up exists only on line 4, inside the if. On line 5 the plot() asks for up, and Pine does not know it. The same happens with a variable first declared inside a for loop.
A misspelling or the wrong case
Pine is case-sensitive. Close, CLOSE and close are three different names, and only the last one is built in. A variable you declared as fastEma cannot be read back as fastema. These FAIL on TradingView:
plot(Close)
plot(fastema)
Used before it is declared
Scripts run from top to bottom. If line 4 reads a variable that line 6 declares, line 4 fails, even though the name exists further down. Move the declaration above the first line that uses it.
Old colour names
Older scripts wrote colours as bare words such as red or green. Current Pine needs the color. prefix: color.red, color.green. A bare red FAILS on TradingView with this error. The fixed form:
//@version=6
indicator("Colour by direction", overlay = true)
c = close > open ? color.green : color.red
plot(close, "Close", c)
How to fix it
For the block case, declare the variable at the top level first and only assign it inside the block. This is the tested fix for the first example:
//@version=6
indicator("Declared first")
float up = na
if close > open
up := close - open
plot(up, "Up range", color.green, style = plot.style_columns)
Look at the two operators. float up = na on line 3 declares the variable at global scope, with na as its value until something changes it. On line 5, up := close - open gives that same variable a new value on green bars. Because up now lives at the top level, plot(up) on line 6 can see it.
The same pattern works for several values set in one block:
//@version=6
indicator("Body size")
float body = na
float wick = na
if close > open
body := close - open
wick := high - close
plot(body, "Body", color.green)
plot(wick, "Upper wick", color.gray)
For spelling and case, copy the name from its declaration rather than retyping it. For order problems, move the declaration up. For colours, add color. in front.
What SuperCharting's Script Editor shows
SuperCharting stops on this mistake. With the first broken example, the error appears when the script runs, which happens when you press Add to chart. The console shows the line number:
Runtime: line 5, bar 0: undeclared identifier 'up'
That points straight at the plot(up) line. The variable it names is the one to move up to global scope.
In our test the built-in Auto-fix did not repair this example, so plan to make the change yourself. On accounts where the site owner enables it, the deeper AI check inside Auto-fix can attempt repairs that simple rules cannot. Read the list of changes it makes before you use the script.
To check your version:
- Open the Script Editor with Ctrl + E.
- Paste the script into the code area.
- Read the console, then press Add to chart. Errors of this kind show in a line that starts with Runtime:.
The Pine Script code checker page explains the difference between the check while you type and the check on Add to chart. Write your first indicator builds a script step by step with variables declared in the right place.
Related errors
This error often appears right after you fix cannot use 'plot' in local scope: the plot moves out of the if, and the variable it reads is left inside. If the name in the message is a function, such as sma, you are looking at could not find function or function reference instead. And if you try to declare the variable globally and change it from inside a function you wrote, see cannot modify global variable in function.
Frequently asked questions
Why does my variable work inside the if block but not after it?
A variable declared inside an if or for block is not visible after the block. Declare it before the block, at the left margin, and assign it inside with :=.
What is the difference between = and := in Pine Script?
= declares a new variable. := gives a new value to a variable that already exists. Inside a block you usually want :=, so that you change the global variable instead of creating a local one.
Is Pine Script case-sensitive?
Yes. close is the built-in price, while Close is an unknown name that gives this error.
Why does red give an undeclared identifier error?
Bare colour names come from old versions. Use color.red and the other color. names.
TradingView and Pine Script are trademarks of TradingView, Inc. SuperCharting is independent and not affiliated with it.