?: - Pine Script Operator
Overview
Section titled “Overview”The ternary operator evaluates a boolean condition and returns one of two expressions. It is useful for choosing a value inside another expression.
Syntax
Section titled “Syntax”condition ? valueWhenTrue : valueWhenFalsecondition must be boolean. The two result expressions must have compatible types;
the returned value and qualifier follow Pine Script’s type rules.
Example
Section titled “Example”//@version=6indicator("Ternary example", overlay = true)
color candleColor = close >= open ? color.green : color.redbarcolor(candleColor)Chaining conditions
Section titled “Chaining conditions”color timeframeColor = timeframe.isintraday ? color.red : timeframe.isdaily ? color.green : timeframe.isweekly ? color.blue : naRemarks
Section titled “Remarks”- The ternary operator returns a value; it does not create a local scope like an
ifstructure. - It has the lowest operator precedence. Use parentheses when the intended grouping is not obvious.
- Long chains are harder to review. Prefer
switchwhen several named cases express the logic more clearly.