Add, Cancel And Execute: The Event Alphabet
Every limit order book, however complex it looks, is built from just a handful of message types. Learn the alphabet and you can read any exchange feed.
Prerequisites: Order Book Mechanics
An exchange does not send you a picture of the order book. It sends you a stream of small messages, and it is your job to turn that stream into a book. Almost every message on almost every exchange is one of four things: an order arrives, an order leaves, an order is matched, or an order's terms change.
Add. A new limit order joins the book. The message carries an order ID, side, price and size. Nothing trades — the order just takes a place in the queue at its price level.
Cancel. A resting order is pulled, in whole or in part. The size at that price level drops by exactly the cancelled amount. A cancel-replace is common in practice: a trader pulls an order and adds a new one in the same instant, usually to move price or add size, and it costs the order its queue priority (see Queue Position and Priority).
Execute (trade/fill). An incoming order matches a resting one. Size is removed from the resting side, and — this is the detail beginners miss — the size removed is taken from the front of the queue, not averaged across it.
Modify. Some venues allow an in-place size decrease without loss of priority (you can only shrink, never grow, and never move price). A price or size increase is always a cancel-replace.
Walking a book through a message sequence
Start with an empty book at 100.00/100.01. Five messages arrive in order:
- Add buy 500 @ 100.00 (order A) → bid queue: [A:500]
- Add buy 300 @ 100.00 (order B) → bid queue: [A:500, B:300], total 800
- Cancel A, 200 shares → bid queue: [A:300, B:300], total 600. A keeps its place at the front; only its size shrank.
- Execute: an incoming sell for 400 hits the bid → the engine eats A first (300 filled, A gone), then B (100 filled, 200 left) → bid queue: [B:200]
- Add buy 150 @ 100.00 (order C) → bid queue: [B:200, C:150], total 350
Notice step 4: the trade print says "400 @ 100.00", but it was really two fills against two different resting orders, in strict time priority. A book reconstructed from trades alone would never know that — you need the message stream.
The book you see is a running sum of adds minus cancels minus executes, applied strictly in the order the exchange sent them. Get the order wrong and your reconstructed book will disagree with the real one within seconds.
When you see a trade print, always ask "how many resting orders did that eat, and in what order?" — the print alone never tells you, only the message stream does.
Every book-reconstruction bug traces back to mishandling one of these four events, usually cancels: forgetting that a cancel can be partial, or applying it to the wrong price level after a modify. See Rebuilding The Book From A Message Feed for how to build the full pipeline.
Related concepts
Practice in interviews
Further reading
- Nasdaq TotalView-ITCH 5.0 Specification
- Harris, Trading and Exchanges (ch. 4)