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

オープン
#10,107 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

この issue はまだ評価されていません。

説明

priority:P1
Description

DefaultMarginCallModel.ExecuteMarginCall can submit multiple liquidations for the same margin deficit within its first execution batch, even when the first pending order would already cover the whole deficit. This is separate from generating duplicate orders for the same group on later scans (#10008).

GetMarginCallOrders sizes every position group's request from the same current portfolio deficit. ExecuteMarginCall submits a group and calls SecurityTransactionManager.WaitForOrder, but ignores its false return when the wait times out. If the order is still open, holdings and MarginRemaining are unchanged, so execution continues to the next group and submits another order covering the same deficit.

In backtesting, MarketOrderFillTimeout is zero, and market orders on daily subscriptions can remain open until the next bar. Thus multiple distinct groups can all be liquidated for the same deficit during a single margin call, even without any subsequent scans.

Reproduction and observed result

Reproduced locally with a temporary NUnit test in MarginCallModelTests, using the existing portfolio/security helpers and FakeOrderProcessor:

  1. Create two distinct USD equity positions, SPY and AAPL, each holding 100 shares at a current price of $100 and leverage 2. Use zero fees and set cash to -$14,000.
  2. Use DefaultMarginCallModel with its default margin buffer and set portfolio.Transactions.MarketOrderFillTimeout = TimeSpan.Zero.
  3. Verify portfolio value is $6,000, maintenance margin is $10,000, and the deficit is $4,000.
  4. Call GetMarginCallOrders, then call ExecuteMarginCall exactly once, leaving submitted orders open.

Observed:

  • Generation returns one request for each group, each selling 81 shares.
  • A single execution submits both requests, returning two tickets.
  • Each sale releases $4,050 of margin at current prices; combined projected relief is $8,100 against a $4,000 deficit.
  • An assertion expecting only one submitted liquidation fails: Expected: 1, But was: 2.

The reproduction fails identically on master (da4d8e4a4) and on 6dd4b06d1; this is a pre-existing execution/sizing problem, not a regression introduced by the pending-ticket guard.

Tested end to end with daily SPY and minute AAPL as well: AAPL's liquidation can fill while a further SPY liquidation is already being submitted from the same unchanged deficit snapshot.

What a fix must achieve

Avoid committing the entire deficit more than once when multiple position groups are processed in one margin call. Continue to allow additional liquidation when pending orders cannot cover the deficit, preserve combo-order grouping, and handle cancellation/rejection without leaving the portfolio indefinitely under-margined.

One possible approach is to account for pending reductions when deciding whether and how much to liquidate from the next group, rather than assuming a timed-out wait means the first liquidation has completed.

主要言語
C#
スター
22.5k
フォーク
5.4k
平均マージ
22時間 6分
マージ済み PR(30日)
81

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

QuantConnect/Lean のほかの issue

QuantConnect/Lean の issue をすべて見る

似ている issue

C# の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。