feature: add one-cancels-the-other (OCO) order group - #9635
Draft
Romazes wants to merge 6 commits into
Draft
Conversation
Romazes
marked this pull request as draft
July 24, 2026 15:34
- add ComboType to GroupOrderManager and the OneCancelsTheOtherOrder api - gate live order groups behind BrokerageModel.SupportsGroupExecution - simulate group fill and sibling cancel in the backtesting brokerage - count one leg per group in buying power and open order aggregations - carry the group manager through the order factory and json round trip
Romazes
force-pushed
the
feature-8253-oco-order
branch
from
July 28, 2026 20:34
99e04e7 to
05ad8b1
Compare
- end the group pass on any fill, partial as well as complete - take the executed quantity off every other open leg instead of only canceling on a complete fill, and cancel a leg left with nothing to execute - add a regression algorithm that fails without the fix - assert the sibling reduction in the backtesting brokerage tests
- add a second group that buys 100 and is won by its stop market leg - sell 200 in the first group so holdings reverse through zero - open the position on its own bar and drop the price rounding the transaction handler already does - find the legs by order type instead of by list index
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 a one-cancels-the-other (OCO) order group.
QCAlgorithm.OneCancelsTheOtherOrderplaces 2 or 3Limit/StopMarketorders together: when one leg fully fills, the other legs are canceled in the same order-event batch. A partial fill does not cancel the siblings.C#:
Python:
Main changes:
ComboTypeenum onGroupOrderManager(Combo,OneCancelsTheOther). Previously serialized groups keep loading asCombo.IBrokerageModel.SupportsGroupExecution(ComboType)with a default body that only acceptsCombo, so existing brokerage models (including Python ones) keep today's behavior.AlpacaBrokerageModelopts into OCO. In live trading the transaction handler rejects a group the brokerage model does not support; backtesting and paper trading simulate the group in the engine.BacktestingBrokerageevaluates the legs of a group in a fixed order (stop legs first, then limit legs, then by id) and cancels the open siblings as soon as one leg fully fills.LiquidateandCancelOpenOrdersissue one cancel per group instead of one per leg, since canceling one leg cancels the whole group.0001-bracket-order-type.mdand0002-one-cancels-the-other-order-group.md.Related PR(s)
N/A
Related Issue
#8253
Motivation and Context
First step of the bracket order plan: OCO now, conditional (OTO) next, bracket on top of both. Users can protect a position with a take-profit and stop-loss pair without managing the cancellation themselves.
Requires Documentation Change
Yes — the new
OneCancelsTheOtherOrderAPI needs documentation.How Has This Been Tested?
OneCancelsTheOtherOrderRegressionAlgorithm(winner leg fills and the sibling cancels in the same event batch),OneCancelsTheOtherOrderCancelRegressionAlgorithm(canceling one leg cancels the whole group), plusOneCancelsTheOtherOrderDemoAlgorithm.BacktestingBrokerageTests(group fill, cancel and invalid paths),AlgorithmTradingTests(submit validation),BrokerageTransactionHandlerTests(live support gate, projected holdings),SecurityPortfolioManagerTestsandCashBuyingPowerModelTests(buying power),OrderJsonConverterTestsandOrderTests(serialization round trip),PythonWrapperTests,AlpacaBrokerageModelTests,DefaultBrokerageModelTests.Types of changes
Checklist:
bug-<issue#>-<description>orfeature-<issue#>-<description>