nz() - Pine Script Function
Overview
Section titled “Overview”nz() protects your logic from na values by substituting a default. The helper supports both simple and series arguments, and it preserves the type of its input (numbers, colors, etc.). Without a replacement argument, it falls back to a sensible zero-value for the type (e.g., 0.0 for floats, color.new(color.black, 100) for colors).
`nz()` is fast and lightweight. Use it when you want to replace missing values, and reach for fixnan() when you prefer to carry forward the last known value instead.
Syntax
Section titled “Syntax”nz(source, replacement) → simple/series typeParameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
source | simple/series int/float/bool/color | Value to sanitize. |
replacement | simple/series int/float/bool/color (optional) | Value to substitute when source is na. Defaults to the type-specific zero. |
Returns
Section titled “Returns”simple/series type — The original source when available; otherwise the replacement or type default.
Remarks
Section titled “Remarks”- Use
nz(series, seriesReplacement)to replace missing values with another series (e.g.,nz(close[1], close)). - For color series,
nz()defaults to a fully transparent black; pass your own color when you need a visible fallback. - On the first bar of a script, many
ta.*functions outputna. Wrap them innz()before plotting so your chart remains visible through warm-up periods.
Example
Section titled “Example”//@version=5indicator("nz() example", overlay=false)
length = input.int(14, "RSI length", minval=2)rsi = ta.rsi(close, length)
defaultedRsi = nz(rsi, 50.0) // center RSI when it is not available yet
plot(rsi, "Raw RSI", color=color.new(color.blue, 60))plot(defaultedRsi, "RSI with fallback", color=color.orange, linewidth=2)
bgcolor(na(rsi) ? color.new(color.red, 90) : na, title="Highlight RSI warm-up bars")