TradeBuilder FillToFill MAE/MFE of a scaled-in lot include prices from before it was bought
Maintainers usually reply within 1 day
Nobody has claimed this yet.
- #9945 by @Jackmeson1 — closed without merging
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 25/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Stale
- Tech stack
- csharp
- Domain
- backend, fintech-quant
Research direction
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.
Written by the indexing model from the issue text.
Description
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
- Dominant language
- C#
- Stars
- 22.6k
- Forks
- 5.4k
- Avg merge
- 11h 39m
- Merged PRs (30d)
- 104
Getting set up
Starts the project's dev container in your browser, under your own GitHub account.
- Ships a Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from QuantConnect/Lean
-
priority:p2
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
QuantConnect/Lean#10216 ·
Maintainers usually reply within 1 day
-
priority:p2
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
QuantConnect/Lean#10206 ·
Maintainers usually reply within 1 day
-
priority:p2
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
QuantConnect/Lean#10199 ·
Maintainers usually reply within 1 day
-
priority:p2
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
QuantConnect/Lean#10198 ·
Maintainers usually reply within 1 day
-
priority:p2
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
QuantConnect/Lean#10197 ·
Maintainers usually reply within 1 day
All issues in QuantConnect/Lean
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
dotnet-outdated/dotnet-outdated#871 ·
Maintainers usually reply within 1 day
-
triage/untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
unoplatform/uno#26676 ·
Maintainers usually reply within 1 day
-
.NET bug
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
jhaizhou-ops/S2Arm#2177 · 3 comments ·
Maintainers usually reply within 1 day
-
area-CodeGen-coreclr untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
dotnet/runtime#138181 · 1 comment ·
Maintainers usually reply within 1 day