Repository navigation
Conversation
…flag - the opt-out has to be a protected virtual on the base brokerage, which the transaction handler cannot read; the sync moves next to the cash sync, where the brokerage already receives the algorithm once a day, and raises the option notification the handler already turns into an exercise or an assignment - IB reports every option position change from its portfolio stream and its holdings read is empty after a reconnect, so it sets the flag to false once a NuGet with it ships; every other brokerage keeps the sync as today
Romazes
force-pushed
the
bug-option-holdings-sync-in-brokerage
branch
from
November 4, 2026 17:59
df9359b to
e092fc4
Compare
4 of 11 tasks
- a protected flag next to the sync was one switch too many; a brokerage that reports every option position change itself now overrides PerformOptionHoldingsSync and returns true, the same shape as PerformCashSync - PerformCashSync ignores the result, so a failed holdings read is retried the next day and never counts as a failed cash sync, as before
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
Lean #9937 added a daily option holdings sync. Once a day, with the cash sync, Lean reads
GetAccountHoldings()and books an exercise or an assignment for every option the algorithm holds and the brokerage list leaves out. A brokerage that reports every option position change itself gains nothing from that read, and an empty read books positions that are still open.Interactive Brokers is such a brokerage: it raises
OptionNotificationfrom every portfolio row. ItsGetAccountHoldings()is also empty after any reconnect (QuantConnect/Lean.Brokerages.InteractiveBrokers#289), so the sync after the nightly gateway restart booked a short SPXW put as expired while IB still held it. This happened on two sessions of one real-money deployment:The change follows the cash sync pattern:
BrokerageTransactionHandlerintoBrokerage.PerformOptionHoldingsSync(IAlgorithm algorithm, DateTime currentTimeUtc), apublic virtual boollikePerformCashSync, called fromPerformCashSynconce a day. The code is the same; a contract the brokerage no longer lists is raised as anOptionNotificationwith position 0, the event the handler already turns into an exercise or an assignment. It returns false only when the holdings read throws;PerformCashSyncignores the result, so a failed read is retried the next day and never counts as a failed cash sync, as before.HoldingsSyncEnabledflag from fix: let a brokerage opt out of the daily option holdings sync #10028 is removed;IBrokeragedoes not change.PerformCashSync, in Lean or in the brokerage plugins, so every brokerage keeps the sync as today. The IB plugin overridesPerformOptionHoldingsSynconce a NuGet with this change is out.Related PR(s)
#9937, #10028 (merged: the first shape, a flag read by the handler)
Related Issue
QuantConnect/Lean.Brokerages.InteractiveBrokers#289
Motivation and Context
On IB the sync only did harm. The algorithm traded as if the put were gone: IB rejected the next short put for margin, and the ETF sells at the close were sized without the put. The plugins that raise no option events (Schwab, Alpaca, Tastytrade, Tradier, TradeStation, WeBull) keep the sync.
Requires Documentation Change
No.
How Has This Been Tested?
BrokerageTransactionHandlerTests.ProcessSynchronousEventsSkipsTheOptionHoldingsSyncWhenTheBrokerageOverridesIt: a short SPY call the brokerage does not list. With the base method the sync books the assignment (1 ticket, option 0, underlying -600). With the test brokerage overriding the method away nothing is booked (0 tickets, option -7, underlying 100). The three sync tests from #9937 are unchanged; they driveProcessSynchronousEvents(), which now reaches the sync through the basePerformCashSync.Types of changes
Checklist:
bug-<issue#>-<description>orfeature-<issue#>-<description>🤖 Generated with Claude Code