TradeBuilder FillToFill MAE/MFE of a scaled-in lot include prices from before it was bought

Aperta
#9,944 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

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

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 master branch
  • 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

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di QuantConnect/Lean

Tutte le issue di QuantConnect/Lean

Issue simili

Altre issue su C#

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.