cond
Conditional/ternary operator (if-then-else)
cond(condition, if_true, if_false)
Parameters
| Name | Type | Description |
|---|---|---|
condition |
bool | Boolean expression to evaluate |
if_true |
field | Value returned when condition is true |
if_false |
field | Value returned when condition is false |
Formula
result = if_true when condition, else if_false
Examples
// 1 for up days, 0 for down days
cond(close > open, 1, 0);
// distance above MA (zero when below)
cond(close > sma(close, 20), close - sma(close, 20), 0);
// -1 = overbought, 1 = oversold, 0 = neutral
cond(rsi(close, 14) > 70, -1,
cond(rsi(close, 14) < 30, 1, 0));
// body-to-range ratio, safe when range is zero
cond(bar_range != 0, bar_change / bar_range, 0);
Returns
field
Notes
Can be nested for multi-level classification (if-else-if chains). Arguments can be any expression: fields, functions, or constants. Both branches are evaluated, so use for calculations not side effects.