Appearance
Personalize your experience
Synthetic Bracket Orders, OCO Logic & Ratcheting Trailing Stops
Quant & Broker APIsBuilding a server-side OCO (One-Cancels-the-Other) stop-loss and take-profit manager with intrabar ratchet rules.
Core Architecture & Intuition
Building a server-side OCO (One-Cancels-the-Other) stop-loss and take-profit manager with intrabar ratchet rules. Within the Quant & Broker APIs curriculum (Strategy DSL & Scripting → State Machines & Stops), this topic establishes the concrete quantitative rules and execution mechanics required for live market deployment.
Professional desks never rely on ambiguous visual guesses for bracket order. Instead, every entry, invalidation level, and risk parameter is defined mathematically before a single order reaches the exchange gateway.
Hover over key terms below for instant definitions: Bracket Order, Oco, and One Cancels The Other. For the complete mathematical derivation and edge-case analysis, open .
Interactive Concept Breakdown
- Bracket Order — Core structural driver governing synthetic bracket orders, oco logic & ratcheting trailing stops.
- Oco — Prevents premature entries and enforces mechanical discipline across State Machines & Stops.
- One Cancels The Other — Ensures worst-case scenario losses stay strictly within predetermined R-multiple budgets.
Step-by-Step Execution Protocol: State Machines & Stops
- 11. Pre-Session Calibration & Regime CheckMeasure baseline volatility and liquidity parameters for bracket order prior to market open.
- 22. Mechanical Signal ConfirmationWait for a completed bar close confirming the oco threshold—never pre-empt mid-candle.
- 33. Risk-Normalized Sizing & Order PlacementCompute exact lot quantity from the invalidation distance and dispatch via a Marketable Limit order.
- 44. Post-Trade Telemetry & AuditLog arrival price, fill slippage, and MAE/MFE metrics against the State Machines & Stops benchmark.
Parameters, Rules & Reference Matrix
| Parameter / Rule | Type | Default | Description |
|---|---|---|---|
| primaryThreshold* | float | Calibrated per ATR | Core quantitative trigger governing Synthetic Bracket Orders, OCO Logic & Ratcheting Trailing Stops. |
| confirmationWindow | bars | 1 closed bar | Requires full bar closure to eliminate intrabar repainting. |
| maxRiskAllocation | % equity | 0.5% - 1.0% | Maximum capital at risk if invalidation level is hit. |
Implementation & Quantitative Model
export function evaluateOcoBracket(barHigh: number, barLow: number, stopPrice: number, targetPrice: number) { // Pessimistic rule: if both stop and target are touched in same bar, assume Stop hit first if (barLow <= stopPrice) return { exit: "STOP_LOSS", fillPrice: stopPrice }; if (barHigh >= targetPrice) return { exit: "TAKE_PROFIT", fillPrice: targetPrice }; return null;}Desk Verification Checklist
Confirm before deploying live capital
- Confirm State Machines & Stops regime alignment before enabling entries.
- Verify candle closure beyond the trigger threshold (no intra-bar guessing).
- Size position strictly from the distance to the structural invalidation point.
Cross-reference standard risk definitions such as drawdown, slippage, and when auditing your logs.
More in State Machines & Stops
- Deterministic Position Lifecycle State Machines (`FLAT → PENDING → OPEN`)
Eliminating race conditions and double-orders by modeling strategy execution as a strict finite state machine.