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

オープン 初心者向け
#10,206 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

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

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

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
78/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
csharp
領域
backend, testing

調査の方向性

Common/Statistics/TradeBuilder.cs の ProcessFillUsingFlatToReduced から始め、issue に記載された LIFO の部分消費分岐を追ってください。提示された fills のシーケンスに対する回帰テストを追加し、クローズされた trade が t0 に始まり、2 分間続くことを確認してください。一致した数量、価格、手数料が変わらないことを確認してください。

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
C#
スター
22.6k
フォーク
5.4k
平均マージ
11時間 39分
マージ済み PR(30日)
104

環境構築

Codespaces で開く

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

はじめの一歩

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

QuantConnect/Lean のほかの issue

QuantConnect/Lean の issue をすべて見る

似ている issue

C# の issue をもっと見る

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

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