Skip to content

Defer cash-account buy orders until reducing fills in ImmediateExecutionModel#9416

Closed
aiSynergy37 wants to merge 1 commit intoQuantConnect:masterfrom
aiSynergy37:bug-9231-immediate-execution-sell-first
Closed

Defer cash-account buy orders until reducing fills in ImmediateExecutionModel#9416
aiSynergy37 wants to merge 1 commit intoQuantConnect:masterfrom
aiSynergy37:bug-9231-immediate-execution-sell-first

Conversation

@aiSynergy37
Copy link
Copy Markdown
Contributor

Description

In cash accounts, ImmediateExecutionModel could submit sell and buy market orders in the same execution pass. In live trading this can cause transient insufficient buying power rejects because the sell has not filled yet.

This change makes ImmediateExecutionModel (C# and Python) defer cash-consuming orders while there are open position-reducing orders.

Changes

  • ImmediateExecutionModel (C#):
    • Detect cash accounts.
    • Detect open position-reducing orders.
    • Skip non-reducing targets while reducing orders are still open.
  • ImmediateExecutionModel.py:
    • Mirrors the same behavior for Python framework users.
  • Added regression test:
    • ImmediateExecutionModelTests.CashAccountSubmitsReducingOrdersBeforeIncreasingOrders
    • Verifies first execution only submits the reducing order, and after fill event, subsequent execution submits the increasing order.

Validation

  • dotnet build Tests/QuantConnect.Tests.csproj --no-restore -v minimal /p:RunAnalyzers=false ✅
  • dotnet test Tests/QuantConnect.Tests.csproj --no-build --filter FullyQualifiedName~ImmediateExecutionModelTests.CashAccountSubmitsReducingOrdersBeforeIncreasingOrders -v normal -m:1 ⚠️
    • Aborted by environment-level Python.NET crash before assertions:
    • System.InvalidOperationException: GIL must always be released, and it must be released from the same thread that acquired it.

Closes #9231

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ImmediateExecutionModel.py feature enhancement request

3 participants