Add contingent orders: OCO, OTO, OUO and brackets - #9828
Open
Martin-Molinero wants to merge 5 commits into
Open
Martin-Molinero wants to merge 5 commits into
Martin-Molinero wants to merge 5 commits into
Conversation
This was referenced Sep 23, 2026
Martin-Molinero
force-pushed
the
feature-contingent-orders
branch
from
September 23, 2026 22:54
f019928 to
a99b51b
Compare
Introduces contingent orders, sets of independent orders whose lifecycle is related: one cancels other (OCO/OCA), one triggers other (OTO), one updates other (OUO) and any composition of them like brackets (OTOCO), including combo orders as members. - Order model: Order.Contingency (OrderContingency) with the set shared by its orders and per order links (contingency id, type, nullable role parent/child, triggered state), JSON serialization (backwards compatible). OrderContingency owns the composition: Trigger/Relate join orders (single orders or all the legs of a combo order) into a set, used by the algorithm and by the brokerages to rebuild the contingencies of their open orders - Algorithm API: OrderFactory on the algorithm creates SubmitOrderRequests composed through Triggers, Bracket, OneCancelsOther and OneUpdatesOther, submitted with Order(); BracketOrder, OneCancelsOtherOrder, OneUpdatesOtherOrder and OneTriggersOtherOrder helpers. Every existing order method goes through the same submit path, no API changes - Transaction handler: atomic submission once all the orders arrive, buying power skipped for held children and shared by OCO siblings, per set request routing, order update events for triggered children and brokerage side resizing - Backtesting/paper brokerage: ContingentOrderProcessor drives the lifecycle (hold, trigger on full fill, cancel siblings, reduce OUO, cascade cancels), triggered orders require new data, stop loss wins same bar ties, trailing stops anchor on trigger - Brokerage models: IB, Charles Schwab, TradeStation, Alpaca and Binance validate what each brokerage supports, the rest reject - Brokerages: ContingentOrderCache and GroupOrderCacheManager on the base brokerage, OnContingentOrdersTriggered and shared helpers for plugins - Regression algorithms (C# and Python) and unit tests Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Martin-Molinero
force-pushed
the
feature-contingent-orders
branch
from
September 24, 2026 14:21
a99b51b to
7c732a0
Compare
ContingentOrderTestParameters builds OCO, OUO, OTO and bracket sets out of the existing order test parameters, or any custom set. BrokerageTests gets ContingentOrdersCancel, ContingentOrdersUpdate and ContingentOrdersTrigger, which brokerages opt into like the rest. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…gent brokerage tests TradeStation rejects a bracket group (BRK) without a stop order. The contingent cancel test no longer assumes the rest of a group is canceled with its canceled member, and the trigger test takes updated orders as working. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
The brokerage open orders are rebuilt with the same contingencies as the placed orders. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…test Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds contingent orders: sets of independent orders whose lifecycle is related.
Algorithm API. Existing order methods are unchanged and go through the same submit path.
OrderFactory(self.order_factory) createsSubmitOrderRequests. They're composed withTriggers,Bracket,OneCancelsOtherandOneUpdatesOther, then submitted withOrder(...).BracketOrder,OneCancelsOtherOrder,OneUpdatesOtherOrderandOneTriggersOtherOrderare helpers for the common shapes.Order model.
Order.Contingency(OrderContingency) holds the set shared by its orders, with per-order links: contingency id, type, and role (parent, child, or none for siblings).OrderContingencyowns the composition (Trigger/Relate). The brokerages also use it to rebuild the contingencies of their open orders.OrderContingency.IsWaitingForTriggertells whether a child is still held.Transaction handler.
GetOpenOrdersRemainingQuantityand in the cash buying power model.Backtesting.
ContingentOrderProcessordrives the lifecycle: holding children, triggering on a complete fill, cancelling siblings, resizing OUO siblings and cascading cancels.Brokerage models.
BrokerageexposesContingentOrderCacheandOnContingentOrdersTriggeredfor the plugins.Related Issue
Brokerage plugin support, to be merged after this one:
Motivation and Context
Brackets, OCO and OTO are standard order types the supported brokerages offer natively. Until now algorithms had to emulate them with order events, which isn't atomic and leaves naked positions when a fill and a cancel race.
Requires Documentation Change
Yes: the new order types and APIs (
OrderFactory,Order,BracketOrder,OneCancelsOtherOrder,OneUpdatesOtherOrder,OneTriggersOtherOrder) and what each brokerage supports.How Has This Been Tested?
AlgorithmOrderFactoryTestsContingentOrdersTransactionHandlerTests, including the remaining open quantity aggregationContingentOrderTests,ContingentOrderProcessorTestsContingentOrdersBrokerageModelTestsBracketOrder,BracketOrderLimitEntryOneCancelsOtherOrder,OneCancelsOtherOrderCashAccount,OneTriggersOtherOrder,OneUpdatesOtherOrderContingentOrderCancel,ContingentOrderUpdate,ContingentComboOrder,ContingentTrailingStopOrderLiveOptionChainProviderReturnsFutureOptionData, which also fails on master (CME endpoint error).Types of changes
Checklist:
bug-<issue#>-<description>orfeature-<issue#>-<description>🤖 Generated with Claude Code