1.6.8Order Types & Mechanics

Learn how the order book - matching engine works

1,912 words9 min readdifficulty · medium3 backlinks

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:
Figure — Learn how the order book  -  matching engine works

HOW the Matching Engine Works — Price/Time Priority

The matching rule (derive it)

Two orders cross (can trade) when: Buy priceSell price\text{Buy price} \ge \text{Sell price}

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.

  1. It takes liquidity from the best ask (101.0). Why? A market order accepts the best available price; best ask is lowest seller.
  2. Qfill=min(100,150)=100Q_{\text{fill}} = \min(100, 150) = 100. Why min? We only want 100.
  3. 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.

  1. Hit ask 101.0 (qty 150): fill min(250,150)=150\min(250,150)=150. Why? Best price first. Remaining need = 100.
  2. Move to next level 101.5 (qty 400): fill min(100,400)=100\min(100,400)=100. Why move up? First level exhausted, next-best price is now the best ask.
  3. Total filled 250. Average price = 150(101.0)+100(101.5)250=101.2\frac{150(101.0)+100(101.5)}{250}=101.2.

Worked Example 3 — Limit order that rests (adds liquidity)

A limit buy: 300 @ 100.7 arrives (best ask is 101.0).

  1. Can it cross? Need buy price ≥ sell price → 100.7101.0100.7 \ge 101.0? No. Why? Buyer won't pay 101, seller won't drop to 100.7.
  2. So it rests on the bid side at 100.7.
  3. 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?
A real-time sorted list of all resting limit orders: bids (highest price first) and asks (lowest price first), each with price + quantity.
Best bid vs best ask?
Best bid = highest price a buyer will pay; best ask = lowest price a seller will accept.
Formula for the spread?
Spread = Best Ask − Best Bid.
Formula for mid price?
(Best Bid + Best Ask) / 2.
Two orders can trade when?
Buy price ≥ Sell price.
At what price does a match execute?
At the resting (maker) order's price; the incoming taker accepts it.
Quantity filled in one match?
min(incoming qty, resting qty).
What is price–time priority?
Match by best price first; if prices tie, earlier order (FIFO) fills first.
Maker vs taker?
Maker adds liquidity (order rests in book); taker removes liquidity (crosses an existing order).
What is slippage?
When a large order walks through multiple price levels, giving a worse average price than the top of book.
Does a market order guarantee price?
No — it guarantees execution, not price.
A limit buy below best ask does what?
Rests on the bid side (can't cross); may become new best bid and tighten the spread.

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

split into

split into

sorted highest first

sorted lowest first

Ask minus Bid

Ask minus Bid

averaged

averaged

ranks orders by

triggers when Buy >= Sell

fills at resting price

Qfill = min of both

Order Book

Bid Side buyers

Ask Side sellers

Best Bid

Best Ask

Spread

Mid Price

Matching Engine

Price-Time Priority

Orders Cross

Executed Trade

Fill Quantity

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.

Test yourself — Order Types & Mechanics

Connections