Picking a value: if and ? :

if c then a else b, or c ? a : b, picks a value per bar.

For AI tools: this page as Markdown · the whole language in one file

if c then a else b gives a on bars where c is true and b elsewhere. c ? a : b is the same thing, shorter. else is required; use none for "no value".

1fast = ema(close, 9)2slow = ema(close, 21)3trend = fast > slow ? 1 : -14plot(fast, color = fast > slow ? green : red)5plot(slow, color = if fast > slow then green else red)6plot(trend, "Trend", style = steps)
  • ? : binds loosest: a + b > c ? x : y picks on the whole comparison.
  • They chain: a ? 1 : b ? 2 : 3, or if a then 1 else if b then 2 else 3.
  • Both sides must be the same kind of value (two numbers, two colors, two yes/nos).
  • With a setting as the condition, if can pick between texts and whole formulas: average == "ema" ? ema(close, n) : sma(close, n).

See also: if, Colors, none