Fall back to per-leg margin when option strategy margin cannot be computed - #9682
Draft
jhonabreul wants to merge 2 commits into
Draft
Fall back to per-leg margin when option strategy margin cannot be computed#9682jhonabreul wants to merge 2 commits into
jhonabreul wants to merge 2 commits into
Conversation
…puted Legging into multi-leg option positions with sequential market orders hard-crashed algorithms with "Sequence contains no matching element" when a margin call probed a degenerate trial position group, and matched strategies without margin modeling (backspreads) threw NotImplementedException. OptionStrategyPositionGroupBuyingPowerModel now falls back to margining each leg individually instead of throwing, logging the condition once. Also adds an OptionLegData (quantity, right, strike, expiration) constructor so strategy legs can be built from Python with either date or datetime expiry, and a one-time hint suggesting combo orders when market orders leg into option positions.
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
Algorithms legging into multi-leg option positions with sequential market orders could crash mid-run. The option strategy margin model threw when a position group's legs didn't fit the modeled strategy's shape:
NotImplementedException, and other degenerate shapesInvalidOperationException.Fix:
OptionStrategyPositionGroupBuyingPowerModelfalls back to margining each leg individually instead of throwing. Well-formed groups keep strategy margin.GetMaintenanceMargin/GetInitialMarginRequirementcatchInvalidOperationExceptionand return per-leg margin sums, each leg margined through its own security'sBuyingPowerModel.NotImplementedExceptiontails for strategies without margin modeling (e.g.CallBackspread) use the same fallback.Also unblocks the recommended atomic-entry path and hints at it:
OptionStrategy.OptionLegDatagets a(quantity, right, strike, expiration, orderPrice = null)constructor, accepting Pythondateanddatetime. Previously only a parameterless constructor existed, so building legs from Python failed.OptionStrategy.Createand theOptionStrategyconstructor no longer NRE on symbol-less legs.QCAlgorithm.MarketOrderwarns once per algorithm when an option market order is placed while holding other contracts of the same chain, suggestingComboMarketOrder/OptionStrategies.Related Issue
N/A
Motivation and Context
Legging into spreads with plain market orders is legitimate API usage. It should never dead-end an algorithm in an engine crash with no user-code workaround.
Requires Documentation Change
N/A
How Has This Been Tested?
LeggedInOptionStrategiesMarginCallRegressionAlgorithm(C# + Python): legs into two spreads, forces a margin call, asserts full liquidation. Reproduces the crash before the fix, passes after, in both languages.OptionStrategyPositionGroupBuyingPowerModelTests: new per-leg fallback tests for a degenerate group and an unmargined strategy — red before the fix, green after.OptionStrategiesTests: newOptionLegDataconstructor tests, including construction from Python withdateanddatetime.QuantConnect.Tests.Common.Securitiessuite: 5502 passed, 0 failed.AlgorithmTradingTests: 189 passed. Related option strategy regression algorithms all passed.Types of changes
Checklist:
bug-<issue#>-<description>orfeature-<issue#>-<description>