Edgoro
Compile error · Types and qualifiers

Pine Script: Value with NA type cannot be assigned (fix)

Fix "Value with NA type cannot be assigned to a variable that was defined without type keyword" in Pine Script by declaring the type: var float x = na.

Pine Editor console · line 3
Value with NA type cannot be assigned to a variable that was defined without type keyword

Cause

The variable starts as na and has no type keyword. na has no type of its own, so the compiler can't tell whether the variable is meant to hold a float, an int, a label or something else.

Broken code

TradingView reports the error on line 3.

Pine Script v6
//@version=6
indicator("Last pivot high", overlay = true)
var lastPivot = na
ph = ta.pivothigh(high, 5, 5)
if not na(ph)
    lastPivot := ph
plot(lastPivot)

Fixed code

The same script, corrected for Pine Script v6. Paste it into the Pine Editor and save.

Pine Script v6
//@version=6
indicator("Last pivot high", overlay = true)
var float lastPivot = na
ph = ta.pivothigh(high, 5, 5)
if not na(ph)
    lastPivot := ph
plot(lastPivot)

Why it works

The type keyword (float, int, string, color, line, label...) tells the compiler what the variable will hold, and na then simply means "no value yet". Other starting values don't need a keyword because their type can be inferred: x = 0.0 is a float and x = 0 is an int.

Not for bools in v6

var bool active = na fails with a different error in Pine Script v6, because a bool can no longer be na. Start it at false instead. See bool cannot be na.

Questions

How do I declare an empty variable in Pine Script?

Give it a type and assign na: var float level = na, var label lbl = na, var line ln = na. Check it later with na(level).

Can a bool variable start as na?

Not in Pine Script v6: bool values are only true or false. Start it at false, or use an int with a separate value for "unknown".

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.