builder_attach_ruleWire a condition definition into a strategy slot (entry, exit, or gate) so the engine evaluates it.
Area
Strategy Builder
Group
Authoring
Access
Write
Risk
May change server state
Details
Full description
Details
Wire a definition into a strategy slot so the engine evaluates it during backtests. Call after building a definition with builder_add_condition or builder_compose. Every strategy needs at least one entry rule to trade.
Roles
entry— open a position when the definition fires (requiresdirection="long"or"short")exit— close the position when the definition fires (size_fraccontrols partial exits). Setdirection="long"or"short"to scope it; omit direction to apply it to BOTH sides.gate— block all entries when the definition is false (evaluated first)
builder_attach_rule(strategy_id, definition_id, role="entry", direction="long")
builder_attach_rule(strategy_id, stop_id, role="exit", direction="long") # long only
builder_attach_rule(strategy_id, time_exit_id, role="exit") # BOTH
Directional entry groups
- Entry rules with the same direction are ANDed: all must fire on the same bar.
- Long and short groups are alternatives. If both fire on one bar, the signal conflicts and no order is placed.
- Bidirectional strategies enter only while flat.
- No same-bar reversal: an opposite signal while invested is diagnostic only. Even if an exit fills on that bar, it does not enter the other side; the complete opposite entry group must fire again on a later bar while flat.
- Sizing and exits whose direction is omitted are shared by both directions.
To express OR / NOT / sequence within one direction or for gates, combine conditions into one definition with builder_compose. Gate rules are ANDed; exit rules remain independent and any firing closes.
Exit ordering — there is no exit ranking. The compiler never assigns ExitRule.priority, so every condition exit carries the default and they are evaluated in attachment order, first match closing. fixed_targets and trailing_stop are not condition exits at all — they compile to standing stop/limit orders filled against the bar's OHLC.
Returns rule_id and definition_id. Use rule_id for builder_detach_rule.
Capabilities
Bounded options and operating modes surfaced above the full JSON schema.
Role options
role
entryexitgateRequired Inputs
definition_idstringConditionDefinition ID to attachroleenum: entry, exit, gateConditionDefinition role: entry, exit, or gate (block entries)strategy_idstringStrategy IDOptional Inputs
directionanyOf (2 variants)Entry role: required ('long' or 'short'). Exit role: optionally scope the exit to that position direction; None means BOTH long and short. Not used for gates.remainder_unprotectedanyOf (2 variants)Explicit permission to leave a partial stop remainder unprotected (exit only)size_fracanyOf (2 variants)Exit role: fraction to close, greater than 0 through 1. None = full close.