TradeBuilder FillToFill MAE/MFE of a scaled-in lot include prices from before it was bought
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
- #9945 @Jackmeson1 による — マージされずにクローズ
評価
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 初心者へのやさしさ
- 25/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 停滞
- 技術スタック
- csharp
- 領域
- backend, fintech-quant
調査の方向性
Read Common/Statistics/TradeBuilder.cs, especially the Position and TradeState classes and the FillToFill full and partial close branches. The issue provides a standalone .NET reproduction; run it to confirm the reported MAE/MFE values, then use the same scenario to check the fix. Done means lot B reports MAE 0 and MFE 4 while single-lot behavior remains the same.
索引モデルが issue の本文から書いたものです。
説明
Expected Behavior
With FillGroupingMethod.FillToFill, each closed trade pairs an entry fill with an offsetting fill, so its MAE and MFE should come from the prices seen while that trade was open, from its entry fill to its exit.
Example, FIFO, zero fees: buy 1 @ 100; the price falls to 90; buy 1 more @ 95 (lot B); the price rises to 99; sell 2 @ 99. Lot B's prices are 95, 99 and 99, so its MAE is 0 and its MFE is 99 − 95 = 4, the same as lot B traded on its own.
Actual Behavior
On master f3d841e, lot B reports MAE −5 and MFE 5:
lot entered at 100, exit 99: P&L -1.0, MAE -10.0, MFE 0.0
lot entered at 95, exit 99: P&L 4.0, MAE -5.0, MFE 5.0
lot B alone: P&L 4.0, MAE 0.0, MFE 4.0
TradeBuilder keeps one MinPrice/MaxPrice per position (Common/Statistics/TradeBuilder.cs, the Position class, updated in SetMarketPrice, lines 152-161) and resets it only when a position opens or flips. The FillToFill close branches compute every pending trade's MAE and MFE from those position-wide extrema (lines 331-332 for a full close, 353-354 for a partial close). A lot added to an open position therefore inherits the low of 90 and the high of 100 that were seen before it was bought: (90 − 95) and (100 − 95). Since #9249 the end-trade drawdown is already tracked per trade (TradeState), so a lot's drawdown and its excursions currently come from different price windows.
Potential Solution
Keep a minimum and maximum price per pending trade in TradeState, starting at its entry fill and updated wherever the position extrema are updated today (market prices, fills, splits), and use them for MAE and MFE in both FillToFill close branches. A single-lot trade gives the same values as now. FlatToFlat and FlatToReduced leave MAE and MFE at zero and are not affected.
Reproducing the Problem
In a Linux shell at the root of a LEAN checkout at f3d841e (the Data folder is not needed), with a .NET 10 SDK:
dotnet new console --framework net10.0 --name ExcursionRepro --output ../ExcursionRepro
dotnet add ../ExcursionRepro/ExcursionRepro.csproj reference "$PWD/Common/QuantConnect.csproj"
Replace ../ExcursionRepro/Program.cs with:
using QuantConnect;
using QuantConnect.Orders;
using QuantConnect.Orders.Fees;
using QuantConnect.Statistics;
var spy = new Symbol(SecurityIdentifier.GenerateEquity("SPY", Market.USA, mapSymbol: false), "SPY");
var t0 = new DateTime(2024, 1, 2, 9, 30, 0);
OrderEvent Fill(int id, decimal price, decimal quantity) => new OrderEvent(id, spy, t0.AddMinutes(id), OrderStatus.Filled,
quantity > 0 ? OrderDirection.Buy : OrderDirection.Sell, price, quantity, OrderFee.Zero);
// Buy 1 @ 100; the price falls to 90; buy 1 more @ 95 (lot B); the price rises to 99; sell 2 @ 99.
var builder = new TradeBuilder(FillGroupingMethod.FillToFill, FillMatchingMethod.FIFO);
builder.ProcessFill(Fill(1, 100m, 1), 1m, 0m);
builder.SetMarketPrice(spy, 90m);
builder.ProcessFill(Fill(2, 95m, 1), 1m, 0m);
builder.SetMarketPrice(spy, 99m);
builder.ProcessFill(Fill(3, 99m, -2), 1m, 0m);
foreach (var trade in builder.ClosedTrades)
{
Console.WriteLine($"lot entered at {trade.EntryPrice}, exit {trade.ExitPrice}: P&L {trade.ProfitLoss}, MAE {trade.MAE}, MFE {trade.MFE}");
}
// Control: lot B on its own, with the same prices after its entry.
var alone = new TradeBuilder(FillGroupingMethod.FillToFill, FillMatchingMethod.FIFO);
alone.ProcessFill(Fill(2, 95m, 1), 1m, 0m);
alone.SetMarketPrice(spy, 99m);
alone.ProcessFill(Fill(3, 99m, -1), 1m, 0m);
var b = alone.ClosedTrades[0];
Console.WriteLine($"lot B alone: P&L {b.ProfitLoss}, MAE {b.MAE}, MFE {b.MFE}");
Then run dotnet run --project ../ExcursionRepro/ExcursionRepro.csproj --configuration Release. The trade lines are the block above. This is a standalone console reproduction, not part of a patch.
System Information
LEAN master f3d841e, Ubuntu 24.04 arm64, .NET SDK 10.0.403 (net10.0).
Checklist
- I have completely filled out this template
- I have confirmed that this issue exists on the current
masterbranch - I have confirmed that this is not a duplicate issue by searching issues
- I have provided detailed steps to reproduce the issue
- 主要言語
- C#
- スター
- 22.6k
- フォーク
- 5.4k
- 平均マージ
- 11時間 39分
- マージ済み PR(30日)
- 104
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile または Docker Compose ファイルあり
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
QuantConnect/Lean のほかの issue
-
priority:p2
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
QuantConnect/Lean#10216 ·
メンテナーはふだん 1 日以内に返信
-
priority:p2
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
QuantConnect/Lean#10206 ·
メンテナーはふだん 1 日以内に返信
-
priority:p2
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
QuantConnect/Lean#10199 ·
メンテナーはふだん 1 日以内に返信
-
priority:p2
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
QuantConnect/Lean#10198 ·
メンテナーはふだん 1 日以内に返信
-
priority:p2
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
QuantConnect/Lean#10197 ·
メンテナーはふだん 1 日以内に返信
QuantConnect/Lean の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
dotnet-outdated/dotnet-outdated#871 ·
メンテナーはふだん 1 日以内に返信
-
triage/untriaged
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
unoplatform/uno#26676 ·
メンテナーはふだん 1 日以内に返信
-
.NET bug
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
jhaizhou-ops/S2Arm#2177 · コメント 3 件 ·
メンテナーはふだん 1 日以内に返信
-
area-CodeGen-coreclr untriaged
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
dotnet/runtime#138181 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信