A market simulator in Rust, checked order by order against real data
A deterministic exchange, a latency-aware simulator and a market-making ladder, with a fill model tested on real Bitstamp and Nasdaq order data.
- Bitstamp snapshots reproduced
- 1,136 of 1,136
- Real Bitstamp fills recovered
- 292 of 298 (99.6% of volume)
- False fills, 801,154 Nasdaq orders
- 0
- Tests
- 213 Rust, 27 Python
- Rust
- Python
hft-sim is a research platform for market microstructure: how orders become trades and prices. It has a deterministic exchange, a simulator in which every message takes time to arrive, trading agents and a ladder of market-making strategies. I wanted to know how market makers fare against latency, informed traders and competition, and whether a backtest’s rule for when a resting order fills survives real exchange data.
How it works #
- The exchange matches orders by price-time priority: best price first, then first come, first served.
- The simulator times every hop between a trader and the exchange, so traders see only a delayed copy of the book. Runs are bit-reproducible from one seed.
- A background market of noise traders, value investors and split-up large orders is calibrated to five statistical regularities of real markets.
- The market makers form a ten-step ladder, from a fixed spread to the Avellaneda–Stoikov and Guéant–Lehalle–Fernandez-Tapia models, with formulas transcribed from the papers and checked numerically.
What the simulator showed #
Eight research questions, each on two backgrounds with 8 seeds and 95% intervals, about this simulated market only.
- More inventory aversion paid. Raising a market maker’s risk aversion from 3.5 to 14 added $9.2 per two-minute window [5.9, 12.5] with less inventory. The gain came from less adverse selection (trading against flow that knows where the price is going), even without informed traders: the background’s split orders are persistent.
- Queue position dominates fills. A resting order at the front of the queue filled about 90% of the time; behind 500 shares, about 10%.
- Speed mattered little here. A remote maker sent three times as many cancels too late (8% against 2–3%) but lost little profit: it had no news to race on.
- The maker profited almost everywhere, even against 60 informed traders. That is a property of the simulator, and it is why nothing here is a profitability claim.
Checking the fill model on real data #
Each real order was replayed leave-one-out: treated as hypothetical, run through the stream without itself, and its modelled fills compared with what really happened.
| Data | Check | Result |
|---|---|---|
| Bitstamp BTC/USD, 19 minutes, 365,180 order events | Rebuilt book against the exchange’s own snapshots | 1,136 of 1,136 reproduced |
| 172,862 resting Bitstamp orders | Fill model, four variants | No false, early or oversized fill |
| The same orders, best variant | Real fills found at the right millisecond | 292 of 298, 99.6% of volume |
| Nasdaq BX ITCH day (30 Dec 2019), 801,154 orders | Fill model | No false, early or oversized fill |
The best variant uses Bitstamp’s published taker orders (the incoming orders that trade against resting ones) and rebuilds history as if each order had never existed. Without takers, as on Nasdaq’s feed, the conservative rules give a lower bound: a third of Bitstamp’s fills on time, and 93% of BX volume eventually. The BX day’s 29.2 million messages also pass every book-consistency check.
The checks exposed three optimistic rules in my model, each now fixed. On BX, 0.2% of executions looked like queue jumping; every one was an order the exchange ranks at a better price than it displays (its Price to Comply rule).
Limits #
- The default background makes one-second returns mean-revert, so every study also runs on a second, zero-intelligence background.
- The real data is one 19-minute crypto window and one day on a small Nasdaq venue. The LOBSTER adapter is tested only on simulated files, because LOBSTER’s samples now need an academic application.
- Replay cannot show how the market would have reacted to an extra order.
How it’s built #
A Rust workspace whose core crates have no external dependencies and no unsafe code, plus a Python layer for scenario grids with optional PyO3 bindings that export runs to Parquet. The 213 Rust and 27 Python tests include differential fuzzing against a naive reference exchange (about 18 million commands) and mutation testing: of 132 deliberately planted bugs, 130 were caught and the other two provably cannot change any output. The engine handles about 8.6 million messages a second with 1,000 resting orders on a laptop.