Learn how the order book - matching engine works
WHAT is an Order Book?
Key derived quantities (from first principles):
- The best bid = highest price any buyer will pay = top of bid side.
- The best ask = lowest price any seller will accept = top of ask side.
- The spread is what it costs to "cross" instantly:

HOW the Matching Engine Works — Price/Time Priority
The matching rule (derive it)
Two orders cross (can trade) when:
Why this inequality? A buyer offering ₹101 is happy to pay ₹100 or less; a seller asking ₹100 is happy to receive ₹101 or more. So any buy price ≥ any sell price means both are satisfied → a trade happens.
At what price does it trade? At the price of the order already resting in the book (the passive/maker order). The incoming aggressor (taker) accepts the resting price.
Worked Example 1 — A simple cross
Book before:
| Bids (buy) | Asks (sell) | ||
|---|---|---|---|
| Price | Qty | Price | Qty |
| 100.5 | 200 | 101.0 | 150 |
| 100.0 | 300 | 101.5 | 400 |
- Best bid = 100.5, best ask = 101.0 → Spread = 0.5, Mid = 100.75.
A market buy for 100 shares arrives.
- It takes liquidity from the best ask (101.0). Why? A market order accepts the best available price; best ask is lowest seller.
- . Why min? We only want 100.
- Trade prints at 101.0. Ask 101.0 now has 50 left.
Result: buyer pays 101.0, spread unchanged (best ask still 101.0).
Worked Example 2 — Sweeping multiple levels
Same book. Now a market buy for 250 shares arrives.
- Hit ask 101.0 (qty 150): fill . Why? Best price first. Remaining need = 100.
- Move to next level 101.5 (qty 400): fill . Why move up? First level exhausted, next-best price is now the best ask.
- Total filled 250. Average price = .
Worked Example 3 — Limit order that rests (adds liquidity)
A limit buy: 300 @ 100.7 arrives (best ask is 101.0).
- Can it cross? Need buy price ≥ sell price → ? No. Why? Buyer won't pay 101, seller won't drop to 100.7.
- So it rests on the bid side at 100.7.
- New best bid = 100.7 → new spread = 101.0 − 100.7 = 0.3 (tighter!).
This trader is a maker (added liquidity). Someone crossing it later is a taker.
Common Mistakes
Flashcards
What is an order book?
Best bid vs best ask?
Formula for the spread?
Formula for mid price?
Two orders can trade when?
At what price does a match execute?
Quantity filled in one match?
What is price–time priority?
Maker vs taker?
What is slippage?
Does a market order guarantee price?
A limit buy below best ask does what?
Recall Feynman: explain to a 12-year-old
Imagine a big whiteboard. On the LEFT, kids write "I'll buy a card for ₹10, ₹9, ₹8..." (highest on top). On the RIGHT, kids write "I'll sell for ₹11, ₹12..." (lowest on top). A referee watches. The moment the top buyer's price is as high as the top seller's price, he shouts "MATCH!" and swaps the card for money at the price that was written first. If two kids wrote the same price, whoever wrote it earlier gets to trade first. If you say "just buy me one NOW," the referee grabs the cheapest seller — but if you want lots, he takes the cheapest, then the next cheapest, so you pay a bit more on average. That's the whole game.
Connections
- Limit Order vs Market Order — the two order types that populate and hit the book.
- Bid-Ask Spread & Liquidity — what the spread tells you about a stock.
- Slippage & Market Impact — cost of walking the book.
- Maker-Taker Fee Model — exchanges reward liquidity providers.
- Order Types & Mechanics — parent chapter.
- Level 2 Market Data — seeing the full depth of the book.
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Socho exchange ek bade market jaisa hai jahan hazaaron log ek saath "main kharidunga" aur "main bechunga" chilla rahe hain. Is chaos ko sambhalne ke liye do cheezein hain: order book aur matching engine. Order book bas ek sorted list hai — ek taraf saare buyers (bids), jinme sabse zyada price dene wala sabse upar hota hai, aur doosri taraf sellers (asks), jinme sabse kam price maangne wala sabse upar. Sabse upar wale do prices ke beech ka gap hi spread kehlata hai, aur uska beech ka number mid price.
Matching engine referee hai. Uska rule simple hai: jab kisi buyer ka price kisi seller ke price se bara ya barabar ho jaye (Buy ≥ Sell), tab trade ho jaata hai. Trade us price par hota hai jo pehle se book me rest kar raha tha (maker), aur jo naya order aaya use taker kehte hain. Agar do orders ka price same ho, to jo pehle aaya (time priority, FIFO) usko pehle fill karte hain — order ka size badha hone se koi line nahi todta.
Yeh samajhna kyun zaroori hai? Kyunki market order price guarantee nahi karta, sirf execution guarantee karta hai. Agar tumhara order bara hai, wo pehle sabse sasta seller khaata hai, phir agla, phir agla — matlab tum average price zyada de doge. Isko slippage kehte hain. Chhote liquid stock me spread chhota hota hai (sasta trading), illiquid me bada. Toh order book padhna seekh lo — best bid, best ask, aur depth dekh kar tum samajh jaoge ki trade karna kitna mehnga padega.