Repository navigation
Use the executable spread side for IB option market order fees - #10178
Conversation
There was a problem hiding this comment.
Approving. The fix is the smallest one that gives the issue's result: GetPotentialOrderPrice now takes the ask for buys and the bid for sells for market, market on open, market on close and combo market orders, which is the side those orders fill at in Lean's fill models and the side every other quote-based fee model already uses (CoinbaseFeeModel, BinanceFeeModel, BitfinexFeeModel, KrakenFeeModel, FTXFeeModel, AlphaStreamsFeeModel; BybitFeeModel prices its sells from the bid too). The reversed mapping dates back to #7436, and nothing in that pull request explains it as a deliberate choice.
What I checked:
GetPotentialOrderPriceis only called for US equity and index options (InteractiveBrokersFeeModel.cs:118), so futures, equities and forex fees are untouched; the limit, stop and combo limit branches don't change.- The new
USAOptionMarketFeeUsesExecutableSideOfTheSpreadcases cross both premium thresholds of the default tier in both directions for all four order types, and fail on master by inspection (e.g. a buy at 0.09/0.11 gets the 0.50 rate from the bid). TheUSAOptionFeeedit to quote on the ask keeps that test about buys; the 24 failures reported with the fix reverted add up (16 new cases plus the 8 market-type buys at 0.07 and 0.2). - The regression statistics only change fees, end equity and, for the two null-margin algorithms, turnover; the bull call spread's fees rising and the bear call spread's falling is what moving buys to the ask and sells to the bid predicts. The Python
NullMarginMultipleOrdersRegressionAlgorithmshares the C# statistics. - CI is green on this commit (API Tests skipped by its workflow). An end-to-end backtest is described with its outcome.
- Nothing private in the diff, commits or description.
Other repositories: the Documentation premium table is updated in QuantConnect/Documentation#2923, which matches this change. No stubs or API changes are needed.
Label: risk:low. It's a two-line bug fix that's easy to revert, makes the estimated commission match the price the order fills at, leaves every path that's right today as it was, and has tests that fail without it, green CI and an end-to-end run.
There was a problem hiding this comment.
Requesting changes for one caller interaction: the quote-side correction is right, but DefaultOptionAssignmentModel.EstimateArbitragePnL values a hypothetical option sale at the bid while constructing a buy to estimate its fee. The corrected buy fee can therefore cause a new early assignment, not merely change commissions. The inline finding contains the base/head reproduction. Please address the sale-side estimate and cover the assignment decision, or obtain a maintainer decision on accepting that limitation rather than silently widening #10139's behavior.
Verification on this head:
dotnet test Tests/QuantConnect.Tests.csproj -c Release --filter FullyQualifiedName~InteractiveBrokersFeeModelTests: 156 passed. Restoring only the original reversed quote selection gives 24 failures, including all 16 new cases; restoring this head gives 156 passed again.- All six affected regression cases passed, including the Python null-margin case, with the edited fees, equity and turnover statistics.
- Tested end to end: 20 SPY option market fills across both premium thresholds charged the ask-based rate for buys and bid-based rate for sells, with all assertions passing. This verifies the intended fee correction, not the assignment interaction.
- A separate .NET 10 assignment reproduction returned quantity 0 with the base fee model and 10 with this head. Correcting only the assignment simulator's hypothetical order to a sale restored quantity 0.
- Lean CI is green; API Tests is skipped by its workflow.
Cross-repository check: QuantConnect/Documentation#2923 correctly updates all four affected premium-table rows; no further documentation or API/stub update is needed for the intended quote-side correction. The coordinated change remains pending the Lean assignment interaction.
Set risk:high on both PRs because the current implementation newly changes simulated assignment outcomes outside the requested fee correction. Existing unrelated option-fee limitations are tracked separately and are not additional blockers here.
|
Addressed the requested change: the default option assignment model now estimates the fee of the counter-party's hypothetical close as a sale, so the corrected buy-side fee can no longer trigger an assignment the base branch wouldn't. New assignment test covers the reproduced case; the description lists the results. The fee correction itself is unchanged. |
There was a problem hiding this comment.
Requesting changes. The fee-model fix from my earlier approval still holds. The new commit (24624d8) fixes the IB assignment case, but it causes the same kind of unwanted assignment for Tastytrade users, so this needs a person to decide the approach.
Same cause, different fee model. EstimateArbitragePnL builds the counter-party's hypothetical close, but it passes that order to the fee model with the algorithm's security, and that security holds the algorithm's short position. InteractiveBrokersFeeModel reads only the order direction (to pick the bid or ask), so it needs a sale. TastytradeFeeModel reads the direction against Security.Holdings (TastytradeFeeModel.cs:59, BrokerageExtensions.GetOrderPosition). A sale against short holdings counts as sell-to-open and pays $1 per contract. On master, a buy against short holdings counts as buy-to-close and is free. So whichever sign the simulator picks, one of the two models gets a wrong close fee.
I reproduced it with the new test's setup, changing only option.FeeModel = new TastytradeFeeModel() (bid 0.09, ask 0.11, short 10):
- this head: assignment quantity 10
- the same head with only
DefaultOptionAssignmentModel.csreverted to master: 0 - bids of 0.095 and 0.10 give 0 in both.
The IB fee change doesn't reach the Tastytrade path, so this new early assignment comes from this commit alone. Among Lean's fee models, only IB and Tastytrade depend on direction for options.
As far as I can see, no sign choice in the simulator gives the master assignment results for both models. That leaves two limitations to choose between, or a wider change:
- Keep the simulator as on master (drop 24624d8). An IB short option whose bid and ask straddle a premium threshold can then be newly assigned, which was the earlier reviewer's finding.
- Keep 24624d8. A Tastytrade short option can then be newly assigned, as shown above.
- Estimate the close fee against the counter-party's position instead of the algorithm's holdings. This is wider than #10139 and changes how the simulator calls the fee models.
A QuantConnect member should pick one. Whatever they pick, the assignment tests should cover both fee models.
What I checked on this commit:
- CI is green (API Tests skipped by its workflow).
DefaultOptionAssignmentModelTestspass on this head. The new test fails with master's assignment model, as described.- Nothing private in the diff, commits or description.
Other repositories: QuantConnect/Documentation#2923 still matches the fee-side change, and no stubs or API changes are needed. It waits on this decision.
Label: risk:high stays on both pull requests. The change now alters simulated early-assignment outcomes for Tastytrade, beyond the fee correction #10139 asks for, and the approach is still open.
|
The reviewer is right, and I confirmed it from the code: I'm not patching this again until a QuantConnect member picks the approach for
Whichever is chosen, I'll add assignment test cases for both the IB and Tastytrade fee models. The fee-model fix itself (ask for buys, bid for sells) and the Documentation table are unaffected by this choice. |
There was a problem hiding this comment.
Requesting changes. Pricing the fees against the counter-party's long position (the approach chosen in the new commits) fixes what I raised last time: Tastytrade now sees a sell-to-close, and the IB and Tastytrade assignment tests pass. The IB quote-side fix still holds. Two problems remain in the new commits, and both are reproduced locally.
-
The stand-in option has no local time, so a custom fee model that reads it now stops the backtest.
CreateCounterPartyOptionbuilds a newOptionthat never gets aLocalTimeKeeper. If a fee model readsparameters.Security.LocalTime, the call now throwsInvalidOperationException("SetLocalTimeKeeper must be called before using LocalTime"). Nothing catches it on the way fromBacktestingBrokerage.Scan→ProcessAssignmentOrders, so the algorithm dies with a runtime error. I added a test with a fee model that readsSecurity.LocalTimeand a short deep-ITM call 3 days before expiry. It throws on this head and passes with master'sDefaultOptionAssignmentModel.cs. The stand-in should carry the option's local time. Don't do that by callingSetLocalTimeKeeperon it, though: that also rewires the cache it shares with the real option. Please add a test that covers it. -
Tastytrade charges $5 twice for one exercise.
BacktestingBrokeragecalls the fee model once for each fill whose fee is zero (BacktestingBrokerage.cs:369-379). An ITM physically settled exercise has two fills, the option and the underlying, and both pass the option security and the same order. I ranOptionExerciseRegressionAlgorithmwithTastytradeFeeModelset on every security. The singleExerciseOptionof 20 puts filled as two events at $5 each, so it cost $10. The schedule is $5, and master charged $0. The fee model can't tell the two fills apart, so the fix needs one of these: charge the exercise fee only on the option fill in the backtesting brokerage, which also changes every other fee model that charges exercises, or leave the flat fee out of this pull request. A person should pick; the test should cover a full exercise, not just a directGetOrderFeecall.
CI is green on this commit (one build job skipped by its workflow). Nothing private in the diff, commits or description.
Other repositories: QuantConnect/Documentation#2923 still matches the IB fee change. The Tastytrade exercise fee may need a line in the Tastytrade fee model docs.
Label: risk:high stays. The change now also alters Tastytrade exercise fees and how the assignment simulator calls every fee model, and both regressions above are live on this head.
The Interactive Brokers fee model estimated a market, market on open, market on close or combo market option order's premium from the bid for buys and the ask for sells. Buys execute at the ask and sells at the bid, so when the spread straddles a premium threshold the commission was taken from the wrong tier. Use the ask for buys and the bid for sells, and update the regression statistics whose fees change. Closes #10139 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The default option assignment model compares the counter-party closing its long option position at the bid with exercising it, but estimated that close's fee with a buy order. Now that IB option market buys are priced at the ask, that buy could fall in a higher commission tier and trigger an early assignment the sale does not justify. Estimate the fee with the sale it models. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Tastytrade's fee schedule charges $5 for an option exercise or assignment, whatever its size. The fee model had no case for exercise orders, so it charged them the per-contract opening or closing rate depending on the holdings. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The assignment simulator models the counter-party closing or exercising its long contracts, but passed the algorithm's option, holding the short side, to the fee model. Fee models that depend on the position, such as Tastytrade's, then saw a sale as opening a position. Estimate both fees against a stand-in of the same option type that shares the option's prices and holds the counter-party's long contracts. The exercise order now carries the holder's change in position, as Lean's exercise orders do. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The backtesting brokerage asked the fee model for every fill of an exercise order without a fee. An exercise of a physically settled option fills both the option and the delivered underlying, and both calls got the option security and the same order, so the exercise was charged twice. Delivering the underlying is part of the exercise: only ask the fee model for the option's fill. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The counter-party option was a new security that never got a local time keeper, so a fee model reading Security.LocalTime threw and stopped the backtest. Copy the option instead, with its own holding of the counter-party's position: it keeps the option's type, local time, prices and models without rewiring the cache it shares. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With the double-counted contract unit removed from the exercise estimate, the test's underlying quote no longer put exercising and selling close enough for the close fee's commission tier to decide the assignment. Move the underlying bid so that it does again. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
dc61fd6 to
f563da6
Compare
There was a problem hiding this comment.
Approving. Both problems from my last review are fixed in the new commits, and the IB quote-side fix still holds.
- Counter-party option without local time: fixed in 5d9356a.
Option.WithHoldingscopies the option withMemberwiseCloneand gives the copy its ownOptionHolding. The copy keeps the real option's type, local time keeper, cache and models, and the real option's holdings stay as they were.EstimatesFeesAgainstTheCounterPartyPositioncoversOption,IndexOptionandFutureOptionand checksLocalTime. It fails with master's assignment model. - Tastytrade charging $5 twice for one exercise: fixed in ddc2e11 by charging only the option's fill in
BacktestingBrokerage.OptionExerciseRegressionAlgorithmwithTastytradeFeeModelnow charges the exercise $5 once, on the option fill (Total Fees $25 = $20 to open + $5), where my last run showed $30.ChargesTheExerciseFeeOnceForAFullExerciseruns a full exercise through the brokerage and fails without the brokerage change. The schedule's $5 per exercise or assignment matches tastytrade's published fee schedule.
What I checked on this head:
- The new tests (
TastytradeFeeModelTests,DefaultOptionAssignmentModelTests,InteractiveBrokersFeeModelTests,BacktestingBrokerageTests) pass: 206 tests. With master'sBacktestingBrokerage.csandDefaultOptionAssignmentModel.cs, 5 fail: the full-exercise test, the IB assignment case and the three counter-party cases. The Tastytrade assignment case passes on master too, as it should, because master's buy-to-close was free. - With the exercise order's quantity now negative (the holder's change in position), only quantity-aware fee models see it, and IB's and AlphaStreams' exercise branches don't depend on the sign.
- Only the exercise's underlying fill skips the fee model. Fee models that already charged exercises (IB futures options, custom models) are now charged once instead of twice for a physically settled exercise, and CI's regression tests pass with unchanged statistics.
- Nothing private in the diff, commits or description.
Other repositories: QuantConnect/Documentation#2923 (the IB premium table) is merged. The Tastytrade fee model page (17 Tastytrade Model.html) still needs a line for the $5 option exercise and assignment fee.
Label: risk:high stays. Beyond the IB fee fix, this changes how the assignment simulator calls every fee model, and the backtesting brokerage's exercise fees for every fee model that charges exercises. The tests and regression runs back those changes, but the blast radius is wide.
There was a problem hiding this comment.
Requesting changes for one remaining counter-party snapshot regression: replacing the holding loses its market price, so custom fee models can estimate zero notional and change assignment decisions. Details and the base/head reproduction are inline. My earlier sale-side assignment finding is fixed.
Verification:
- The IB, Tastytrade, default-assignment and backtesting-brokerage fixtures pass: 206 tests.
- Option, expiry, exercise, assignment and null-margin regression algorithms pass: 300 cases.
- Tested end to end: opening 20 Tastytrade option contracts costs $20; exercising them charges $5 on the option fill and $0 on the delivered underlying, for $25 total.
- A separate custom proportional-fee reproduction assigns 10 contracts with the current base assignment model, but 0 with this head because the copied holding reports price 0. Synchronizing its market price restores quantity 10. No built-in fee model currently reads this holding-price state; the regression affects custom models using it.
The Tastytrade fee-model page in QuantConnect/Documentation still needs the newly added $5 exercise/assignment fee documented.
The copy's new holding only got the counter-party's quantity and average price, so its Price, HoldingsValue and GetQuantityValue were zero, and a fee model valuing the order through the holding estimated no fee. Give it the option holding's market price. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Closes #10139
Description
InteractiveBrokersFeeModel.GetPotentialOrderPricepriced market, market on open, market on close and combo market option orders at the bid for buys and the ask for sells. When the spread crosses a premium tier ($0.05 or $0.10), the fee came from the wrong tier: a 10-lot buy at 0.09/0.11 cost $5.00 instead of $6.50. Buys now use the ask and sells the bid.DefaultOptionAssignmentModelnow prices the counter-party's sale and exercise against a copy of the option (Option.WithHoldings) that holds their long contracts. Fee models that read the position, such as Tastytrade's, then see closing trades. The copy keeps the option's type, prices, local time and models.TastytradeFeeModelcharges $5 per option exercise or assignment, per its fee schedule, instead of its per-contract open or close rate.BacktestingBrokeragepriced both fills of a physically settled exercise, the option and the delivered underlying, with the option and the same order. Only the option's fill is charged now. Fills from expirations and assignments are still emitted without fees.The simulator's estimates change for Tastytrade, whose exercise now costs $5, and India, whose close now pays sell-side charges. QuantConnect/Documentation#2923 updates the IB premium table.
Tests
USAOptionMarketFeeUsesExecutableSideOfTheSpread: IB, 16 cases, all fail on master.ChargesAFlatFeeForOptionExercisesAndAssignmentsandChargesTheExerciseFeeOnceForAFullExercise: Tastytrade.EstimatesFeesAgainstTheCounterPartyPosition:Option,IndexOptionandFutureOption.DoesNotAssignWhenSellingAtTheBidBeatsExercising: IB and Tastytrade. The IB case assigns with master's simulator.🤖 Generated with Claude Code