Cross-zero replacement uses full Lean quantity after the closing part fills

Đang mở
#10,148 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
55/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
csharp
Lĩnh vực
backend, testing

Hướng nghiên cứu

Start with Brokerages/Brokerage.cs, especially TryHandleRemainingCrossZeroOrder and TryGetUpdateCrossZeroOrderQuantity, then trace TradeStation's and Alpaca's UpdateOrder callers. Add regression tests with a test brokerage that records replacement requests for both split parts after the closing part fills; confirm price-only replacements preserve each part's intended quantity and ordinary orders still work.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

priority:p2

Problem

After a split cross-zero order's closing part fills, Brokerage.TryGetUpdateCrossZeroOrderQuantity forgets that the still-working opening part is only part of the original Lean quantity. A later price-only replacement can therefore request too much quantity.

Deterministic reproduction

No brokerage connection is needed. Using a test brokerage that records PlaceCrossZeroOrder and replacement requests:

  1. Start short 5 AAPL and register a Lean buy limit order for 10.
  2. Call TryCrossZeroPositionOrder(order, -5). The closing request buys 5; the deferred opening request also buys 5.
  3. Pass a Filled event for the first 5 through TryHandleRemainingCrossZeroOrder and wait for the opening request to be submitted.
  4. Update the limit price without changing the Lean quantity.

Observed with TradeStation's UpdateOrder, recording its replacement arguments:

Brokerage id: opening part's id
Expected replace quantity: 5
Actual replace quantity:   10

If the replacement is accepted and filled, this can buy 15 shares in total instead of the requested 10. The test records the outgoing request; it does not connect to a broker or establish a live occurrence.

Cause and affected callers

  • Brokerages/Brokerage.cs, TryHandleRemainingCrossZeroOrder: the first Filled removes _leanOrderByBrokerageCrossingOrders[order.Id] before submitting the opening part.
  • TryGetUpdateCrossZeroOrderQuantity: once that entry is absent, it returns leanOrder.Quantity, losing the split quantity.
  • TradeStation's UpdateOrder sends that quantity to the last brokerage id.
  • Alpaca's UpdateOrder also uses the returned quantity with the last brokerage id, and CreateAlpacaChangeOrder puts it in the replacement request. It uses the same shared split lifecycle.
  • Tradier calls the helper but ignores the returned quantity and does not send a quantity in its price replacement, so this specific over-sizing path does not apply there.

This is present independently of the invalid-id soft-rejection change in TradeStation #118.

Required result

A price-only replacement of either part must preserve that part's intended total quantity, including after the closing part completes. Existing first-part updates and ordinary non-split orders must continue to work.

Suggestion: retain enough split lifecycle information in the shared brokerage helper to calculate replacement quantities for both parts, with regression tests; TradeStation and Alpaca can then reuse the correction rather than implementing separate bookkeeping.

Ngôn ngữ chính
C#
Star
22.6k
Fork
5.4k
Merge trung bình
11 giờ 39 phút
Pull request đã merge (30 ngày)
104

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của QuantConnect/Lean

Tất cả issue của QuantConnect/Lean

Issue tương tự

Thêm issue về C#

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.