The undeclared identifier error in Pine Script occurs when the compiler encounters a name—a variable, function, or built-in—that it cannot resolve to any declaration in the current scope. In plain terms, you used a word that Pine Script does not recognize as something that exists at that point in the script, so compilation stops before the script can run.
What "Undeclared Identifier" Actually Means
Pine Script is compiled before it executes. During compilation, the Pine Script compiler builds a symbol table: a record of every name that has been declared and is visible at each point in the code. When it meets a name that is not in that table, it raises Undeclared identifier and points to the line where the unknown name appears. The message is not saying the name is misspelled in a general sense—it is saying no declaration for that name is visible from that location.
This is different from a runtime error. The script never runs; the error appears in the Pine Editor console and the script cannot be added to a chart until it is fixed. That is why the fix is always about making the name resolvable at compile time, not about changing data or settings.
The Common Causes, in Order of Frequency
Most occurrences fall into a small number of patterns. Checking these first resolves the majority of cases.
- Simple typos.
closewritten asclsoe, orta.smawritten asta.smaa. The compiler has no fuzzy matching, so any deviation is an unknown name. - Wrong case. Pine Script is case-sensitive.
Close,CLOSE, andcloseare three different identifiers; only the last is the built-in price series. - Using a variable before its declaration. Pine Script reads top to bottom. A reference on line 5 to a variable first declared on line 10 is undeclared at line 5.
- Scope problems. A variable declared inside an
ifblock, aforloop, or a user-defined function is local to that block. Referring to it outside the block produces the error. - Missing or wrong
var/varipdeclaration. Assigning to a name that was never introduced with=,:=after a declaration, or a declaration keyword leaves the name unknown. - Built-in not available in the script type. Some names exist only in indicators, only in strategies, or only in libraries. Using a strategy-only name in an indicator script yields an undeclared identifier.
- Version mismatch. A function introduced in a later Pine version is undeclared in a script whose
//@version=comment specifies an earlier one. The compiler only knows the names that existed in that version. - Namespace mistakes. Built-ins are grouped under namespaces such as
ta.,math.,request.,str., andarray.. Writingsma(close, 14)instead ofta.sma(close, 14), or using the wrong namespace prefix, leaves the bare name undeclared. - Library functions not imported. A call to a function from a published library requires an
importstatement with the correct alias. Without it, the alias and everything under it are undeclared. - Forward references between functions. A user-defined function that calls another function declared later in the script can fail, depending on how the call is structured.
How Scope and Declaration Order Produce the Error
Scope is the concept most often misunderstood. Every declaration has a visibility region. Top-level declarations are visible from the line where they appear to the end of the script. Declarations inside a block are visible only within that block and any nested blocks. Function parameters are visible only inside the function body.
Consider a script that computes an average only when a condition is true:
if close > open
myAvg = ta.sma(close, 20)
plot(myAvg)
Here myAvg is declared inside the if block. The plot call sits at top level, outside that block, so myAvg is not visible there and the compiler reports an undeclared identifier. The fix is to declare the variable at top level first, then assign inside the block using :=:
float myAvg = na
if close > open
myAvg := ta.sma(close, 20)
plot(myAvg)
Declaration order works the same way at top level. Pine Script does not hoist declarations the way some languages do; a name exists only after its declaration line has been read. Reordering the code so the declaration precedes every use is usually the whole fix.
Version, Namespace, and Script-Type Mismatches
Pine Script has evolved through several versions, and the set of built-in names differs between them. A script that begins with an older version comment compiles against that older language definition. Functions and variables added later are simply not part of the symbol table, so they appear as undeclared identifiers even though they are documented and work in newer scripts. Checking the version comment and the documentation for the matching version resolves this class of error.
Namespaces are the second mismatch. Many built-ins live under a prefix that identifies their category. The prefix is part of the name, not decoration. Omitting it, or using the wrong one, produces an undeclared identifier for the bare name. The documentation for each function states its full name including the namespace, and copying that full name is the reliable fix.
Script type matters too. Indicator scripts, strategy scripts, and libraries expose different sets of names. A name that only makes sense in a strategy—order-related functions, for example—is undeclared in an indicator. Changing the script declaration or moving the logic to the appropriate script type resolves it.
A Practical Debugging Sequence
When the error appears, work through the following steps in order. Each step narrows the search.
- Read the exact name in the error message and the line number. The compiler names the identifier it could not resolve.
- Search the script for every occurrence of that name and confirm the spelling and capitalization match the declaration exactly.
- Locate the declaration. If there is none, add one. If there is one, check whether it appears above every use.
- Check the scope. If the declaration is inside a block, either move it to top level or move the use inside the same block.
- Check the namespace. Compare the name against the documentation entry for the function or variable, including its prefix.
- Check the version comment and the script type against the documentation for that name.
- If the name comes from a library, confirm the
importstatement exists and the alias matches the one used in the call.
A common misconception is that the error means the script is broken beyond repair or that the platform is at fault. In practice it is almost always a local naming or ordering issue in the script text, and the compiler's line number points close to the problem.
Preventing the Error While Writing
Most occurrences can be avoided with a few habits. Declare variables at the top level when they need to be visible in multiple blocks, and assign inside blocks with :=. Copy built-in names directly from the documentation rather than typing them from memory, which preserves both the namespace and the exact spelling. Keep the version comment current and consistent with the functions used. When importing a library, use a short, memorable alias and apply it consistently. Compile early and often; the editor reports the first unresolved name it encounters, so fixing errors as they appear prevents a long list from accumulating.
Frequently Asked Questions
Is the undeclared identifier error the same as a typo?
Often, but not always. A typo is one cause. The same message also appears for scope problems, missing imports, version mismatches, and wrong namespaces, where the spelling is correct but the name is not visible at that point.
Why does a built-in function show as undeclared?
The most common reasons are a missing namespace prefix, a version comment that predates the function, or a script type that does not expose it. Checking the documentation entry for the exact full name and the version it requires usually identifies the cause.
Can I use a variable before declaring it?
No. Pine Script resolves names in reading order, so a declaration must appear before any use. Reordering the code so the declaration comes first is the fix.
Why does a variable declared inside an if block fail outside it?
Declarations inside a block are local to that block. To use the value outside, declare the variable at top level first and assign to it inside the block with the reassignment operator.
Does the error mean my script will not run at all?
Yes. Compilation fails, so the script cannot be added to a chart or executed until the unresolved name is fixed. There is no partial execution with this error.
Why does a library function show as undeclared?
Library functions are only visible after an import statement that brings in the library under an alias. Without the import, or with a mismatched alias, the call is an unknown name.