Variable

[[variable]] creates a numeric variable that is updated each time strategy execution reaches the block. It lets you keep state across candles, such as counters, smoothed values, or custom intermediate metrics reused by later blocks.

Unlike condition-testing blocks ([[condition]], [[trend]], etc.), [[variable]] does not evaluate to true or false for validation. It is auto-validated: it performs the calculation, then immediately continues to the next block.

How the block works

  • At backtest initialization, the variable is set from initialization.
  • On each visit to the block, formula is evaluated and its result replaces the current variable value.
  • The block always validates on the current candle and then jumps to next_block_id.
  • This block cannot be used as a child of [[and]] or [[or]].

Block declaration

A strategy can contain multiple [[variable]] blocks. Each block manages one numeric variable named by variable.

formula and initialization follow the general expression syntax rules. See Expressions and functions. In this block, formula and initialization must stay single expressions: statement separators (;) and assignment operators (:=, +=, -=, *=, /=, %=) are not allowed.

Examples

Minimal block

This example initializes a counter at 0, then increments it by 1 on each pass.

[[variable]]
id             = "variable_counter"
variable       = "entry_counter"
initialization = 0
formula        = "entry_counter + 1"
next_block_id  = "next_step"

Smoothed state value

This example builds a reusable smoothed value using a simple exponential blend.

[[variable]]
id             = "variable_smooth_close"
variable       = "smooth_close"
initialization = "close"
formula        = "smooth_close * 0.9 + close * 0.1"
next_block_id  = "entry_filter"

Counter inside a strategy loop

This example counts consecutive bullish candles (close > open). After each update, the [[if]] block checks whether the counter has reached 3. If not, a [[wait]] block pauses until the next candle and routes execution back to [[variable]], so the counter keeps updating over time.

[[variable]]
id             = "variable_green_streak"
variable       = "green_streak"
initialization = 0
formula        = "close > open ? green_streak + 1 : 0"
next_block_id  = "check_green_streak"

[[if]]
id            = "check_green_streak"
condition     = "green_streak >= 3"
then_block_id = "enter_long"
else_block_id = "wait_next_candle"

[[wait]]
id            = "wait_next_candle"
wait_candles  = 1
next_block_id = "variable_green_streak"

Block parameters

ParameterDescription
id
 Text
 Required
Unique identifier of the block.
variable
 Text
 Required
Name of the numeric variable created/updated by the block.
Must not be empty and must match [A-Za-z_][A-Za-z0-9_]*.
Must also be unique in the runtime context (no collision with an existing symbol).
initialization
 Expression
 Required
Initial variable value at backtest startup.
Accepted TOML types: string, integer, floating-point number.
formula
 Expression
 Required
Expression evaluated each time the block is reached.
The result replaces the current value of variable.
Accepted TOML types: string, integer, floating-point number.
next_block_id
 Text
 Required
Identifier of the conditional block executed immediately after the calculation.

Exposed variable

The block exposes a reusable scalar variable for downstream expressions.

Assume this configuration:

[[variable]]
id             = "variable_smooth_close"
variable       = "smooth_close"
initialization = "close"
formula        = "smooth_close * 0.9 + close * 0.1"
next_block_id  = "entry_filter"

Then:

VariableDescription
smooth_close
Decimal
Current value computed by the block.

Notes

  • smooth_close is a current scalar variable (not an indexed indicator series like series[n]).
  • You can use it freely in numeric expressions (arithmetic, comparisons, logical expressions).