If
On this page
[[if]] creates a branch in the strategy based on the result of condition. When the expression is true, execution jumps to the block referenced by then_block_id. When the expression is false or evaluates to NaN, execution jumps to the block referenced by else_block_id. In both cases, execution continues immediately, without waiting for the next candle.
[[if]] cannot be used in the condition_ids key of an [[and]] or [[or]] block. To combine several conditions before choosing a branch, use [[and]] or [[or]] first, then continue to [[if]].
Block declaration
A strategy can contain multiple [[if]] blocks.
Examples
Choose between two branches
This example uses [[if]] because both outcomes are defined: the strategy routes to enter_long when the condition is true, otherwise it routes to enter_short.
[[if]]
id = "trend_switch"
condition = "rsi_fast > rsi_slow"
then_block_id = "enter_long"
else_block_id = "enter_short"Single branch with condition
This example uses [[condition]] because there is no branch to execute when the condition is false. As long as close > resistance is false, the strategy moves to the next candle and evaluates the condition again. Once the expression becomes true, it routes to enter_breakout.
[[condition]]
id = "breakout_guard"
condition = "close > resistance"
next_block_id = "enter_breakout"Block parameters
| Parameter | Description |
|---|---|
idText Required | Unique identifier of the block. |
conditionExpression Required | Condition expression evaluated on every candle. 0.0 and NaN are considered false; any other non-zero result is considered true. |
then_block_idText Required | Identifier of the block executed when the condition is true. |
else_block_idText Required | Identifier of the block executed when the condition is false. |