FlatToReduced LIFO trade starts too late when an older entry is partially consumed

Đang mở Phù hợp với người mới
#10,206 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ó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
78/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

Bắt đầu trong Common/Statistics/TradeBuilder.cs, tại ProcessFillUsingFlatToReduced, và lần theo nhánh tiêu thụ một phần LIFO được mô tả trong issue. Bổ sung kiểm thử hồi quy cho chuỗi fills được cung cấp, kiểm tra rằng giao dịch đã đóng bắt đầu tại t0 và kéo dài hai phút. Xác nhận rằng các khối lượng được khớp, giá và phí vẫn không thay đổi.

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

Mô tả

priority:p2
Expected behavior

With FlatToReduced grouping and LIFO matching, a closed trade's EntryTime should be the earliest entry fill contributing to that trade, including an older fill that is only partially consumed. The duration should cover the entire matched trade.

Actual behavior

Verified on master f62b677be: buy 1 at time t0, buy 2 at t1, then sell 2.5 at t2. The closing fill consumes all 2 units from t1 and 0.5 from t0, but the trade reports EntryTime = t1, not t0. With fills a minute apart, Duration is one minute instead of two. Downstream average/median holding durations inherit this error.

In Common/Statistics/TradeBuilder.cs, ProcessFillUsingFlatToReduced updates entryTime for LIFO only when an entry fill is fully consumed; the partial-consumption branch does not update it.

Reproduction

Run in a console project referencing Common/QuantConnect.csproj:

using System;
using QuantConnect;
using QuantConnect.Orders;
using QuantConnect.Orders.Fees;
using QuantConnect.Statistics;

var symbol = new Symbol(SecurityIdentifier.GenerateEquity("SPY", Market.USA, mapSymbol: false), "SPY");
var t0 = new DateTime(2026, 1, 1);
var builder = new TradeBuilder(FillGroupingMethod.FlatToReduced, FillMatchingMethod.LIFO);
var fills = new[] { (Quantity: 1m, Price: 100m), (2m, 100m), (-2.5m, 100m) };
for (var i = 0; i < fills.Length; i++)
{
    var (quantity, price) = fills[i];
    builder.ProcessFill(new OrderEvent(i + 1, symbol, t0.AddMinutes(i), OrderStatus.Filled,
        quantity > 0 ? OrderDirection.Buy : OrderDirection.Sell, price, quantity, OrderFee.Zero), 1m, 0m);
}
var trade = builder.ClosedTrades[0];
Console.WriteLine(trade.EntryTime == t0); // actual False; expected True
Console.WriteLine(trade.Duration);       // actual 00:01:00; expected 00:02:00

A fix should retain the earliest contributing entry time for LIFO trades even when the oldest contributing fill is only partly matched, without changing matched quantities, prices or fees. Add regression coverage for partial consumption of an older fill.

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.