WHY the sort order? The exchange must always trade at the best available price for the incoming party. So the algorithm only ever needs to look at the top of each queue — the highest bid and lowest ask. Sorting guarantees an O(1) peek at the "best" prices.
There are two fundamental order types, and understanding the difference is 80% of microstructure:
HOW a market buy of size Q executes (the "walk"):
It eats the ask side from the top down. It fills against the best ask first; if that level's depth is exhausted, it moves to the next-higher ask price, and so on until Q is filled. This is why big orders get worse average prices — they "walk up the book." Note: each partial match only removes the matched quantity from a resting order; any leftover depth at that level stays.
WHY OBI predicts direction: If Vb≫Va, many buyers are waiting but few sellers — the next small market sell can't push price far, while a market buy easily lifts the ask. Price tends to drift up. OBI →+1 = buy pressure; OBI →−1 = sell pressure. It's a weak, fleeting predictor because the book changes every millisecond.
Imagine a lemonade stand market. On one wall, kids write "I'll pay 2foracup!"(thehighestpriceatthetop).Ontheotherwall,sellerswrite"I′llsellfor2.10!" (lowest at top). The little gap between them ($0.10) is the spread. If a thirsty kid says "give me lemonade NOW at any price," they grab the cheapest seller first, then the next cheapest, paying a bit more each cup — that's walking the book. If a seller had 10 cups but the kid only wanted 4, only 4 cups are sold and the other 6 stay written on the wall. If way more buyers are waiting than sellers, prices will probably creep up soon. The book is just everyone's notes about what they'll do, and it keeps changing as kids add or erase their notes.
Price Discovery — how crossing orders set the "true" price.
What is a limit order book?
A continuously-updated, price-ranked record of all resting limit orders, split into a bid side (highest price at top) and an ask side (lowest price at top).
What is the best bid and best ask?
Best bid = highest price a buyer will pay; best ask = lowest price a seller will accept.
Define the spread and mid price.
Spread s=a−b; mid m=(a+b)/2, where a=best ask, b=best bid.
Maker vs taker?
Maker posts a resting limit order (adds liquidity); taker sends a marketable/market order that removes liquidity by crossing the spread.
Why do large market orders suffer slippage?
They exhaust the best level then execute against progressively worse price levels ("walking the book"), raising the average execution price above the best quote.
When two orders match, what quantity is removed from the book?
Only the matched (overlapping) quantity = min of the two sizes; any unfilled remainder of the larger order stays resting.
Formula for average execution price when walking the book?
pˉ=C/Q where C=∑i=1k−1piqi+pkr, r=Q−∑i=1k−1qi.
Order book samajhna market microstructure ka dil hai. Socho ek table ke do side hain: ek taraf saare buyers apni highest price likhte hain (bid side, sabse upar sabse zyada price), doosri taraf sellers apni lowest price likhte hain (ask side, sabse upar sabse kam price). In dono ke beech ka gap hi spread hai, aur beech ka point mid price. Jab kisi buyer ki price kisi seller ki price ko chhoo leti hai (cross ho jaati hai), tab trade hota hai — lekin sirf matched (overlapping) quantity hi book se hatti hai. Agar ek order bada hai toh uska bacha hua hissa book me resting reh jaata hai.
Do type ke orders hote hain. Limit order matlab tum price fix karte ho aur agar turant match nahi hua toh woh book me "rest" karta hai — tum maker ban jaate ho (liquidity add karte ho). Market order matlab "abhi kharido/becho jo bhi price ho" — yeh spread cross karke resting orders ko kha jaata hai, tum taker ban jaate ho. Bada market order book ko walk karta hai: pehle best price kha gaya, phir agla thoda mehnga level, phir uske baad — isiliye average price kharaab ho jaati hai, ise slippage kehte hain.
Ek important signal hai order-book imbalance (OBI) = (Vb−Va)/(Vb+Va). Agar bid side pe volume bahut zyada hai toh short-term me price upar ja sakti hai, kyunki buyers zyada hain sellers kam. Lekin yaad rakho yeh signal bahut fleeting hai — book har millisecond change hoti hai.
Ek common galti: log sochte hain woh mid price pe trade karenge. Nahi! Market buy me tum ask pay karte ho, market sell me tum bid receive karte ho. Mid sirf ek reference hai. Doosri galti: log sochte hain match hote hi dono orders gayab ho jaate hain — nahi, sirf min(size) trade hoti hai, bacha hua hissa resting rehta hai. Aur jo depth screen pe dikh rahi hai woh bhi poori sachai nahi — hidden/iceberg orders hote hain, isliye asli cost aksar zyada aati hai. Yeh cheezein samajh loge toh trading cost bahut behtar control kar paoge.