Skip to content

Enumerate option positions in a deterministic order - #9825

Merged
jhonabreul merged 2 commits into
QuantConnect:masterfrom
jhonabreul:bug-9648-deterministic-option-strategy-matching
Sep 24, 2026
Merged

jhonabreul merged 2 commits into
QuantConnect:masterfrom
jhonabreul:bug-9648-deterministic-option-strategy-matching

Conversation

@jhonabreul

@jhonabreul jhonabreul commented Sep 23, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Identical option holdings could be grouped into different strategies across runs, so the same book could require different margin in different backtests.

Cause: OptionPositionCollection enumerated positions in ImmutableDictionary hash order, and Symbol hashes include the ticker's string hash, which .NET randomizes per process.

The fix:

  • OptionPositionCollection.GetEnumerator yields positions in a fixed order: underlying first, then expiration, strike and right, with the symbol ID as the final tie-break.
  • The sorted positions are cached per collection, which is immutable, so each instance sorts at most once.

Related Issue

Closes #9648

Motivation and Context

Backtests must be reproducible, and buying power checks depend on these groups, so an order could be accepted in one run and rejected in another.

Requires Documentation Change

No.

How Has This Been Tested?

  • OptionStrategyMatcherTests.MatchesIdenticalBooksIdenticallyRegardlessOfSymbolHashes: matches the same book under 100 tickers and asserts a single grouping. It fails without the fix.
  • A local run including the StrategyMatcher and OptionStrategyPositionGroupBuyingPowerModelTests fixtures: 3,220 passed, 0 failed.
  • Option strategy and combo order regression algorithms (85 C#, 15 Python): 100 passed, no expected statistics changed.
  • End-to-end resolver benchmark on 4 to 40 contract books: no measurable difference against master.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • Refactor (non-breaking change which improves implementation)
  • Performance (non-breaking change which improves performance. Please add associated performance test and results)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Non-functional change (xml comments/documentation/etc)

Checklist:

  • My code follows the code style of this project.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • My branch follows the naming convention bug-<issue#>-<description> or feature-<issue#>-<description>

Option strategy matching took the first match in the position collection's
hash order, which depends on per-process string hashes, so identical holdings
could be grouped differently, with different margin, across runs.
@jhonabreul
jhonabreul merged commit c3a346f into QuantConnect:master Sep 24, 2026
8 checks passed
@jhonabreul
jhonabreul deleted the bug-9648-deterministic-option-strategy-matching branch September 24, 2026 13:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Option strategy matching is not deterministic across processes, changing position groups and margin for identical holdings

2 participants