Skip to content

Size margin call orders for the deficit earlier liquidations leave - #10131

Merged
Martin-Molinero merged 8 commits into
masterfrom
kronos/bug-10107-size-margin-calls-remaining-deficit
Nov 18, 2026
Merged

Martin-Molinero merged 8 commits into
masterfrom
kronos/bug-10107-size-margin-calls-remaining-deficit

Conversation

@quantconnect-kronos

@quantconnect-kronos quantconnect-kronos Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Description

DefaultMarginCallModel sized every position group's margin call order from the group's full current holdings, and ExecuteMarginCall executed each one as generated until the deficit was covered. So:

  • When the open margin call orders covered part of the deficit, or an earlier order of the same call did, the next group was still liquidated for the whole deficit.
  • A group whose holdings were partly being sold by an open order was dropped instead of sized from what's left, which could leave the deficit uncovered until that order closed.
  • Groups sharing a security, such as a covered call and a separate stock group, were each checked against the open orders but not against each other.

Approach, as decided by a maintainer on this pull request (option C): generation returns one alternative per group, and execution sizes each one for what the earlier ones leave, so a failed order falls through to the next group within the same call, as on master.

GetMarginCallOrders:

  • Returns one request per position group, as before. The groups are resolved from the holdings the open margin call orders leave, regrouping the impacted positions, as it already did to compute the margin those orders release. Each is sized for the deficit the open orders leave.
  • In GenerateMarginCallOrders, a group held as is gets sized by its buying power model, as before, with its lots capped at its own quantity, since the security's buying power model sizes from all of the security's holdings, which other groups can share. A group that only exists once the open orders fill, such as naked calls left from covered calls whose stock is being sold, is liquidated for the fewest of its own lots whose margin covers the deficit, up to all of them.
  • Skips orders that would buy back holdings an open order sells beyond what's held, such as an OnMarginCall request larger than the position.

ExecuteMarginCall:

  • Executes the first group as given. Once an earlier order of the same call is filled or open, each later group is scaled down to the fewest of its lots that cover the deficit the earlier orders leave, and never scaled up. A rejected or canceled order frees no margin, so after failed orders alone the next group is executed as generated for the whole deficit, as on master. A combo is scaled with a new group order manager for the new quantity. Requests without quantity are executed as given.
  • A group is scaled down only if every leg of the smaller order passes DefaultBrokerageModel.IsValidOrderSize, the symbol's minimum order size. Otherwise the requested orders are executed, as on master. Nothing waits on a rejection.
  • Requests OnMarginCall adds or changes are only ever reduced, so padding on the first group stays.
  • Margin call orders that closed without filling (Invalid or Canceled) are kept until the margin call ends, when margin remaining is back above zero. Groups are executed in order of how many of their margin call orders failed during this margin call, then losers first. A failed order counts against the group with exactly its symbols, so a rejected covered call combo doesn't push back a separate stock group on the same underlying. Nothing is excluded, so nothing needs resetting. This also covers an order that stays open through a scan and is canceled afterwards.

IMarginCallModel, the engine's exchange-open check and OnMarginCall are unchanged. With no open margin call orders and the first group's order covering the deficit, the orders executed are the same as before.

