Add BracketOrder API with engine-guaranteed OCO semantics - #9666
Draft
jhonabreul wants to merge 2 commits into
Draft
Add BracketOrder API with engine-guaranteed OCO semantics#9666jhonabreul wants to merge 2 commits into
jhonabreul wants to merge 2 commits into
Conversation
QCAlgorithm.BracketOrder places an entry order plus linked stop loss / take profit exit tickets (BracketOrderTicket). The transaction handler enforces the linkage on every order event: the entry fill places the exit legs sized to the filled quantity, a leg fill cancels its sibling (deterministically stop-loss-first on a bar spanning both legs), an unrelated order closing or flipping the position cancels the remaining legs, and a new bracket is refused while one is still active for the symbol.
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 first-class bracket order API with engine-guaranteed one-cancels-the-other (OCO) semantics:
QCAlgorithm.BracketOrder(...)submits the entry (market by default, limit viaentryLimitPrice) and returns aBracketOrderTicket.BracketOrderTicketholds the linked tickets plusCancel(),MoveStopLoss(price),MoveTakeProfit(price).OnOrderEvent:Transactions.GetBracketOrderTicket(symbol)exposes the active bracket.Related Issue
N/A
Motivation and Context
Hand-rolled stop-loss/take-profit state machines are common and error-prone: an overwritten ticket field can leave a stop leg dangling, and both legs filling on a gapping bar can flip the position. This makes the correct pattern a one-liner.
Requires Documentation Change
Yes: new
BracketOrderAPI andBracketOrderTickettype.How Has This Been Tested?
BracketOrderTests(16 tests) covering the full handler+brokerage loop: leg placement and sizing, OCO cancel in both directions, single-bar-spans-both-legs, manual close, partial reduction, re-entry refusal,Cancel(),MoveStopLoss, limit entry, short bracket, validation. Disabling the engine hook makes 12/16 fail.BracketOrderRegressionAlgorithm(C# + Python): full lifecycle over SPY minute data; both languages pass with identical statistics.Deferred
stop_loss/take_profit(absolute prices only for now).move_stop_to_breakeven().Types of changes
Checklist:
bug-<issue#>-<description>orfeature-<issue#>-<description>