When you place an order, the exchange's computer (the matching engine) pairs it with an opposite order using a simple rule called price-time priority: the best price trades first, and among orders at the same price, the one that arrived earliest trades first. Every trade on NSE and BSE happens this way, automatically, in fractions of a second.
What is price-time priority?
Think of it as two queues. All buy orders line up on one side, all sell orders on the other. Each queue is sorted twice:
- Price first. A buyer offering more moves ahead of buyers offering less. A seller asking less moves ahead of sellers asking more.
- Time second. Among orders at the exact same price, whoever placed their order first stands ahead.
The engine constantly checks the front of both queues. The moment the highest buy price meets the lowest sell price, a trade happens. The gap between those two front prices is the spread. See what are bid, ask, and the bid-ask spread?
Who fills first, a worked queue scenario
Say shares of Kaveri Motors (a fictional company) are trading around ₹100.
- At 10:15:02, Priya places a limit order to buy 100 shares at ₹100.
- At 10:15:09, Vikram places a limit order to buy 200 shares, also at ₹100.
Both are at the same price, so time decides: Priya stands ahead of Vikram in the ₹100 queue.
Now Ramesh sends a sell order for 150 shares at ₹100. The engine works down the queue:
| Step | What happens | Remaining to sell |
|---|---|---|
| 1 | Priya's full 100 shares fill at ₹100 | 50 |
| 2 | Vikram gets 50 of his 200 shares at ₹100 | 0 |
Priya filled completely because she was first. Vikram got a partial fill, 50 done, 150 still waiting in the queue. If Anjali had instead bid ₹100.05, she would have jumped ahead of both, because price beats time.
What is a partial fill?
An order does not have to trade in one go. If only part of the opposite quantity is available at your price, that part trades and the rest stays pending (for order types that allow it). You might see one order produce several trades at slightly different times. This is normal. It just means the engine matched you against multiple counterparties.
Where does a market order fit in?
A market order skips the price queue entirely: it says "fill me now at whatever the other side is asking". The engine walks it through the opposite queue, best price first, until the quantity is done. A limit order, by contrast, joins the queue at your price and waits its turn.
All of these waiting orders together form the exchange's order book. See what is an order book?
Things to keep in mind
- Being early matters at a given price. Two identical orders can have very different outcomes purely because of a few seconds' difference.
- A partial fill is not an error. The unfilled portion stays live until it trades, you cancel it, or its validity runs out.
- Improving your limit price by even one tick moves you into a fresh queue ahead of everyone at the old price. That is why prices "tick" up and down through the day.
- The matching engine is fully automated and anonymous. You never know (and never need to know) who is on the other side of your trade.
Read next
What is an order book? — Matching happens against a queue. This is what that queue looks like.