Skip to content
Algo Trade Analytics Docs
Pine library page — review pending. This page is preserved for compatibility but is not part of the reviewed search index. Check the official Pine Script v6 reference before relying on its explanation, signature, or example.

nz() - Pine Script Function

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.

nz(source, replacement) → simple/series type
NameTypeDescription
sourcesimple/series int/float/bool/colorValue to sanitize.
replacementsimple/series int/float/bool/color (optional)Value to substitute when source is na. Defaults to the type-specific zero.

simple/series type — The original source when available; otherwise the replacement or type default.

  • 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 output na. Wrap them in nz() before plotting so your chart remains visible through warm-up periods.
//@version=6
indicator("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")