FlatToReduced LIFO trade starts too late when an older entry is partially consumed
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 78/100
Research direction
Start in Common/Statistics/TradeBuilder.cs, in ProcessFillUsingFlatToReduced, and trace the LIFO partial-consumption branch described in the issue. Add regression coverage for the provided sequence of fills, checking that the closed trade starts at t0 and lasts two minutes. Confirm matched quantities, prices, and fees remain unchanged.
Written by the indexing model from the issue text.
Description
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.
- 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 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
-
priority:p2
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
QuantConnect/Lean#10191 · 1 comment ·
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