Fix OnAssignmentOrderEvent fired before portfolio has assigned shares#9334
Open
tsen1220 wants to merge 1 commit intoQuantConnect:masterfrom
Open
Fix OnAssignmentOrderEvent fired before portfolio has assigned shares#9334tsen1220 wants to merge 1 commit intoQuantConnect:masterfrom
tsen1220 wants to merge 1 commit intoQuantConnect:masterfrom
Conversation
EmitOptionNotificationEvents processed fills one at a time and fired HandlePositionAssigned immediately after the option fill, before the underlying delivery fill was processed. Split the single foreach into two passes: first process all fills to update the portfolio, then fire assignment events. Fixes QuantConnect#9321
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
EmitOptionNotificationEventsprocessed fills one at a time in a singleforeachloop, firingHandlePositionAssigned(which triggersOnAssignmentOrderEvent) immediately after the option fill — before the underlying delivery fill was processed. This meant the assigned shares were not yet in the portfolio when the user'sOnAssignmentOrderEvent/on_assignment_order_event()callback executed.Split the single
foreachinto two passes: first process all fills viaHandleOrderEventto update the portfolio, then iterate again to fire assignment events. This ensures the underlying shares are in the portfolio andOnOrderEventhas already been called whenOnAssignmentOrderEventfires.Both C# and Python algorithms are affected and fixed, since they share the same engine code path.
Related Issue
Fixes #9321
Motivation and Context
Users cannot reliably handle assignment in
OnAssignmentOrderEvent(e.g. liquidate the delivered shares, open a new strategy) because the portfolio does not yet reflect the assigned position. The only workaround was to useOnOrderEventinstead, which is unintuitive.Requires Documentation Change
No.
How Has This Been Tested?
Added 6 new unit tests in
BrokerageTransactionHandlerTests:OptionAssignmentEventFiredAfterPortfolioUpdate(2 cases) — verifies portfolio state and event ordering for at-expiry assignment (Short Call + Short Put)EarlyAssignmentEventFiredAfterPortfolioUpdate(4 cases) — verifies portfolio state and event ordering for early assignment (Short Call + Short Put, full + partial)Each test asserts:
OnAssignmentOrderEventfiresOnOrderEventfor the underlying fill has already been called beforeOnAssignmentOrderEventAll 6 new tests and 32 existing related tests pass.
Types of changes
Checklist:
bug-<issue#>-<description>orfeature-<issue#>-<description>