Building an Order Book from Scratch
25 Jan 2026 • Programming, Personal
I’ve read about market microstructure for years — spreads, adverse selection, market making — but never built any of it. I wanted to see how far I could get using Claude Code as a Socratic tutor.
The answer: further than I expected.
The Method
The approach was simple: derive concepts before learning their names. Don’t start with “let me explain adverse selection.” Start with a scenario and work through the logic.
Here’s how one exchange went:
Claude: You’re a market maker quoting BTC at $99,990 bid / $100,010 ask. Someone hits your bid for 10 BTC. What scenarios explain this trade?
Me: Either they need liquidity and I’m the cheapest offer, or they have information I don’t — the price is about to drop and they’re getting out.
Claude: If they’re informed, what happens to your position?
Me: I’m holding 10 BTC that’s about to be worth less. So the spread has to compensate for that risk — I can’t distinguish informed traders from noise traders, so I price in the expected loss.
Only after working through this did Claude name it: adverse selection. This pattern repeated. Queue position value. Volatility-adjusted quoting. Order flow toxicity. Each time: scenario first, reasoning second, terminology last. The concepts stuck because I’d built the intuition before attaching labels.
Why This Was Fast
Normally, learning market microstructure means reading textbooks, working through academic papers, maybe taking a course. A lot of background before you write any code.
With Claude as a tutor, the loop was tight: work through a concept conversationally, implement it, test it against an adversary that exploits the weakness, see what breaks, ask why, iterate.
The speedup came from two things. First, instant feedback on my reasoning — no waiting for office hours to find out I’d misunderstood something. Second, no context switching. I could go from “I don’t understand why inventory matters” to “here’s an implementation” to “here’s a test that proves the naive version fails” without leaving the conversation.
What I Built
Seven modules, each answering one question:
- How does a matching engine work? Order book with price-time priority
- Why does the spread exist? Informed trader adversary that exploits naive market makers
- What does speed cost? Latency simulator, stale quote sniper
- How to trade large orders? TWAP, VWAP, adaptive execution
- What predicts price? Book imbalance signals, alpha decay simulation
- How to provide liquidity? Three market makers with increasing sophistication
- What kills you? Risk limits, circuit breakers, health monitoring
The structure is adversarial: register a market maker and an informed trader, run the simulation, see who profits. A naive market maker bleeds money to the informed trader. The Avellaneda-Stoikov implementation survives by widening spreads when volatility spikes and skewing quotes to shed inventory. Watching that difference play out made the theory concrete.
What I Actually Learned
Spreads compensate for uncertainty. Quote too tight and informed traders pick you off. Too wide and you lose flow to competitors. The equilibrium spread is where these forces balance.
Speed is defensive. I’d always thought of HFT as racing to grab opportunities. It’s more about not getting picked off. A slow market maker with stale quotes is free money for anyone watching.
Signals decay because they work. When a signal predicts price movement, traders act on it, their trades move the price, and the signal stops working. This is why alpha is hard to keep — success contains the seeds of its own destruction.
The Avellaneda-Stoikov market maker was the best “aha” moment. The core idea: when you’re long, shade your ask down to shed inventory. When volatility is high, widen your spread because adverse selection is more dangerous. Simple rules, but watching it adapt where the naive version just bled made something click.
Running It
git clone https://github.com/davidrzs/OrderbookFromScratch
cd OrderbookFromScratch
uv sync
uv run python examples/replay_example.py~6,000 lines of Python across 32 files, plus a Rust order book. Each
module has a SPEC.md explaining the theory and a
SOCRATIC.md with the guided derivation exercises.
Code: github.com/davidrzs/OrderbookFromScratch
Have any feedback?
Please feel free to send me a mail! I would love to hear from you.