Replaces na with 0, or with a value you give, and returns any other value unchanged.
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
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 colorArguments
| Name | Type | Default | Description |
|---|---|---|---|
| source | simple int or simple int/ | required | The value to check. It can be an int, a float or a colour, and it can come from a series. |
| replacement | simple int or simple int/ | 0 for numbers | The 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
//@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)//@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