Skip to main content
On this page

Undeclared Identifier in Pine Script: Scope, Names, and Versions

· 4 min read
Pineify Team
Pine Script and AI trading workflow research team

An undeclared identifier means Pine cannot resolve a name at the point where it is used. Check spelling, declaration order, scope, and the script's version before changing the calculation.

Start with the compiler's first reported name​

A variable must be declared before its use in a scope that can access it. Names are case-sensitive. A missing function or namespace can produce a related function-resolution diagnostic rather than the exact words “undeclared identifier”; follow the actual message.

This existing complete v5 example declares values before plotting them:

//@version=5
indicator("My Indicator", overlay=true)

// Declare variables at the top
var float mySignal = na
var int barCount = 0

// Now use them in your logic
if close > open
mySignal := close - open
barCount += 1

plot(mySignal, title="Signal", color=color.blue)

var retains the last value across bars. Here mySignal stays at its previous value on a bearish or flat candle; it does not reset automatically. If each bar needs an unavailable initial float, declare it with an explicit float type at global scope on each execution. An untyped initialization to na cannot establish the variable's type.

Distinguish scope from shadowing​

The next retained fragment illustrates a local declaration and a separate outer declaration. It is not a standalone script; it needs a version and indicator declaration:

// Wrong way - variable trapped in if block
if close > open
float myVar = close - open // This won't work outside the if statement

// Right way - declare outside, modify inside
float myVar = na
if close > open
myVar := close - open // Use := to modify existing variable

plot(myVar)

The first local myVar cannot be accessed outside its block. The later outer declaration creates a different variable, then := updates that outer value. Declaring an identically named local variable with = can compile while leaving the outer variable unchanged. That is a shadowing/logic problem, not necessarily an undeclared-name error.

Unavailable plotted samples are not zero. Also, the default line plot can bridge missing samples; use a plot style deliberately rather than assuming na always creates a visible line break.

Check version changes in the migration guide​

Pine v5 introduced names such as indicator(), ta.sma(), math.abs(), and request.security(). Some changes, including color.red, belong to earlier migrations. input() still exists; typed helpers such as input.int() are not a universal mandatory replacement.

There is no general rule that replacing security() with request.security() changes the leading symbol/timeframe/expression argument order. Review behavior, named arguments, and data alignment against the official v5 migration guide rather than applying speculative edits.

Inspect values after compilation succeeds​

This retained debugging fragment assumes myVar already exists in the surrounding script:

// Use this to check if variables are being set correctly
if barstate.islast
label.new(bar_index, high, text="Debug: " + str.tostring(myVar))

A label cannot execute while a compilation error remains. A displayed na means the value is unavailable; it does not by itself identify a declaration or scope fault. First fix compilation, then inspect the series over the bars where the problem occurs.

Use the Pine type system for argument restrictions and the undeclared identifier diagnostic guide for a focused reproducer. Reduce the script to the failing declaration and its use before adding other logic back.

Pineify | Best Pine Script Generator