FlatToReduced LIFO trade starts too late when an older entry is partially consumed

Open Beginner friendly
#10,206 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
csharp
Domain
backend, testing

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

priority:p2
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

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from QuantConnect/Lean

All issues in QuantConnect/Lean

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.