Paste this indicator into the Pine Editor. Its synthetic price is 100 on bar 0, 103 on bar 1, and 101 thereafter, independently of the chart symbol. Inspect the first three dataset bars in the Data Window and compare them with the table. The plots expose both the unwanted zero-price result and the chosen initialization.
Synthetic data demonstration: missing history, fallback, and cumulative statepinescript
//@version=6
indicator("na and nz: synthetic first-bar demo", overlay = false)
// Demonstration prices, not market data.
float demoPrice = bar_index == 0 ? 100.0 : bar_index == 1 ? 103.0 : 101.0
float previousPrice = demoPrice[1]
float rawDelta = demoPrice - previousPrice
float zeroPriceDelta = demoPrice - nz(previousPrice)
// Explicit convention: the initial change contributes zero.
float initializedDelta = demoPrice - nz(previousPrice, demoPrice)
var float contaminatedTotal = 0.0
contaminatedTotal := contaminatedTotal + rawDelta
var float initializedTotal = na
initializedTotal := nz(initializedTotal, 0.0) + initializedDelta
plot(demoPrice, "Synthetic price", display = display.data_window)
plot(rawDelta, "Raw difference", color = color.gray, style = plot.style_linebr)
plot(zeroPriceDelta, "Zero-price fallback difference", color = color.orange)
plot(initializedDelta, "Initialized difference", color = color.blue)
plot(contaminatedTotal, "Contaminated total", display = display.data_window)
plot(initializedTotal, "Initialized total", color = color.green)
plot(na(previousPrice) ? 1 : 0, "Previous price missing (1=yes)", display = display.data_window)