Threshold
[[threshold]] compares reference with comparison by applying the relative gap defined by threshold. The reference, comparison, and threshold expressions are evaluated on every candle, so the condition is recalculated from the current values.
The condition is evaluated as follows:
position = "above": the condition is true when .position = "below": the condition is true when .
When the condition is true, execution continues to the block named in next_block_id.
Block declaration
A strategy can contain multiple [[threshold]] blocks.
Examples
Fixed threshold
This example routes to next_block when close_price is at least 5% above ma_slow.
[[threshold]]
id = "my_threshold"
position = "above"
reference = "close_price"
comparison = "ma_slow"
threshold = 5
next_block_id = "next_block"Threshold from expression
Use quotes when the threshold is computed from other values.
[[threshold]]
id = "dynamic_threshold"
position = "above"
reference = "close_price"
comparison = "ma_slow"
threshold = "(atr / close_price) * 100"
next_block_id = "next_block"Optimizable threshold with [[constant]]
This example defines entry_gap_pct as a search range and uses it as the threshold value.
[[constant]]
id = "entry_gap_pct"
start = 1.0
stop = 3.0
step = 0.5
[[threshold]]
id = "gap_filter"
position = "above"
reference = "close_price"
comparison = "ma_slow"
threshold = "entry_gap_pct"
next_block_id = "next_block"Block parameters
| Parameter | Description |
|---|---|
idText Required | Unique identifier of the block. |
positionText Required | Direction of the comparison: "above" or "below". |
referenceText Required | Reference value to compare (e.g. close_price). |
comparisonText Required | Expression compared to the reference (e.g. a moving average). |
thresholdNumeric expression Required | Allowed gap percentage. Accepts an unquoted number (e.g. 5) or a quoted expression (e.g. "(atr / close_price) * 100"). |
next_block_idText Conditional | Required when the [[threshold]] block is used on its own. Must be omitted when it is used as a child of an [[and]] or [[or]] block. |
This block does not produce any specific output variable; it only controls the execution flow according to the observed difference.
Equivalent with [[condition]]
The [[threshold]] block is a readability shortcut for a condition expression.
Equivalent for position = "above":
[[condition]]
id = "gap_filter"
condition = "close_price >= ma_slow * (1 + entry_gap_pct / 100)"
next_block_id = "next_block"Equivalent for position = "below":
[[condition]]
id = "gap_filter"
condition = "close_price <= ma_slow * (1 - entry_gap_pct / 100)"
next_block_id = "next_block"Common pitfalls
- A negative
thresholdshifts the target level in the opposite direction and makes the condition more permissive. In practice, use a non-negative threshold unless this behavior is intentional. - If
comparisonis zero or negative, the intuitive meaning of a “percentage gap” is weaker. Confirm that this is the behavior you actually want.