A FIFO simulator can give you a false fill even when its arithmetic looks right. The bug often starts upstream: the feed sends a trade and a book update for the same event, and the simulator treats them as two separate removals.
This article was drafted with an AI writing tool. The examples and test fixtures use synthetic order flow.
1. Normalize the feed before updating queue state
Consider a buy order with 65 contracts ahead of it. A trade consumes 20 contracts at that price, and a depth update shows the same 20-contract reduction. After processing both messages, 45 contracts should still be ahead. A simulator that processes the trade and then records another 20-contract cancellation leaves only 25 ahead. On the next 40-contract trade, the faulty simulator can fill a 10-contract order that should still have five contracts ahead of it.
Use the venue's feed specification to relate trades and depth updates. Preserve exchange event time, strategy receive time, feed sequence, and the IDs of source messages behind each normalized event. When an aggregated update cannot be decomposed reliably, retain that uncertainty instead of labeling the entire decrease a cancellation. HftBacktest documents how its probabilistic queue model accounts for trade volume when processing depth changes. Queue model reference
2. Test the exact boundary between waiting and filling
For one simulated order on a visible FIFO book, track quantity ahead and own quantity remaining. A confirmed or modeled cancellation ahead reduces the first value. A same-price aggressive trade consumes that quantity first. The remaining trade volume can fill your order, up to its remaining size.
def apply_trade(ahead, remaining, trade_size):
consumed_ahead = min(ahead, trade_size)
fill = min(remaining, trade_size - consumed_ahead)
return ahead - consumed_ahead, remaining - fill, fill
def test_fill_boundaries():
# Start with 100 ahead, 10 resting, and an aggressive trade of 50.
# The expected tuple is (ahead after trade, own remaining, fill).
scenarios = [
(0, (50, 10, 0)),
(50, (0, 10, 0)),
(55, (0, 5, 5)),
(60, (0, 0, 10)),
]
for canceled_ahead, expected in scenarios:
assert apply_trade(100 - canceled_ahead, 10, 50) == expected
def test_partial_fill_carries_forward():
ahead, remaining, fill = apply_trade(45, 10, 50)
assert (ahead, remaining, fill) == (0, 5, 5)
assert apply_trade(ahead, remaining, 7) == (0, 0, 5)
def test_normalized_trade_sequence():
# The first 20-contract trade and its depth update refer to one event.
ahead, remaining, fill = apply_trade(65, 10, 20)
assert (ahead, remaining, fill) == (45, 10, 0)
assert apply_trade(ahead, remaining, 40) == (5, 10, 0)
if __name__ == "__main__":
test_fill_boundaries()
test_partial_fill_carries_forward()
test_normalized_trade_sequence()
The boundary case is the second row: 50 contracts ahead and a 50-contract trade. Your order reaches the front of the queue with zero fill. The trade has no volume left to allocate.
The last test operates on already normalized input. To test the adapter, build a fixture from the venue's actual message format with one trade and its associated depth update. Assert that the adapter emits one trade and zero extra cancellation volume.
3. Log enough to explain an execution
Record effective order arrival separately from the order send timestamp. Keep a resting order eligible for execution until its cancellation takes effect at the venue. Apply the instrument's rules for modifications and replenishment. In a simulation with multiple resting orders, give each aggressive trade a shared volume budget. Total allocation across those orders cannot exceed the trade's size.
For each event that changes a simulated order, log the source event ID, quantity ahead before the event, any cancellation credited ahead, trade quantity, fill quantity, and own quantity remaining. When results disagree between two runs, those fields let you answer whether the discrepancy came from event ordering, a queue assumption, or fill allocation.
For a KAEL AI execution-research example, run the same order instructions against a visible queue reconstructed from order-level data and against a version of that feed aggregated by price. Review disagreements event by event. That makes it possible to spot where an estimator systematically grants early fills before those fills make their way into a strategy report. CME Group's Market by Order FAQ
Top comments (0)