Rebuild your fills into round-trip trades before any journal stats run

Partial fills, scale-ins and partial exits make a raw broker export misstate win rate and R. A script groups fills into flat-to-flat trades and reconciles them with the broker's realised total, and only those trades feed your stats.

How it works

  1. Export executions (individual fills), not a trade summary, from your broker. Each row should carry the time, side, quantity, price, fees and an execution id.
  2. A script groups fills by account and instrument into round trips: a trade opens when the position leaves zero and closes when it returns to zero. Scale-ins and partial exits stay inside the same trade.
  3. For closed round trips, the net result, average entry and exit, and R don't depend on how partial exits are matched to entries. If you also keep a per-exit breakdown (useful for tax lots), pick one matching method, first-in first-out or average cost, write it in your journal settings and never switch it silently. A change gets a dated note.
  4. For each round trip, the script records open and close times, total quantity, average entry and exit, fees, net result and the number of fills, plus R from the initial risk you recorded at the first entry.
  5. It checks its own work. The total net result across round trips must match the export's realised total within a small tolerance, every execution id is used exactly once, and duplicates from re-imports are dropped and listed.
  6. Only reconciled round trips go into your trade review and weekly stats. The agent reads the result and never regroups fills itself.

Copy-paste prompt

Write a Python script that turns my broker's execution export into round-trip trades. It gives no trading advice and never suggests trades or securities.
Inputs: {{executions_csv}} (execution_id, account, instrument, time, side, quantity, price, fees), the broker's realised total for closed trades {{broker_realised_total}}, {{initial_risk_file}} with the risk I planned at each first entry.
The script must:
1. Group fills by account and instrument into flat-to-flat round trips, keeping scale-ins and partial exits inside one trade.
2. Output, for each round trip: trade_key, open_time, close_time, total_quantity, avg_entry, avg_exit, fees, net_result, fill_count and R.
3. Drop exact duplicate execution_ids and list them.
4. Fail with a clear message if the sum of net_result for closed round trips differs from {{broker_realised_total}} by more than {{tolerance}}, or if any execution is unused or used twice.
5. Mark positions that are still open as open, with no R, and leave them out of the reconciliation.
6. Optionally, produce a separate per-exit breakdown using {{method}} (first-in first-out or average cost). Changing {{method}} must not change any round-trip field.
Include test cases for a scale-in, a partial exit and a duplicate import, with expected outputs I can check by hand.

Guardrails

  • Tested code does the grouping, and the model never regroups fills. The matching method for any per-exit breakdown is written down and never changes round-trip totals or R.
  • Round trips must reconcile with the broker's realised total before any stats use them.
  • Each execution is used exactly once, and duplicates from re-imports are listed and dropped.
  • Read-only on your exports, with no broker connection that can place orders.

What can go wrong

A trade-level export looks easier, but it already hides the fills. A position built in three steps shows as one entry with an average price, and R measured against that average isn't the risk you planned at the first entry. The matching method only matters for a per-exit breakdown: first-in first-out and average cost split the same round trip's result differently across its exits, so switching methods mid-year makes those breakdowns hard to compare, even though the round-trip totals stay the same. The reconciliation is what catches a missed fill or a double import.

Why it helps

Every journal statistic, whether win rate, average R or holding time, assumes you know where one trade ends and the next begins. A raw fill list doesn't say. One position built in three steps and trimmed twice can show up as five "trades", and a re-imported file can quietly double a month. Rebuilding round trips with tested code and reconciling them against the broker's own total means every number downstream starts from the same, checkable list of trades.

How it fits with other entries

This is the step before the post-trade review (aft-0006), the weekly stats (aft-0007) and rules-first tagging (aft-0035): they all assume clean trades. The cost entry (aft-0048) matches fees and conversion to the round trips built here.

Where this comes from

A public post by @zbtcslayer on X about a free, open-source, self-hosted trade journal whose broker import groups fills into round-trip trades. Its public README and importer docs, linked below, describe the flat-to-flat grouping. We link the documentation for the method only and don't recommend any product. The reconciliation checks and the matching-method rule are ours.

Source and references

Based on a public post by @zbtcslayer on X about a free, open-source, self-hosted trade journal whose read-only broker import rebuilds fills into round-trip trades. The project's public documentation describes grouping partial fills and scale-ins into flat-to-flat trades. The reconciliation checks and the single written matching method are our adaptation.

Original: Thebtcslayer on x

Spotted an error, or want your post removed? Email hello@agentsfortraders.com.

Educational content only. Not investment advice or a personal recommendation. No tickers, signals or trade alerts. Trading involves risk of loss.

Conteúdo exclusivamente educativo. Não constitui aconselhamento nem recomendação de investimento. Sem tickers, sinais ou alertas de negociação. Negociar envolve risco de perda.

Related use cases