If

[[if]] is a conditional block that evaluates the condition expression each time the strategy reaches it.

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 if it is defined.

If else_block_id is not defined and the expression is false or evaluates to NaN, the block is not validated on that candle. The strategy stays on the same [[if]] block and evaluates it again on the next candle.

The condition field of the [[if]] block can directly contain the condition used to choose between then_block_id and else_block_id.

This block cannot be referenced in the conditions key of an [[and]] or [[or]] block.

[[and]] and [[or]] only accept blocks that answer a simple question on the current candle: is the condition true or false. The [[if]] block does not serve that purpose. It is not meant to validate a condition for a logical parent. Its role is to choose the next step of execution itself through then_block_id and else_block_id.

For that reason, [[if]] must remain in the main execution flow of the strategy. If you need to validate a logical combination before choosing a branch, let the [[and]] or [[or]] block validate first, then route execution to [[if]] through next_block_id.

Block declaration

A strategy can contain multiple [[if]] blocks.

Examples

Explicit else branch

This example 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"

No else branch

This example routes to enter_breakout when the condition is true. When the condition is false or evaluates to NaN, the strategy stays on this block and re-evaluates it on the next candle.

[[if]]
id            = "breakout_guard"
condition     = "close > resistance"
then_block_id = "enter_breakout"

Block parameters

ParameterDescription
id
 Text
 Required
Unique identifier of the block.
condition
 Expression
 Required
Condition expression evaluated on every candle. 0.0 and NaN are considered false; any other non-zero result is considered true.
then_block_id
 Text
 Required
Identifier of the conditional block executed when the condition is true.
else_block_id
 Text
 Optional
Identifier of the block executed when the condition is false. If omitted, and the condition is false or evaluates to NaN, the strategy stays on this [[if]] block and re-evaluates it on the next candle.