TradeBuilder FillToFill MAE/MFE of a scaled-in lot include prices from before it was bought
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
- #9945 di @Jackmeson1 — chiusa senza merge
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 25/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Ferma
- Stack tecnologico
- csharp
- Ambito
- backend, fintech-quant
Direzione di ricerca
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.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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
- Lingua principale
- C#
- Stelle
- 22.6k
- Fork
- 5.4k
- Merge medio
- 11h 39m
- PR unite (30g)
- 104
Preparare l'ambiente
Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.
- Include un Dockerfile o un file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di QuantConnect/Lean
-
priority:p2
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
QuantConnect/Lean#10216 ·
I maintainer di solito rispondono entro 1 giorno
-
priority:p2
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
QuantConnect/Lean#10206 ·
I maintainer di solito rispondono entro 1 giorno
-
priority:p2
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
QuantConnect/Lean#10199 ·
I maintainer di solito rispondono entro 1 giorno
-
priority:p2
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
QuantConnect/Lean#10198 ·
I maintainer di solito rispondono entro 1 giorno
-
priority:p2
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
QuantConnect/Lean#10197 ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di QuantConnect/Lean
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 66/100
dotnet-outdated/dotnet-outdated#871 ·
I maintainer di solito rispondono entro 1 giorno
-
triage/untriaged
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
unoplatform/uno#26676 ·
I maintainer di solito rispondono entro 1 giorno
-
.NET bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
I maintainer di solito rispondono entro 1 giorno
-
[问题] 导出文件时,提示后面还有1243390911步Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
jhaizhou-ops/S2Arm#2177 · 3 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
area-CodeGen-coreclr untriaged
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
dotnet/runtime#138181 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno