Reference / Trade execution

Holdings

An account's holdings from one trade are whether it bought or sold, how much is held, and the average price paid to build it. There is one record per trade and account, so two trades in the same pair are kept apart.

Contents

A fill on the same side as the record adds what that fill delivered, and the average price is worked out again over the new total. A fill on the other side takes the same figure away.

When a record goes away

A record is removed in three ways.

A reducing fill that brings the amount to zero drops it. One that would take the amount below zero is written to the log, set to zero, and then dropped by that same test โ€” so going past zero and landing on it both end with no record.

And closing the trade drops it whatever it holds. A trade can be closed with a small amount left, too small to sell on its own, and the record goes with the close rather than with a fill.

Only a reducing fill reaches the first two tests, and a record holding nothing still meets them: the next fill on the other side takes it below zero, where it is set to zero and dropped like any other.

Closing without the venue's sizes

Whether a reducing fill closes the position outright is normally decided against the venue's smallest step: less than a step left is too little to trade and the position is closed.

Where the venue's sizes are not to hand, that test answers yes for any reducing fill reaching it. The remainder is then whatever was left, tradeable or not โ€” so a position can be written closed with an amount in it the venue would have accepted.

Source evidence
---
row: P2-16
baseline: 49aa659
created: 2026-09-25 20:33 UTC
modified: 2026-10-14 11:20 UTC
evidence:
  - claim: "Kept per trade and account, not per symbol"
    source: "`trading_state.py` 482, the record built at 489-497"
  - claim: "A fill on the same side adds what it delivered and reckons the average over the new total"
    source: "`trading_state.py` 500-505"
  - claim: "A fill on the other side takes that away"
    source: "`trading_state.py` 506-507"
  - claim: "A reducing fill reaching zero drops the record"
    source: "`trading_state.py` 517-522"
  - claim: "One going below zero is logged, set to zero, and dropped by that same test"
    source: "`trading_state.py` 508-522"
  - claim: "Closing the trade drops the record whatever it holds"
    source: "`trading_state.py` 571-572"
  - claim: "A trade can be closed with an amount still standing"
    source: "`execution_manager.py` 10891-10899, the close emitted at 10806-10816"
  - claim: "A reducing fill leaving less than the venue's step closes the position"
    source: "`execution_manager.py` 10899"
  - claim: "With the venue's sizes absent, the test answers yes for any reducing fill that reaches it"
    source: "`execution_manager.py` 10896-10897, reached from 10891"
---

Edit this sourceCheckout revision ยท 7381aa49
Reference / keep your place
Find a passage