Sidechair

nz()

Browse the reference

Replaces na with 0, or with a value you give, and returns any other value unchanged.

Try it in the editor

nz(source, replacement) returns replacement when source is na and source otherwise. Leave the replacement out and numbers become 0.

It is how you keep na from spreading: arithmetic with na is na, so a running total such as total := nz(total[1]) + x survives the first bar, where total[1] does not exist yet.

Syntax

Pine Script
nz(source: simple int, replacement: simple int (optional)) -> simple int
nz(source: simple int/float, replacement: simple int/float (optional)) -> simple float
nz(source: simple color, replacement: simple color (optional)) -> simple color
nz(source: series int, replacement: series int (optional)) -> series int
nz(source: series int/float, replacement: series int/float (optional)) -> series float
nz(source: series color, replacement: series color (optional)) -> series color

Arguments

NameTypeDefaultDescription
sourcesimple int or simple int/float or simple color or series int or series int/float or series colorrequiredThe value to check. It can be an int, a float or a colour, and it can come from a series.
replacementsimple int or simple int/float or simple color or series int or series int/float or series color0 for numbersThe value to return when source is na. Leave it out and a number is replaced with 0.

Returns

  • simple int: The source when it holds a value, otherwise the replacement.
  • simple float: The source, or the replacement when it is na.
  • simple color: The source, or the replacement when it is na.
  • series int: The source, or the replacement when it is na.
  • series float: The source, or the replacement when it is na.
  • series color: The source, or the replacement when it is na.

Remarks

  • nz() fixes one bar at a time. To carry the last real value forward, use fixnan().
  • The replacement has to have the type of source: a number for numbers, a colour for colours.

Examples

Pine ScriptRunning total of up volumeOpen in editor
//@version=6
indicator("Up volume total")
var float total = 0.0
total := nz(total[1]) + (close > open ? volume : 0)
plot(total, "Up volume", color.green)
Pine ScriptFallback valueOpen in editor
//@version=6
indicator("nz fallback", overlay = true)
avg = ta.sma(close, 20)
plot(nz(avg, close), "SMA, close until ready", color.orange)

See also

Runs on Sidechair