TradeBuilder drops commissions reported after the first execution of an order

Closed
#10,126 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

A pull request for this has already been merged.

  • #10188 by @quantconnect-kronos — merged

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
csharp
Domain
backend

Research direction

The fee cache is in TradeBuilder.ProcessFill, keyed by order ID in _ordersWithFeesAssigned; the reproduction is a C# snippet that builds a SPY partial fill followed by a status-only Filled event with a $1 commission and checks ClosedTrades[0].TotalFees. Start by reading how ProcessFill decides whether an order's fee has already been assigned and separate commission handling from execution and price updates. Done means the snippet reports 1, duplicate commission reports are still ignored, and a regression test covers a status-only completion after a partial fill.

Written by the indexing model from the issue text.

Description

priority:p2

Problem

TradeBuilder.ProcessFill records an order ID in _ordersWithFeesAssigned on its first execution, even when the reported fee is zero. Any commission reported by a later event for that order is then ignored by trade statistics.

For example, a partial buy execution can report no commission, followed by a final status-only event carrying the commission. After the position closes, ClosedTrades[].TotalFees is zero instead of the commission paid. This is independent of portfolio cash accounting: it concerns the fees attached to reconstructed trades.

Reproduction

var symbol = new Symbol(
    SecurityIdentifier.GenerateEquity("SPY", Market.USA, mapSymbol: false), "SPY");
var time = new DateTime(2024, 1, 2, 15, 0, 0, DateTimeKind.Utc);
var builder = new TradeBuilder(FillGroupingMethod.FillToFill, FillMatchingMethod.FIFO);
builder.SetSecurityManager(new SecurityManager(new TimeKeeper(time)));
var noFee = new OrderFee(new CashAmount(0m, "USD"));
var commission = new OrderFee(new CashAmount(1m, "USD"));

builder.ProcessFill(new OrderEvent(1, symbol, time,
    OrderStatus.PartiallyFilled, OrderDirection.Buy, 300m, 2m, noFee), 1m, 0m);
// The brokerage closes the order without another execution and reports its commission.
builder.ProcessFill(new OrderEvent(1, symbol, time.AddMinutes(1),
    OrderStatus.Filled, OrderDirection.Buy, 300m, 0m, commission), 1m, 1m);
builder.ProcessFill(new OrderEvent(2, symbol, time.AddMinutes(2),
    OrderStatus.Filled, OrderDirection.Sell, 301m, -2m, noFee), 1m, 0m);

Console.WriteLine(builder.ClosedTrades[0].TotalFees); // actual: 0; expected: 1

Verified with the QuantConnect.Common assembly built from master (0b5c3db88): the first closed trade has TotalFees = 0 for FillToFill, FlatToFlat, and FlatToReduced. The same missing commission remains with the zero-quantity statistics guards. It is not caused by those guards: the existing first-event fee cache already discarded it.

Expected result

Trade reconstruction must retain genuine commissions reported after an order's first execution, without inventing an execution or treating a status-only event's price as a market-price update. Existing protection against duplicate commission reporting must be preserved.

Suggestion: separate commission accounting from execution/price processing and define how incremental versus repeated order fees are handled. Add regression coverage for deferred commission, including a status-only completion after a partial fill.

Impact is limited to trade fee statistics; this reproduction does not imply missing portfolio cash charges.

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.