Skip to content
Algo Trade Analytics Docs

?: - Pine Script Operator

The ternary operator evaluates a boolean condition and returns one of two expressions. It is useful for choosing a value inside another expression.

condition ? valueWhenTrue : valueWhenFalse

condition must be boolean. The two result expressions must have compatible types; the returned value and qualifier follow Pine Script’s type rules.

//@version=6
indicator("Ternary example", overlay = true)
color candleColor = close >= open ? color.green : color.red
barcolor(candleColor)
color timeframeColor = timeframe.isintraday ? color.red :
timeframe.isdaily ? color.green :
timeframe.isweekly ? color.blue : na
  • The ternary operator returns a value; it does not create a local scope like an if structure.
  • It has the lowest operator precedence. Use parentheses when the intended grouping is not obvious.
  • Long chains are harder to review. Prefer switch when several named cases express the logic more clearly.