Sidechair

fixnan()

Browse the reference

Fills each na in a series with the most recent value that was not na.

Try it in the editor

fixnan(source) carries the last real value forward over any gaps. If source has a value on the bar, that value is returned; if it is na, you get the last value seen before.

Until the series has produced its first real value there is nothing to carry, so the result stays na on those bars.

Syntax

Pine Script
fixnan(source: series int/float) -> series float
fixnan(source: series int) -> series int
fixnan(source: series color) -> series color

Arguments

NameTypeDefaultDescription
sourceseries int/float or series int or series colorrequiredThe series whose na values are filled with the latest earlier value.

Returns

series float or series int or series color: The source where it has a value, otherwise the latest earlier value.

Remarks

  • Use it on sparse series such as pivots: fixnan(ta.pivothigh(high, 5, 5)) turns the occasional pivot into a level that stays until the next pivot.
  • fixnan() reads the series on every bar, so call it at the top level of the script, not inside an if block.

Example

Pine ScriptPivot high as a levelOpen in editor
//@version=6
indicator("Last pivot high", overlay = true)
ph = ta.pivothigh(high, 5, 5)
plot(fixnan(ph), "Last pivot high", color.red, 2, plot.style_stepline)
plot(ph, "Pivot", color.red, 4, plot.style_circles, offset = -5)

See also

Runs on Sidechair