Maintainer decisions

  • Option C: generation returns one request per position group, and ExecuteMarginCall scales later groups down to what the earlier orders leave, with failed groups executed last (Size margin call orders for the deficit earlier liquidations leave #10131 (comment) and the reply after it).
  • The scaled size is checked before it's sent: a scaled-down group is executed only if every leg passes DefaultBrokerageModel.IsValidOrderSize, otherwise the requested orders are executed. There's no fallback after a rejection. The protective-put buying-power rejection of a partial close is Partly closing an option strategy can fail the buying power check #10179, which this pull request merges after. A brokerage rejecting a scaled order for a reason Lean doesn't model counts as a failure of that group, which goes last on the next margin call.

Related Issue

Closes #10107

How Has This Been Tested?

New tests in MarginCallModelTests, all failing on master:

  • SizesEachPositionGroupForTheDeficitAndHoldingsTheEarlierLiquidationsLeave: 100 IBM (the loser) and 100 MSFT at $100, $4,000 deficit. Both get -81, and only IBM's is executed, which covers it. With it open, MSFT falls to $45 leaving a $2,700 deficit: MSFT -100 and the 19 IBM shares left are generated, and IBM is scaled to -9 for the $450 MSFT leaves.
  • LiquidatesTheStockOfACoveredCallWhoseCallIsBeingBoughtBack: 3 covered calls, OnMarginCall replaces the combo with an order per leg, and the stock order is cancelled while the call buyback stays open. The 300 shares regroup into a stock group and are liquidated (-300).
  • LiquidatesPositionGroupsThatAreNotHeldForTheDeficit: 3 covered calls, OnMarginCall sells only the stock, which stays open, and cash drops to -$29,000:
    • stock order -300: the 3 calls left are naked, $3,300 of margin against a $2,600 deficit: all 3 are bought back (+3).
    • stock order -150: a covered call, 50 shares and 2 naked calls against a $9,000 deficit. Executing them, the covered call (-100 / +1, $5,000) and the 2 naked calls (+2, $2,200) go as generated and the shares are scaled to -36 ($1,800).
  • LiquidatesTheNextPositionGroupForTheWholeDeficitWhenAMarginCallOrderFails, rejected and canceled: IBM, MSFT and SPY can each cover the $4,000 deficit. IBM's and MSFT's orders fail, and SPY is liquidated in the same call as generated (-81).
  • LiquidatesAStockGroupSharingTheUnderlyingOfARejectedComboOrder: a covered call (the loser) and a 200 SPY group, $4,100 deficit, and combo orders are rejected. The stock group is liquidated in the same call as generated (-83), as on master.
  • ExecutesTheRequestedOrderWhenTheScaledDownOneIsBelowTheMinimumOrderSize: 100 IBM (the loser) and 100 MSFT with a 25-share minimum order size, whose orders the order processor checks with DefaultBrokerageModel.IsValidOrderSize. Selling IBM leaves $1,000, the 20 MSFT shares that cover it are below the minimum, so the requested MSFT order is executed and nothing is rejected.
  • ExecutesThePositionGroupsWhoseMarginCallOrdersFailedLast: IBM's order is rejected and MSFT's covers the deficit but is canceled after a scan saw it open. The next call executes SPY first, then IBM and MSFT. Once all of them failed again, SPY (one failure) goes before IBM and MSFT (two each). After margin remaining is back above zero, the next margin call starts with IBM, the loser, again.
  • ExecutesLaterMarginCallRequestsWithoutQuantityAsGiven: a later request without quantity, as OnMarginCall could make, is submitted as given.
  • LiquidatesAllTheSharesOfAStockGroupSharingTheSecurityWithAnotherGroup: a covered call and a 200 SPY group with no equity left. The stock group is liquidated with its own 200 shares (-200), not sized from all 300 SPY and then dropped.
  • LiquidatesTheShortPositionsLeftAfterOpenMarginCallOrders: without option strategy grouping, 3 short calls ($1,100 each). With a +1 buyback open and $500 of equity, the 2 left are both bought back (+2).
  • DoesNotSellMoreThanHeldAcrossPositionGroupsSharingALeg: covered call plus 200 SPY, the combo liquidation cancelled with the -50 stock order open, and SPY falls to $80. The 250 shares left regroup into a covered call and a 150 SPY group. The stock group, whose orders haven't failed, is executed first (-150), then the combo (-100 / +1): 300 shares with the open order, all of those held.

Updated tests:

  • LiquidatesPositionGroupsSharingALegForTheDeficitOpenMarginCallOrdersLeave: the stock group's executed order is -50 for the $2,500 the combo leaves. With the stock order open and the combo cancelled, both groups left are generated for the $5,000 left.
  • GeneratesMarginCallOrdersForTheDeficitOpenMarginCallOrdersDoNotCover: MSFT is executed as -20 for what selling all of IBM leaves, so the later numbers change accordingly.
  • StopsExecutingMarginCallOrdersOnceTheOpenOnesCoverTheDeficit: IBM and MSFT are generated, and only IBM's, which stays open and covers the deficit, is executed.
  • FakeOrderProcessor gained RejectsOrder, checked with the requests of all of an order's legs once they're submitted, and RejectedOrderStatus, to simulate rejections.

Full test suite, Release build, with the CI filter: Passed: 39610, Skipped: 100, Failed: 1. The failure is FutureSymbolGeneratorTests.GeneratesFutureSymbolWithCorrectExpiryDateOverWiderDateRange, a NullReferenceException in FuturesExpiryUtilityFunctions.GetExpirationHolidays, which doesn't touch margin calls and passes when run again. The margin call regression algorithms (MarginCallEventsAlgorithm, OptionStrategyMarginCallEventsAlgorithm, the short ITM expiry ones and the others using margin calls, C# and Python) pass with unchanged statistics.

Tested end to end with a backtest holding SPY, AIG, GS and GE on daily data at 3.8x leverage, September 22 to October 31, 2008, against master:

  • No failures: the first margin call executes all of AIG plus GE -225 for what AIG leaves, where master executed AIG plus GE -1000.
  • A brokerage model rejecting AIG's margin call orders: on the first margin call AIG is rejected and GE -1000 is executed in the same call for the whole deficit, as on master. Later margin calls execute AIG, whose orders failed, last, and the other stocks are scaled for what the earlier ones leave.
  • Rejecting both AIG's and GE's: the first margin call falls through both to SPY -971 in the same call; later ones liquidate SPY and GS before retrying AIG and GE.
  • No position went short in any run.

Also tested end to end with the Bitfinex brokerage model on a margin account, BTCUSD and ETHUSD minute data on December 14, 2022: BTC (the loser) and ETH held, with a deficit that BTC's liquidation covers but for $0.20. BTC's order fills. Scaled for the $0.20 left, ETH would be -0.00077191, below Bitfinex's 0.0008 minimum, so the requested -0.01 is sent and fills in the same margin call, with no rejected order, leaving positive margin remaining. On e6845f5 the same run leaves the deficit uncovered for two hours of further small margin calls.

A documentation update for the new default behavior is in QuantConnect/Documentation#2915.

GetMarginCallOrders sized every position group's order for the whole
deficit left after the open margin call orders, and from its full current
holdings. ExecuteMarginCall stops once the open orders cover the deficit,
but when they covered most of it the next group still got an order for all
of it, and a group whose holdings were partly being sold was dropped rather
than sized from what's left, leaving the deficit uncovered.

Position groups are now resolved from the holdings left after the open
margin call orders, ordered losers first as they are executed, and each one
is sized for the deficit the open orders and the earlier groups' orders
leave, never for more lots than it holds. Generation stops once the deficit
is covered. Orders that would buy back holdings other liquidations sell
beyond what's held are skipped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@quantconnect-kronos quantconnect-kronos Bot added kronos:develop:working develop: in progress kronos:review Request review labels Nov 13, 2026
@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot added kronos:review:queued review: queued kronos:review:working review: in progress and removed kronos:review Request review kronos:review:queued review: queued labels Nov 13, 2026

@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requesting changes: one sizing defect in the new regrouped path.

CI is green, and the new and updated tests pass locally too.

Finding: a regrouped option strategy that isn't held gets one lot too few, so the deficit stays uncovered. GenerateMarginCallOrders now gets position groups that only exist after the open orders fill. PositionGroupBuyingPowerModel.GetMaximumLotsForDeltaBuyingPower and GetMaximumLotsForTargetBuyingPower size from Portfolio.Positions[parameters.PositionGroup.Key], the held group with that key. When no held group has that key, the indexer returns an empty group. The result is then the number of lots whose margin fits inside the deficit, rounded down, not the number needed to release it. The new Math.Min cap only limits overselling. It can't raise an undersized result. (For the security model and for a key that is held, such as 2 covered calls left from 3, the sizing works with the cap.)

I reproduced it locally with GetSpyPortfolio(orderProcessor, -26000, -3) (3 covered calls, $0 buffer). OnMarginCall splits the combo into SPY -300 and +3 calls. The call buyback is cancelled and the stock order stays open. Then cash drops to -$29,000:

  • TPV is $700. Margin after the open order is $3,300, all of it for the 3 now-naked calls at $1,100 each, which leaves a $2,600 deficit.
  • The branch generates +2 calls. That releases $2,200 and leaves $400 uncovered until a later scan, which on daily data is the next bar.
  • The same deficit on a portfolio that holds the 3 naked calls gives +3, which is correct.
  • With the stock order at -150 instead, the regroup is 1 covered call, 50 SPY and 2 naked calls, against a $9,000 deficit. The branch generates the combo, -50 SPY and +1 call, and leaves a naked call and $400 of the deficit uncovered.

Master generates nothing in these cases, so the branch is still better than master. But they're the cases the issue asks this change to cover ("the covered call group's request is dropped because of its call leg, though 300 shares still need selling"), and sizing for the deficit the earlier liquidations leave is the point of the change. A regrouped group needs to be sized against itself, not against a held group with its key or an empty one. Please add a test where the regrouped strategy isn't held, like the one above, and check that its order covers the deficit.

Other checks, no change needed:

  • Groups partition the holdings, and every order is capped to its group's quantity. The running liquidatingQuantities check therefore only rejects genuine oversells, such as an OnMarginCall request larger than the position, and the sign check stops those from being bought back.
  • The losers-first order here matches ExecuteMarginCall's, which uses the same per-symbol UnrealizedProfit sums, including for groups that share a leg.
  • Cost: each scan now runs one more ResolvePositionGroups call, plus one for each accepted group until the deficit is covered. It only runs while margin remaining is <= 0, and it replaces a GenerateMarginCallOrders call per group, so it's fine.
  • OnMarginCall now receives only the liquidations needed, not one per group. The description says so, and the documented use, adjusting or choosing among the requests, still works. The default behaviour matches ExecuteMarginCall's, since it already stopped once the deficit was covered.

Other repositories: none need an update. The documentation's OnMarginCall examples still apply.

Risk: I set risk:high. The change alters which liquidation orders users' OnMarginCall receives and how much margin calls liquidate (money), and the defect above shows the new path can still under-size.

@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot added risk:high High risk and removed kronos:review:working review: in progress labels Nov 13, 2026
The holdings left after open margin call orders can regroup into a
position group that isn't held, such as naked calls left from covered
calls whose stock is being sold. The buying power model sizes a group
from the held group with its key, or an empty one, so such a group got
too few lots and part of the deficit stayed uncovered. These groups are
now liquidated for the fewest of their lots whose margin covers the
deficit. Held groups are sized by their buying power model as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@quantconnect-kronos

Copy link
Copy Markdown
Contributor Author

Addressed the review in 5e74b88:

  • A position group that only exists once the open margin call orders fill, so isn't held, is now sized against itself: the fewest of its own lots whose margin covers the deficit, up to all of them. Held groups are still sized by their buying power model, so a margin call without open orders behaves as before.
  • New LiquidatesPositionGroupsThatAreNotHeldForTheDeficit, with the reproduction from the review. With the stock order at -300 the 3 naked calls are bought back (+3, the $2,600 deficit at $1,100 each). With it at -150, the covered call (-100 / +1), the 2 naked calls (+2) and -36 shares cover the $9,000 deficit. Both cases fail on the previous commit.
  • Partly sold single-security groups now use the same sizing, so in SizesEachPositionGroupForTheDeficitAndHoldingsTheEarlierLiquidationsLeave IBM is -9 for the $450 left rather than -10.

Full suite, Release, CI filter: Passed: 39603, Skipped: 100, Failed: 0. The end-to-end backtest gives the same orders as before, since its margin calls have no open orders. The description is updated.

@quantconnect-kronos

Copy link
Copy Markdown
Contributor Author

The failing check is the Benchmarks workflow, which failed twice: once on the first run and once on its re-run. I don't think this change causes it.

  • First run: the re-check failed EmptyFuturesBenchmark (C#, 388k vs a minimum of 424k) and EmptySingleSecuritySecondEquityBenchmark (C#, 267k vs 297k). The first pass of that same job had measured them at 471k and 330k.
  • Re-run: EmptySingleSecuritySecondEquityBenchmark passed (327k vs 268k). EmptyFuturesBenchmark failed at 395k vs 401k, after its own first pass in that job measured 446k. All the Python benchmarks passed.

These algorithms subscribe to data and never place an order, so they never use margin. GetMarginCallOrders returns as soon as TotalMarginUsed <= 0, before any code this change touches. The changed sizing only runs when margin remaining is zero or below. The same algorithm swings by more than 10% between passes of the same job, and which benchmarks fail varies between runs. That points to the runner's throughput, not the code. The build itself has no errors, and every other check passes.

@quantconnect-kronos quantconnect-kronos Bot added the kronos:review Request review label Nov 13, 2026
@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot added kronos:review:queued review: queued kronos:review:working review: in progress and removed kronos:review Request review kronos:review:queued review: queued labels Nov 13, 2026
@quantconnect-kronos-reviewer
quantconnect-kronos-reviewer Bot marked this pull request as ready for review November 13, 2026 17:17

@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving 5e74b88. It resolves my earlier finding, and I found nothing new.

The earlier finding is fixed. A position group that isn't held as is gets sized against itself now: the fewest of its own lots whose margin covers the deficit, found by a binary search. Held groups still go through their buying power model, so a margin call without open orders behaves as before. LiquidatesPositionGroupsThatAreNotHeldForTheDeficit covers both of my reproductions (+3 calls for the -300 stock order; the covered call, +2 calls and -36 shares for -150). It passes locally with the rest of MarginCallModelTests.

What I checked in the new sizing path:

  • Short groups. The search passes a negative group quantity to WithQuantity for a short group. WithLots then flips it to a long position of the same size. For equities the margin is the same on both sides, so the sizing holds. I checked locally: -100 IBM with a +10 open buyback and a $1,000 deficit generates +10 for the $500 left, and long IBM mirrors it with -10. Option strategy groups such as naked calls have a positive group quantity (10 for -10 calls), so they never reach the flip. With an open +2 buyback of 10 naked calls ($1,100 each), deficits of $1,300, $2,100 and $3,800 after it generate +2, +2 and +4, the fewest calls that cover each one.
  • Bounds. A deficit larger than the group's margin gives the whole group. A fractional lot count is capped at what's held. The loop only runs while there's a deficit.
  • Fees. Unlike the buying power model, the search doesn't leave room for fees, so a fill can leave a deficit of a few dollars. That's well inside the margin buffer that gates new orders, so it doesn't cause another margin call.
  • Cost. The search runs about log2(lots) margin evaluations, and only for regrouped or partly sold groups during a margin call. That's negligible.

CI: only Benchmarks fails, on EmptyFuturesBenchmark (C#, 395k vs a 401k floor in the re-run). I agree with the diagnosis on the pull request. Neither benchmark that failed places an order (Algorithm.CSharp/Benchmarks/EmptyFuturesBenchmark.cs, EmptySingleSecuritySecondEquityBenchmark.cs), so GetMarginCallOrders returns at totalMarginUsed <= 0, before any changed code. The previous commit passed the same workflow, and this commit only changes GenerateMarginCallOrders. Also, the same algorithm measured 446k earlier in the same job. Every other check passes.

Testing: the new tests fail on master, the full suite passes, and the change was tested end to end. That end-to-end run can't reach the regrouped option-strategy path, but the unit tests above cover it.

Other repositories: none need an update.

Risk: I kept risk:high. The change alters how much a margin call liquidates and which requests OnMarginCall receives, which is money and behaviour users can see. It isn't a small revert either. Nothing more would lower that: it's the kind of change, not missing evidence.

@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot removed the kronos:review:working review: in progress label Nov 13, 2026
@quantconnect-kronos quantconnect-kronos Bot added the kronos:review Request review label Nov 13, 2026
@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot added kronos:review:queued review: queued and removed kronos:review Request review labels Nov 13, 2026
@quantconnect-kronos-reviewer-2 quantconnect-kronos-reviewer-2 Bot added kronos:review:working review: in progress and removed kronos:review:queued review: queued labels Nov 13, 2026

@quantconnect-kronos-reviewer-2 quantconnect-kronos-reviewer-2 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requesting changes on 5e74b88b1. The earlier unheld-strategy sizing reproduction is fixed, but I reproduced three remaining liquidation failures:

  1. Rejected orders lose their fallback. Generation stops on projected coverage before submission. If that order is rejected, execution has no alternative, and subsequent scans select the same failing group again. A reproduction using Bitfinex's actual minimum-size validation repeatedly rejects BTC while never attempting a valid ETH liquidation; master attempts ETH after the rejection. This violates #10107's cancellation/rejection recovery requirement.
  2. A held stock group sharing a security can still be skipped instead of capped. The buying-power model sizes from the security's full holdings, not the selected group's allocation. In the reproduction below it requests 201 shares from a 200-share group; the cumulative guard drops the whole request, leaving $9,000 of the deficit uncovered despite those shares being available.
  3. The synthetic sizing search reverses residual short positions. With strategy grouping disabled but normal option margin retained, a partial short-call buyback causes the search to evaluate long calls instead. It generates one additional buyback where two are needed, leaving $600 uncovered.

Please add regression coverage for these cases and preserve recovery after a terminal order failure without restoring duplicate full-deficit commitments. The inline comments contain the setups and relevant call paths.

Verification: all 21 existing MarginCallModelTests cases passed locally. Four new assertions failed: three shared-stock variants and the residual-short case; the ordinary strategy-grouped short-call control passed. Temporarily capping the held-group lot count and removing the extra sign in WithQuantity made all 26 selected cases pass; those experimental edits were reverted. I also reran the rejection reproduction against the PR and an otherwise unchanged copy of master's margin-call model. This uses a test order processor with real brokerage-model validation, not a live brokerage connection. I did not repeat the full suite or the end-to-end backtest described in the PR.

CI: Build & Test Lean and the regression checks pass. Benchmarks remains red: its re-check reports EmptyFuturesBenchmark at 395k against a 401.4k floor, after 446k for the base comparison. I verified the failing log and the benchmark source: it never trades, so GetMarginCallOrders returns at the unchanged totalMarginUsed <= 0 guard. I agree that this failure is unrelated to the changed sizing path.

Other repositories: none need a source or contract update.

Risk: kept risk:high; the demonstrated failures can leave margin deficits uncovered, including indefinitely after a rejected liquidation.

@quantconnect-kronos-reviewer-2 quantconnect-kronos-reviewer-2 Bot removed the kronos:review:working review: in progress label Nov 13, 2026
Each margin call order is sized for the deficit the earlier ones leave,
so one the brokerage rejects or cancels left part of it uncovered, and
later scans picked the same failing group again. ExecuteMarginCall now
liquidates the other position groups for the deficit left, excluding
the groups whose orders didn't fill.

A held group's lots are capped at its own quantity, since the security's
buying power model sizes from all of its holdings, which other groups
can share. The search for groups that aren't held keeps the direction of
short positions instead of flipping them, which matters for asymmetric
margin such as short options.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@quantconnect-kronos

Copy link
Copy Markdown
Contributor Author

Addressed the second review in 85d4f9b:

  • Rejected orders: when a margin call order closes without filling, ExecuteMarginCall generates orders again for the deficit actually left, skipping the groups whose orders failed, and executes them in the same call.
  • Held groups sharing a security: their lots are capped at the group's own quantity before the holdings check.
  • Short groups in the search: they keep their side, so asymmetric margin such as short options is sized correctly.

There's a new test for each, and all three fail on the previous commit. FakeOrderProcessor gained RejectedSymbols to simulate rejections. Full suite, Release, CI filter: Passed: 39606, Skipped: 100, Failed: 0. End to end, with a brokerage model rejecting one stock's margin call orders, each margin call fell back to the other stocks, for the same total as master on the first one. The description is updated.

@quantconnect-kronos
quantconnect-kronos Bot marked this pull request as draft November 16, 2026 20:36
@quantconnect-kronos quantconnect-kronos Bot added the kronos:review Request review label Nov 16, 2026
@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot added kronos:review:queued review: queued and removed kronos:review Request review labels Nov 16, 2026
@quantconnect-kronos-reviewer-2 quantconnect-kronos-reviewer-2 Bot added kronos:review:working review: in progress and removed kronos:review:queued review: queued labels Nov 16, 2026

@quantconnect-kronos-reviewer-2 quantconnect-kronos-reviewer-2 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requesting changes on e9d6c2676, pending a maintainer decision on the existing recovery regression. No code has changed since my previous review; this review reassesses the November 16 replies.

I withdraw the closed-exchange finding. The comparison with an earlier PR commit was not sufficient. On origin/master, generation includes both IBM and MSFT, and AlgorithmManager's all-exchanges-open check also prevents an IBM retry while MSFT is closed. This is not a regression against the base branch, and I am not asking this PR to change that behavior. The delayed terminal-failure limitation likewise remains separate, not an additional blocker here.

One existing blocker remains: planning away the execution alternatives loses a viable fallback after combo rejection. In the covered-call reproduction (300 SPY at $100, one short call at $1, leverage 2, cash -$19,000), the $4,100 deficit is projected to be covered by the combo. Once that combo fails, excluding its symbols also excludes the standalone SPY group, and the empty-result reset selects the combo again. The base model instead retains and submits the standalone SPY -83 alternative. Alpaca's brokerage-model validation rejects the equity ComboMarket leg but accepts a standalone Market sale.

This is the same cause as the earlier recovery findings: generation now returns a projected liquidation plan rather than alternatives execution can fall through. I agree with stopping the sequence of recovery patches. Please have a QuantConnect maintainer choose whether to accept this limitation or change the sizing/execution approach, as requested in the author's November 16 comment. I am not prescribing another exclusion-set patch. Option C also needs an explicit decision about resizing requests after OnMarginCall; it is not automatically an acceptable replacement.

Verification: all 44 existing margin-call-related tests pass locally. I added four temporary NUnit controls: both closed-exchange controls submit nothing during six subsequent scans after the IBM rejection is disabled; the rejected-combo control fails on this head after six combo-only attempts and passes with an otherwise unchanged copy of origin/master's model, which submits SPY -83 on the first execution. The rejection simulation is checked against the actual Alpaca brokerage model, not an Alpaca connection. Temporary source changes were removed. I did not repeat the full suite or the end-to-end runs described in the PR.

CI: all seven active checks pass; API Tests is skipped.

Other repositories: none need a source or contract update for the current change.

Risk: kept risk:high. The confirmed regression can leave the portfolio under-margined despite an available liquidation. A maintainer's approach decision and evidence that the chosen behavior is implemented are still needed.

@quantconnect-kronos-reviewer-2 quantconnect-kronos-reviewer-2 Bot removed the kronos:review:working review: in progress label Nov 16, 2026
@quantconnect-kronos quantconnect-kronos Bot added kronos:develop:idle develop: paused and removed kronos:develop:working develop: in progress labels Nov 16, 2026
@Martin-Molinero

Copy link
Copy Markdown
Member

Going with C. The plan moves the fallback out of the call where the failure is known, and putting it back has needed state across scans every time. Execution already knows which orders failed, so the fallback belongs there.

  • GetMarginCallOrders returns one request per position group again. Each is regrouped from the holdings the open orders leave and sized for the deficit they leave. Keep the held-group cap, the sizing of groups that aren't held against their own lots, and the unsigned lots. Drop the cumulative sizing and _failedLiquidationSymbols.
  • ExecuteMarginCall executes the first group as given. Each later group is scaled down to the fewest of its lots that cover the deficit the earlier orders of the same call leave, never up. A rejected or canceled order frees no margin, so the next group gets the whole deficit, as on master.
  • On OnMarginCall: ExecuteMarginCall already drops later requests once the deficit is covered, and trimming them to what's left is the same rule. Requests are only reduced, so padding like MarginCallEventsAlgorithm's stays on the first group.
  • Failed orders go last: keep the margin call tickets that closed without filling (Invalid or Canceled) until the margin call ends, when margin remaining is back above zero. ExecuteMarginCall orders groups by how many of their margin call orders failed, then losers first. Each failure moves a group further back, but nothing is left out, so nothing needs resetting and no alternative is dropped. This also covers the delayed failure case recorded on ExecuteMarginCall can liquidate the same deficit across several groups in one batch #10107.

@quantconnect-kronos quantconnect-kronos Bot added kronos:develop:queued develop: queued and removed kronos:develop:idle develop: paused labels Nov 17, 2026
GetMarginCallOrders returns one request per position group again, each
regrouped from the holdings the open margin call orders leave and sized
for the deficit they leave. ExecuteMarginCall executes the first group as
given and scales each later one down to the fewest of its lots that cover
the deficit the earlier orders leave, so a failed order falls through to
the next group in the same call. Groups whose margin call orders failed
during the current margin call are executed last.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@quantconnect-kronos

Copy link
Copy Markdown
Contributor Author

Implemented option C in e6845f5:

  • GetMarginCallOrders returns one request per position group again, each regrouped from the holdings the open orders leave and sized for the deficit they leave. The held-group cap, the sizing of groups that aren't held against their own lots, and the unsigned lots stay. The cumulative sizing and _failedLiquidationSymbols are gone.
  • ExecuteMarginCall executes the first group as given and scales each later one down, never up, to the fewest of its lots that cover the deficit the earlier orders of the same call leave. A failed order frees nothing, so the next group gets the whole deficit, as on master.
  • Margin call tickets that closed without filling (Invalid or Canceled) are kept until margin remaining is back above zero. Groups are executed by how many of their orders failed, counting a failed order against the group with exactly its symbols, then losers first.

The rejected covered-call combo case from the last review now liquidates the standalone SPY group in the same call (-82 for the $4,100 deficit, the fewest shares that cover it), and the delayed-cancel case moves the canceled group behind the others on the next margin call. Both have tests, as do the multi-failure fall-through for rejected and canceled orders.

Full suite, Release, CI filter: Passed: 39610, Skipped: 100, Failed: 0. The margin call regression algorithms pass unchanged. End to end, without failures the orders are the same as the previous commit's, and with AIG's (or AIG's and GE's) orders rejected each margin call falls through to the other stocks in the same call. The description is updated.

@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving e6845f568. It implements option C as the maintainer decided on November 17, and I found nothing that blocks it.

What changed since my last review (e9d6c2676): _failedLiquidationSymbols and the cumulative sizing are gone. GetMarginCallOrders returns one request per group again, regrouped and sized for the deficit the open orders leave. ExecuteMarginCall scales each later group down for what the earlier orders of the same call leave, and puts groups whose orders failed last. I checked each part of the decision:

  • One alternative per group, no exclusion. Every group gets a request. A failed order releases no margin, so the next group's search needs the whole deficit and runs as generated, as on master. This resolves the second reviewer's rejected-combo case: the standalone SPY group isn't excluded any more. LiquidatesAStockGroupSharingTheUnderlyingOfARejectedComboOrder covers it (-82 for the $4,100). The rejected-BTC case from the first round falls through to the next group in the same call too.
  • Only ever reduced. GetQuantity caps every leg at its requested quantity, and the search starts at the requested lots. So padding that OnMarginCall adds stays on the first group, and later groups shrink only to the lots that cover what's left. For combos, the lots never go above |GroupOrderManager.Quantity|, so the cap never applies to one leg without the others, and the leg ratios hold. The scaled combo gets a new GroupOrderManager with the scaled quantity.
  • Failed orders go last, nothing left out. Failed tickets (Invalid or Canceled) stay until margin remaining is above zero or no margin is used. Each one counts against the group with exactly its symbols, by GroupOrderManager for combos. GetOpenMarginCallOrderQuantities filters on IsOpen, so the kept failures never count as pending. ExecutesThePositionGroupsWhoseMarginCallOrdersFailedLast covers a cancel that comes after a scan has seen the order open. That is the delayed-failure case recorded on #10107, and here it moves the group back instead of looping.
  • No failures. All counts are zero, so execution is losers first as on master, and the first group runs as generated.

Other checks, no change needed:

  • Search bounds. LotSize is validated as positive in SymbolProperties, so the per-lot quantity is never zero. Requests without quantity, and combos with a zero group quantity, skip the scaling. ExecutesLaterMarginCallRequestsWithoutQuantityAsGiven covers that. The search takes about log2(lots) GetMarginReleased evaluations per later group (around 30 for a crypto position at a 1e-8 lot size), and only during a margin call.
  • Holdings across groups. Groups partition the holdings left after the open orders, each request is capped at its group's quantity, and execution only reduces. So a call can't sell more than is held. DoesNotSellMoreThanHeldAcrossPositionGroupsSharingALeg covers it.
  • Fees. A scaled order covers the deficit before fees, unlike the first group, which the buying power model sizes. A fill can leave margin remaining a few dollars below zero. That's well inside the margin buffer that triggers margin call orders, so it doesn't start another one. The previous approvals accepted the same for the regrouped-group search.

CI: every check passes on this commit; one is skipped. Locally, all 45 margin call tests pass on a Release build of this commit.

Testing: the description lists new tests that fail on master, a passing full suite, and the margin call regression algorithms with unchanged statistics. It was also tested end to end, with and without a brokerage model rejecting one or two stocks' margin call orders. In each run, a rejection fell through to the next stock in the same call.

Other repositories: none need an update. IMarginCallModel, the engine's exchange-open check and OnMarginCall are unchanged.

Risk: I kept risk:high. The change alters how much a margin call liquidates, and it reduces later requests that OnMarginCall set. That's money and behaviour users can see, and the change isn't a small revert. The kind of change sets the label, not missing evidence.

@quantconnect-kronos-reviewer-2 quantconnect-kronos-reviewer-2 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requesting changes on e6845f568. It implements the maintainer's option C and resolves the earlier rejection-fallback/shared-underlying findings. The held-group cap and residual-short fixes still pass, and the delayed-failure test preserves recovery progress. I am resolving my earlier threads, including the previously withdrawn closed-exchange finding.

One new blocker: execution-time downsizing can turn valid liquidations into rejected orders. The search considers maintenance-margin release, but not whether the reduced request remains executable under the existing buying-power and brokerage checks. I verified two cases of this same cause:

  • Protective puts: 1,000 SPY at $100, ten $100-strike puts at $1, and 100 IBM at $100 bought at $200; cash -$102,500, leverage 2. Equity is $8,500, maintenance $15,000, and the deficit $6,500. IBM executes first, releasing $5,000. The generated ten-lot protective-put liquidation passes Portfolio.HasSufficientBuyingPowerForOrder, but execution reduces it to two lots. That request fails the same real validation: the remaining eight lots require $40,000 of initial margin against the held group's $10,000 maintenance. Master submits the valid ten-lot combo. The rejection also occurs if IBM has already filled rather than remaining open.
  • Brokerage minimums: with Bitfinex's actual symbol properties and validation, both generated crypto liquidations are valid, but the later ETH order is reduced below its 0.0008 minimum. Tested end to end on December 14, 2022 minute data: the valid ETHUSD -0.00113768 candidate becomes -0.00059873 and is rejected. Three subsequent scans also reject undersized ETH orders; final margin remaining is -$0.39704. The otherwise identical master-model control fills the original candidate and finishes with +$0.35411322 margin remaining. This is brokerage-model validation in a backtest, not a brokerage connection.

Please preserve executability when reducing later requests, within the original requested quantities, and add regressions that exercise the actual position-group buying-power check and brokerage minimum-size validation. The inline comment anchors the shared sizing cause.

Verification: all 45 margin-call-related NUnit tests pass locally in Release. Both additional reproductions were checked against the base implementation; the crypto case was also tested end to end as above. I did not repeat the full suite or regression-algorithm set reported in the description.

CI: all seven active checks pass; API Tests is skipped.

Other repositories: QuantConnect/Documentation still needs the margin-call default-behavior/event documentation updated for failure-priority ordering and reduction of later OnMarginCall requests. No brokerage source or API-contract update is needed.

Risk: kept risk:high, because the verified downsizing failures can leave a portfolio under-margined despite an executable liquidation being available.

{
quantities[requests[i].Symbol] = quantities.GetValueOrDefault(requests[i].Symbol) + GetQuantity(i, lots);
}
if (marginRemaining + GetMarginReleased(quantities) >= 0)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Preserve executability when downsizing liquidation requests

This predicate only checks maintenance-margin release. A smaller request can satisfy it while failing the real order-validation rules that the original candidate satisfies. With 1,000 SPY, ten ATM protective puts and the earlier IBM liquidation described in the review, the search shrinks a valid ten-lot combo to two lots; Portfolio.HasSufficientBuyingPowerForOrder rejects it because the residual group's initial margin exceeds its held maintenance. BrokerageTransactionHandler uses that check and invalidates the orders. Independently, the same search reduces a valid Bitfinex ETH sale below MinimumOrderSize; the end-to-end control leaves negative margin while master fills the original candidate.

Please account for the existing initial-margin/order-admissibility constraints when choosing a reduced quantity, without exceeding the supplied request, and test the resulting orders through those real checks rather than checking projected maintenance relief alone. These are two manifestations of the same downsizing cause, not a request to change the brokerage or position-group validation rules.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in a85ae37: when every order of a scaled-down group is rejected, ExecuteMarginCall executes the requested orders instead, so they go through the same buying-power and brokerage checks, and the smaller ones don't count as failures of that group. Groups are also scaled only once an earlier order of the call is filled or open, so after failed orders alone the next group runs as generated. Tests cover both of your cases through Portfolio.HasSufficientBuyingPowerForOrder and DefaultBrokerageModel.IsValidOrderSize.

…ejected

A smaller order can fail checks the requested one passes, such as the
initial margin of the holdings it leaves or the brokerage's minimum order
size. When every order of a scaled down group is rejected, the requested
orders are executed instead, and the smaller ones don't count as failures
of those position groups. Groups are only scaled down when an earlier order
of the call didn't fail, so after failed orders alone the next group is
executed as generated for the whole deficit, as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@quantconnect-kronos

Copy link
Copy Markdown
Contributor Author

Addressed the latest review in a85ae37:

  • Executable downsizing: a smaller order can fail checks the requested one passes, such as the initial margin of the holdings it leaves or the brokerage's minimum order size. The margin call model can't see the brokerage model, so instead of re-implementing those checks, ExecuteMarginCall falls back on the real ones: when every order of a scaled-down group is rejected, it executes the requested orders instead, never more than requested, and doesn't count the smaller ones as failures of that group.
  • No scaling after failures alone: groups are scaled only once an earlier order of the call is filled or open. After failed orders alone, the next group runs as generated for the whole deficit, as on master. The fallback cases now match master's quantities (SPY -81, and SPY -83 for the rejected combo).
  • Tests: ExecutesTheRequestedOrderWhenTheScaledDownOneHasNotEnoughBuyingPower is your protective-put case, with orders checked through Portfolio.HasSufficientBuyingPowerForOrder. The 2-lot combo is rejected and the 10-lot combo is executed. ExecutesTheRequestedOrderWhenTheScaledDownOneIsBelowTheMinimumOrderSize checks orders with DefaultBrokerageModel.IsValidOrderSize. Both fail on e6845f5. FakeOrderProcessor.RejectsOrder now sees all of an order's legs, so combos are checked as a whole.
  • End to end: with the Bitfinex brokerage model on December 14, 2022 minute data, the ETH order scaled to -0.00077191 is rejected for the 0.0008 minimum and the requested -0.01 fills in the same margin call. On e6845f5 the same run leaves the deficit uncovered for hours. The equity runs give the same orders as before.
  • Documentation: Update default margin call model behavior for deficit sizing Documentation#2915 updates the margin call Default Behavior and the OnMarginCall notes.

Full suite, Release, CI filter: Passed: 39612, Skipped: 100, Failed: 0. The margin call regression algorithms pass unchanged. The description is updated.

@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving a85ae376d. It resolves the second reviewer's finding on e6845f568, and I found nothing new that blocks it.

What changed since my last review (e6845f568):

  • Falling back to the requested order. When every order of a scaled-down group comes back Invalid, ExecuteMarginCall removes those tickets from the failure bookkeeping and executes the generated requests instead. This happens only for a group that was actually scaled (requests != generatedRequests). So the requested order goes through the same buying-power and brokerage checks it would face on master, and execution never sends more than was requested. Because the smaller tickets are removed from _marginCallTickets, a group counts as failed at most once per call. They're still returned and logged as executed orders, the same way master returns and logs rejected margin call orders.
  • No scaling after failures alone. Scaling now starts only once an earlier order of the call is filled or open. After failed orders alone, the next group runs as generated, so the fall-through cases match master's quantities again (SPY -81, and SPY -83 for the rejected combo). Before, they were one share short of what the buying power model sizes with fees.
  • The stop check moved below the fallback, so it also sees margin released by the requested orders.

Verification: locally, all 47 margin call tests pass on a Release build of this commit. With only DefaultMarginCallModel.cs reverted to e6845f568, both new tests fail, and so do the three that now expect master's quantities. FakeOrderProcessor.RejectsOrder now sees all of a combo's legs before it decides, so the protective-put test checks the combo as one order through Portfolio.HasSufficientBuyingPowerForOrder, as the transaction handler does.

Checked, no change needed: the fallback reuses the original GroupOrderManager, which nothing submitted before. The scaled orders had their own. A non-combo group is always a single request, so "every order rejected" can't be a partial rejection. If the requested order is rejected too, that's one failure of the group, and the next group is scaled for what the filled orders leave, since nothing more was released.

CI: every check passes on this commit; one is skipped.

Testing: the new tests fail on the previous commit, the full suite passes, and the margin call regression algorithms are unchanged. It was tested end to end, including a crypto run under a brokerage minimum order size: there the requested order now fills in the same margin call, where the previous commit left the deficit uncovered.

Other repositories: QuantConnect/Documentation#2915 documents the new default behavior, and I'm approving it alongside this one. No other repository needs an update: IMarginCallModel, the engine's exchange-open check and OnMarginCall are unchanged.

Risk: I kept risk:high. The change alters how much a margin call liquidates and reduces later requests that OnMarginCall set, which is money and behavior users can see, and it isn't a small revert. It's the kind of change that sets the label, not missing evidence.

@quantconnect-kronos-reviewer-2 quantconnect-kronos-reviewer-2 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requesting changes on a85ae376d. The synchronous protective-put and minimum-order-size cases from my previous review are fixed. One manifestation of that same executability finding remains: a scaled order rejected after the order wait times out loses its valid original alternative.

I reproduced this with the real background BrokerageTransactionHandler, a live-mode algorithm, and the five-second market-order wait:

  • Hold 100 IBM at $10, bought at $20, and 100 MSFT at $100; leverage 2, cash -$7,000. Equity is $4,000, maintenance margin $5,500, and the deficit $1,500. MSFT has a 25-share minimum. Generation supplies IBM -100 and the valid MSFT -31.
  • IBM remains submitted. Execution reduces MSFT to -20. A synchronization gate delays the real DefaultBrokerageModel.IsValidOrderSize refusal until after the wait. When execution returns, the reduced ticket is still New; after releasing the gate it becomes Invalid, but MSFT -31 is never submitted. Three subsequent scans each submit and reject MSFT -21, leaving the $1,000 gap after IBM's projected relief uncovered.
  • The identical asynchronous test passes with only the margin-call model replaced by the base-branch implementation: it submits MSFT -31 in the first execution. A separate delayed-response test that fills IBM likewise leaves margin remaining at -$1,000 on this head, versus +$550 with the base model. These are controlled local validation tests, not a brokerage connection.

AddOrder waits for the ticket's order-set signal, which precedes validation (BrokerageTransactionHandler.cs:374-376,405-418,923,962-983). Therefore an order-set ticket is not proof that validation finished. The one-time all-Invalid check cannot provide the fallback once WaitForOrder times out. Please preserve the originally supplied executable alternative through the reduced request's terminal result, without submitting both while the reduced request can still fill, and cover this asynchronous path. This is the existing downsizing/executability cause, not a request for another independent recovery strategy.

Verification: all 47 existing margin-call-related tests pass locally in Release. The additional asynchronous regression fails on this head and passes with the base model. I also tested the synchronous path end to end with deterministic prices and a synthetic 25-share minimum: the reduced 20-share order is rejected, the original 30-share order fills, and margin remaining ends at +$500. That ordinary backtest uses synchronous transaction processing and does not exercise the asynchronous failure above. I did not repeat the full suite or all regression algorithms reported in the description. Temporary source changes were removed.

CI: all seven active checks pass; API Tests is skipped.

Other repositories: QuantConnect/Documentation#2915 supplies the required documentation update, but the pair is not ready until this remaining Lean finding is resolved. No brokerage source or API-contract update is needed.

Risk: kept risk:high on both PRs. The remaining regression can leave an account under-margined despite an executable liquidation; the change also affects liquidation quantities. My earlier executability thread remains open.

@quantconnect-kronos

Copy link
Copy Markdown
Contributor Author

@Martin-Molinero, one more decision is needed on option C before I change anything else.

The finding (second reviewer, on a85ae37): later groups are scaled down to the remaining deficit. A scaled order can fail checks the requested one passes, such as the initial margin of the holdings it leaves or a brokerage's minimum order size. a85ae37 covers that when the rejection comes back while ExecuteMarginCall waits on the order: it then submits the requested order. WaitForOrder returns on OrderClosed, so this covers the transaction handler's buying-power and brokerage-model checks whenever it validates within MarketOrderFillTimeout (5 seconds). If validation takes longer than that, or the brokerage itself rejects the order after accepting it, the scaled order's Invalid arrives after the call has moved on. The requested order is then never submitted. Later scans generate the group again for the deficit the open orders leave, which can be below the minimum again. Master submits the requested order straight away, so for this case it's a regression.

Closing that gap means remembering each scaled order's requested alternative until the scaled order is closed, across scans. That's the kind of cross-scan state you moved away from with option C, so I'd rather you choose:

  • A. Keep a85ae37 as it is and accept the limitation. The fallback covers rejections that arrive while the margin call waits on the order. A slower one leaves that group to later scans.
  • B. Remember scaled orders' requested alternatives until the scaled order closes. When one closes without filling, the next ExecuteMarginCall submits the requested order for that group ahead of the others. This is more state across scans, cleared when the margin call ends, like the failed tickets.
  • C. Drop the execution-time scaling. Later groups would run as generated, as on master. Generation would still regroup and size for the deficit the open orders leave, and the failure ordering stays. Downside: when an earlier order of the same call covers part of the deficit, the next group is liquidated for all of it, as on master. That's the over-liquidation the first bullet of ExecuteMarginCall can liquidate the same deficit across several groups in one batch #10107 describes.

The second reviewer reproduced this with a gate that delays validation in the real background transaction handler. Their numbers: 100 IBM and 100 MSFT, a 25-share MSFT minimum, and MSFT scaled from -31 to -20, with margin remaining ending at -$1,000 versus +$550 on master. Which should I take?

@Martin-Molinero

Copy link
Copy Markdown
Member

Neither. Check the scaled size before sending it instead of after, so nothing waits on a rejection or needs remembering across scans.

  • In ExecuteMarginCall, a scaled-down group is executed only if every leg passes DefaultBrokerageModel.IsValidOrderSize, reusing it. Otherwise the requested orders are executed, as on master.
  • Drop the fallback that executes the requested orders after the scaled ones are rejected.
  • The protective put rejection isn't for the margin call model to check, since its orders only reduce holdings. PositionGroupBuyingPowerModel.HasSufficientBuyingPowerForOrder compares the initial margin of what's left to the maintenance margin of what's held, so a partial close looks like an increase. That's Partly closing an option strategy can fail the buying power check #10179, and this PR merges after it.
  • A brokerage rejecting the scaled order for a reason Lean doesn't model stays as is. It counts as a failure of the group, which goes last on the next margin call.

@quantconnect-kronos-reviewer-2 quantconnect-kronos-reviewer-2 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requesting changes on a85ae376d, pending the maintainer decision requested in the author's November 17 reply. Neither this head nor Documentation#2915 has changed since my previous review. There are no new independent findings.

The existing P1 remains: a reduced liquidation rejected after the order wait ends loses its executable original alternative. The reply correctly confirms the limitation, rather than resolving it. ExecuteMarginCall checks for Invalid only immediately after its bounded wait; only the reduced ticket survives afterward. The real transaction handler signals OrderSet before buying-power and brokerage validation, so this is a reachable asynchronous path. In the previously reproduced same-head case, valid MSFT -31 becomes -20, the refusal arrives after the wait, and later scans repeatedly generate below-minimum quantities. The base-model control submits the valid original request.

This is the remaining form of the earlier downsizing/executability finding, not a request for another independent recovery patch. Please have a maintainer resolve the pending choice before merging or adding further recovery state. The earlier option C decision authorizes execution-time reduction but does not explicitly accept this subsequently demonstrated loss of an executable alternative. If the chosen approach changes the issue's intended result, that scope decision needs to be explicit too.

Verification: rechecked the unchanged head, the execution/transaction-handler lifecycle, the base implementation, and the latest discussion. The earlier same-head asynchronous regression and base-model control remain the reproduction evidence; I did not rerun NUnit or end-to-end tests in this pass. The synchronous tests and end-to-end runs described in the PR do not exercise rejection after the wait ends.

CI: all seven active checks pass; API Tests is skipped.

Other repositories: QuantConnect/Documentation#2915 remains paired with this change and must reflect the selected fallback behavior. No additional brokerage, interface, wrapper, or stub update is needed.

Risk: kept risk:high on both PRs. The unresolved regression can leave an account under-margined despite an executable liquidation; a documented limitation alone does not resolve that without the pending maintainer decision. My existing executability threads remain open.

A later group is scaled down only if every leg of the smaller order passes
DefaultBrokerageModel.IsValidOrderSize; otherwise the requested orders are
executed, as on master. The fallback that executed the requested orders
after the scaled ones were rejected is removed, so nothing waits on a
rejection.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@quantconnect-kronos

Copy link
Copy Markdown
Contributor Author

Implemented the November 17 decision in 8301c76:

  • ExecuteMarginCall scales a later group down only if every leg of the smaller order passes DefaultBrokerageModel.IsValidOrderSize. Otherwise the requested orders are executed, as on master. The size is checked before sending, so nothing waits on a rejection or is remembered across scans.
  • The fallback that executed the requested orders after the scaled ones were rejected is removed.
  • The protective-put test is removed, since that rejection of a partial close is Partly closing an option strategy can fail the buying power check #10179, which this pull request merges after. A brokerage rejecting a scaled order for a reason Lean doesn't model counts as a failure of the group, which goes last on the next margin call.
  • ExecutesTheRequestedOrderWhenTheScaledDownOneIsBelowTheMinimumOrderSize now checks that the 20-share MSFT order below the 25-share minimum is never sent, and the requested order is executed in its place with nothing rejected.

All 46 margin call tests pass, and so do the margin call regression algorithms, with unchanged statistics. Full suite, Release, CI filter: Passed: 39610, Skipped: 100, Failed: 1. The one failure is FutureSymbolGeneratorTests.GeneratesFutureSymbolWithCorrectExpiryDateOverWiderDateRange, a NullReferenceException in the futures expiry functions, unrelated to margin calls, and it passes when run again. End to end with the Bitfinex brokerage model on December 14, 2022, ETH scaled for the $0.20 left would be below the 0.0008 minimum, so the requested -0.01 is sent and fills in the same margin call, with no rejected order. The description records the decision, and QuantConnect/Documentation#2915 is updated to match.

@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving 8301c7609. It implements the maintainer's November 17 decision, and I found nothing that blocks it. Merge it only after #10179 is fixed, as that decision says: until then, a scaled-down partial close of a protective put fails the position group buying power check. That group then goes last on the next margin call, where it's executed as generated, but the deficit stays uncovered until then.

What changed since my last review (a85ae376d):

  • Size checked before sending. A later group is scaled down only if every leg of the smaller order passes DefaultBrokerageModel.IsValidOrderSize. Otherwise the requested orders go out unchanged, as on master. For combos the check runs on each leg's total quantity, which is what the order processor validates. Nothing now depends on when a rejection arrives. That resolves the second reviewer's finding about rejections that arrive after WaitForOrder times out: the scaled order that came back late was below the minimum, and it's no longer sent.
  • The fallback after a rejection is gone. ExecuteMarginCall is back to one submit loop per group. The stop check still runs after each group, against the open margin call orders.
  • Tests. The protective-put test is removed, since that rejection is #10179. ExecutesTheRequestedOrderWhenTheScaledDownOneIsBelowTheMinimumOrderSize now checks that the 20-share order is never sent.

Verification: locally, all 46 margin call tests pass on a Release build of this commit. With the new size check bypassed, ExecutesTheRequestedOrderWhenTheScaledDownOneIsBelowTheMinimumOrderSize fails.

Not blocking: some brokerage models check the minimum against something other than quantity. BinanceBrokerageModel compares the order's notional value with MinimumOrderSize (Common/Brokerages/BinanceBrokerageModel.cs:257). There, the quantity check usually refuses to scale, so the requested order goes out as on master. For an asset priced under $1, though, a scaled quantity can pass the quantity check and still fall under the notional minimum. That case counts as a failure of the group, as the decision accepts for brokerage rejections, and the group is executed as generated on the next margin call.

CI: every check passes on this commit; one is skipped.

Testing: the full suite passes. The one failure is a futures expiry test that has nothing to do with margin calls and passes when run again. The margin call regression algorithms are unchanged. It was also tested end to end, including under a crypto brokerage minimum order size: the requested order now fills in the same margin call, and no order is rejected.

Other repositories: QuantConnect/Documentation#2915 now describes the minimum-size check, and I'm approving it too. No other repository needs an update: IMarginCallModel, the engine's exchange-open check and OnMarginCall are unchanged.

Risk: I kept risk:high. The change alters how much a margin call liquidates and reduces later requests that OnMarginCall set. That's money and behavior users can see, it isn't a small revert, and it has to merge after #10179.

@quantconnect-kronos-reviewer-2 quantconnect-kronos-reviewer-2 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requesting changes on 8301c7609 only until the maintainer's stated prerequisite, #10179, is fixed and included. The latest minimum-size change implements the November 17 decision; I found no new independent defect in that patch.

The minimum-size/asynchronous finding is resolved. Every leg is checked with DefaultBrokerageModel.IsValidOrderSize before the reduced group is submitted; otherwise the original requests are submitted. The after-rejection fallback is gone, so the reproduced below-minimum rejection can no longer outlast that fallback's wait. I am resolving my asynchronous-validation and pending-decision threads.

One existing blocker remains: the protective-put prerequisite has not landed. #10179 is still open, and the current buying-power implementation still compares the residual group's initial margin with the held group's maintenance margin. I reran the earlier protective-put reproduction against this head: equity $8,500, maintenance $15,000, and a $6,500 deficit; IBM's liquidation projects $5,000 relief. The original ten-lot protective-put liquidation passes Portfolio.HasSufficientBuyingPowerForOrder, but execution replaces it with two lots (SPY -200, puts -2), and both legs become Invalid through that same real validation. The identical test passes with only the margin-call model replaced by origin/master's implementation, which submits the valid ten-lot alternative.

This is the already-discussed executability finding, not a request for another fallback patch or for moving #10179 into this PR. Please land that prerequisite and verify the partial close with it included before merging this change. The protective-put part of my existing executability thread remains open.

Verification: all 46 existing margin-call tests pass in Release. The additional protective-put control fails on this head and passes with the base model. Tested end to end with the Bitfinex margin brokerage model and December 14, 2022 minute data: BTC -0.0002 fills, leaving about $0.20 uncovered; instead of an ETH reduction below its 0.0008 minimum, the original ETH -0.00299470 fills in the same margin call. Two fills, no rejections, final margin remaining +$1.777853325. This uses brokerage-model validation in a backtest, not a brokerage connection. Temporary source changes were removed. I did not repeat the full suite or all regression algorithms.

CI: all seven active checks pass; API Tests is skipped.

Other repositories: Documentation#2915 has merged and its changed pages describe the minimum-size behavior. No further brokerage, interface, wrapper, or stub update is needed.

Risk: retained risk:high: the required buying-power fix is absent, and the current partial-close path still turns an executable liquidation into a rejection. The change also alters later liquidation quantities after OnMarginCall.

@quantconnect-kronos

Copy link
Copy Markdown
Contributor Author

No code change for the latest reviews. Both confirm that 8301c76 implements the November 17 decision. The only open item is the protective-put partial close, which the decision assigns to #10179. That issue is still open with no fix yet, and per the decision, recorded in the description, this pull request merges after it. Once #10179 is fixed, the partial close should be verified on top of it before this merges. QuantConnect/Documentation#2915, which documents this behavior, has already merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk:high High risk

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ExecuteMarginCall can liquidate the same deficit across several groups in one batch

1 participant