na() - Pine Script Function
Overview
Section titled “Overview”na() checks if a value equals Pine’s dedicated “not available” marker. The function works with both simple and series arguments, so you can guard against uninitialized calculations, incomplete security requests, or lookups that fall outside the available history.
Use na(x) rather than x == na. Comparisons with na always return false because na is not a number; the helper is the only reliable way to detect it.
Syntax
Section titled “Syntax”na(x) → simple boolna(x) → series boolParameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
x | simple/series int/float/bool/color | Value to test for na. Works with arrays, matrices, and drawing handles by passing their IDs. |
Returns
Section titled “Returns”simple/series bool — true if x is na, otherwise false.
Remarks
Section titled “Remarks”- Warm-up values for many
ta.*functions start asna. Useif not na(value)before plotting or branching on the result. - Paired helpers:
nz(x, replacement)substitutes a fallback fornavalues, whilefixnan(x)propagates the last non-naresult. nais also a literal you can assign (var float lastPivot = na) to denote an uninitialized state before you store actual data.
Example
Section titled “Example”//@version=5indicator("na() guard example", overlay=false)
length = input.int(14, "RSI length", minval=2)rsi = ta.rsi(close, length)
// Plot RSI only after it becomes available.plot(na(rsi) ? na : rsi, "RSI", color=color.teal)
// Flag bars where the value is still unavailable.plotshape(na(rsi), title="Warm-up bar", style=shape.circle, size=size.tiny, color=color.new(color.orange, 0), location=location.top)
// Compare with nz(): fill forward missing RSI values using the prior close.smoothed = nz(rsi, close)plot(smoothed, "nz() fallback", color=color.new(color.gray, 70))