Handle partial fills without double-counting
An acknowledgement says the exchange accepted a request. Each fill changes inventory. Maintain an order record with requested size, settled fills and current venue status. A cancelled remainder can coexist with earlier fills.
Follow an order for ten units
The first fill is 4 at 100 and the second is 3 at 101. Cumulative executed size is 7 and weighted price is (4 × 100 + 3 × 101) / 7 = 100.4286. The remaining 3 then cancel. The inventory change is 7, and the final record contains 7 executed and 3 cancelled. Store commissions by fill and currency.
Use the venue’s stable execution identity to deduplicate updates. If you receive both a last-fill quantity and a cumulative quantity, count the new fill once and reconcile the cumulative total. Example fields in Binance’s executionReport distinguish the last execution quantity and accumulated quantity. Binance user data stream
Test races and reconnects
Play back fill A, fill B, duplicate B, then a cancellation acknowledgement. The inventory total should stay at 7. Play the sequence again with a fill arriving during cancellation and verify that the remaining amount reconciles to the final status. After reconnect, fetch any missing fills and compare total executed quantity before allowing another action on the order.