IFF Pine Script v5: Convert Deprecated Functions to Ternary
Pine Script v5 removed iff(). Replace a call such as iff(condition, a, b) with condition ? a : b, but also check how its arguments are evaluated. The old function evaluates both alternatives; the ternary operator evaluates only the selected alternative. That difference can change history-dependent calculations.
A complete v5 replacement
This example keeps the historical v5 migration target explicit. Pine v6 is available for new work, but changing the version alone is not a complete migration.
//@version=5
indicator("Ternary candle colors", overlay=true)
color selectedColor = close >= open ? color.green : color.red
barcolor(selectedColor)
A doji is green because the condition includes equality. Assigning selectedColor alone would not color candles; barcolor() provides that output. The same question-mark/colon operator also existed in v4, so using it does not require v5 by itself.
Preserve calculations that need every bar
Consider a choice between the highest high and lowest low over ten bars. Compute both series on every execution before selecting the output:
//@version=5
indicator("Precompute ternary alternatives", overlay=true)
float highestValue = ta.highest(high, 10)
float lowestValue = ta.lowest(low, 10)
float selectedValue = close >= open ? highestValue : lowestValue
plot(selectedValue, "Selected extreme", color=color.orange)
This keeps the rolling calculations outside the lazy conditional selection. It is a calculation example, not a trading strategy. The line can switch between the upper and lower extreme as the current candle changes.
TradingView's v5 migration guide explains the evaluation difference. Do not infer a universal speed improvement from replacing the function: performance depends on the actual script, and preserving results comes first.
Missing values are different from undeclared variables
na represents an unavailable value. For a suitable numeric expression, nz(x) substitutes zero when x is na; nz(x, replacement) uses the specified replacement. It does not declare x, repair an identifier that does not exist, or automatically use the previous bar.
Declare persistent accumulators before updating them, use := for reassignment, and define their reset rule. For a session calculation, specify which session boundary is intended. Substituting zero blindly can hide missing-volume or warm-up problems instead of solving them.
Migration checklist
- Save the working original and its declared version.
- Identify each
iff()call and inspect nested arguments before rewriting it. - Move calculations that need consistent execution outside conditional branches.
- Apply the other v5 changes, including the appropriate namespaces and declaration function.
- Compile, then compare values on the same symbol, timeframe, inputs and historical bars.
- If moving onward to v6, follow its migration guide separately and compare again.
There is no sunset date established here for v4 execution. The removal of iff() from v5 does not prove a v4 script suddenly stopped working. Nor does a successful conversion prove identical trading results.
For the surrounding language structure, see the Pine Script tutorial.

