na - PineScript Variable
Overview
Section titled “Overview”A keyword signifying “not available”, indicating that a variable has no assigned value.
naQualifier
Section titled “Qualifier”simple
Examples
Section titled “Examples”Example 1
Section titled “Example 1”//@version=6indicator("na")// CORRECT// Plot no value when on bars zero to nine. Plot `close` on other bars.plot(bar_index < 10 ? na : close)// CORRECT ALTERNATIVE// Initialize `a` to `na`. Reassign `close` to `a` on bars 10 and later.float a = naif bar_index >= 10 a := closeplot(a)// INCORRECT// Trying to test the preceding bar's `close` for `na`.// The next line, if uncommented, will cause a compilation error, because direct comparison with `na` is not allowed.// plot(close[1] == na ? close : close[1])// CORRECT// Use the `na()` function to test for `na`.plot(na(close[1]) ? close : close[1])// CORRECT ALTERNATIVE// `nz()` tests `close[1]` for `na`. It returns `close[1]` if it is not `na`, and `close` if it is.plot(nz(close[1], close))Remarks
Section titled “Remarks”- Do not use this variable with comparison operators to test values for na, as it might lead to unexpected behavior. Instead, use the na function. Note that na can be used to initialize variables when the initialization statement also specifies the variable’s type.