KARACHI: You press buy, and a moment later the screen says executed — at a price slightly better than you expected, or filled in two pieces, or not filled at all. Between your tap and that result stands one of the most elegant machines in finance: the matching engine. The Pakistan Stock Exchange, like modern exchanges everywhere, does not pair buyers and sellers by negotiation or favour. It runs a strict, published queue discipline called price-time priority. Once you understand it, almost every puzzling thing about your fills — the partial executions, the queue that never reached you, the price that printed a paisa better than your limit — becomes predictable.

The order book: two queues facing each other

Every order sent to the PSX — through a broker's terminal or an app routed via a broker — enters a central electronic order book for that share, maintained by the exchange's trading system. The book has two sides. Bids are orders to buy, each stating a quantity and the maximum price the buyer will pay (or, for a market order, a willingness to take the best available price). Offers, also called asks, are orders to sell, each stating a quantity and the minimum price the seller will accept. At any moment, the highest bid and the lowest offer in the book are the best bid and best offer, and the gap between them is the spread. A trade happens when a bid and an offer become compatible — when someone is willing to buy at a price at or above what someone else is willing to sell for.

The book is not a free-for-all; it is two queues with rules. Orders are anonymous — you see prices and quantities, not identities. Orders rest in the book until they are executed, cancelled, or expire under their time conditions at the end of the session or period specified. What your trading app shows as "market depth" is a window into this book, usually the best several levels of bids and offers; our next guide, on reading the order book, is devoted to interpreting that window. This article is about the engine behind it: given all those resting orders, who gets matched with whom, in what order, and at whose price?

Price priority: the best price goes first

The first rule is price priority. Among buy orders, the highest bid has first claim on any incoming sell order; among sell orders, the lowest offer has first claim on any incoming buy order. The logic is the market's core fairness promise: whoever offers the best price to the other side trades first. If you bid Rs 100 for a share while another investor bids Rs 100.50, the other investor is ahead of you — not because they are bigger, richer or better connected, but because they improved the price by fifty paisa. If you want to jump the queue, there is exactly one legitimate way: bid more. The same discipline applies in reverse to sellers undercutting each other towards the bid.

Price priority is also what builds the spread you see. Competition among buyers pushes the best bid up; competition among sellers pushes the best offer down; the spread narrows until, in a liquid share, it may be a few paisa wide, while in a neglected share it can yawn embarrassingly wide. Every improvement of the best bid or offer is a small public service: it tightens the market for everyone who follows. This is also why your order, even if it never executes, was not useless while it rested in the book — it was part of the displayed competition that shaped the price others traded at.

Time priority: first come, first served at the same price

The second rule decides ties. When two orders share the same price, time priority applies: the order that reached the book earlier executes first. If three buyers are all bidding Rs 100 — for 500, 1,000 and 200 shares, in that order of arrival — an incoming sale of 800 shares fills the first buyer completely (500) and the second partially (300 of their 1,000, leaving 700 still resting), while the third waits untouched. The queue is literal. This is why traders care about getting orders in early at a popular price level, and why modifying an order can be costly: under exchange rules, changing an order's price, or increasing its quantity, typically costs you your time position and sends you to the back of the queue at the new terms, because other investors should not be leapfrogged by an edit. Reducing quantity generally preserves priority — the principle being that you may claim less than your turn, but not more.

A worked example pulls both rules together. Imagine the book for a hypothetical share, prices in rupees, purely for illustration. Bids: 1,000 shares at 150.00 (arrived 10:01), 500 at 150.00 (arrived 10:03), 2,000 at 149.80. Offers: 800 at 150.20, 1,500 at 150.50. The spread is 150.00 to 150.20. Now a market order to sell 1,200 shares arrives. It takes the best bid first: 1,000 at 150.00 from the 10:01 order, then 200 from the 10:03 order at the same price — time priority within the level — leaving that second buyer with 300 still wanted at 150.00. The seller received 150.00 for the whole 1,200, the price of the resting orders, not some average invented by the system. If instead a buyer had sent a limit order to buy 1,000 at 150.50, it would have crossed the spread: matched first against the 800 offered at 150.20 (the best offer), then 200 at 150.50 — two fills, two prices, both determined by the resting orders' prices and order of arrival. This is the answer to the common beginner's surprise of receiving several execution prices for one order: your order walked the book, consuming offers level by level.

At whose price? The resting order sets it

Note what the examples imply: the trade price is the price of the order that was already resting in the book, not the price of the incoming order. If you send a limit buy at Rs 160 when the best offer is Rs 155, you will normally pay Rs 155 — the resting seller's price — and enjoy "price improvement" on the difference. Your limit is a ceiling on what you will accept, not a declaration of what you insist on paying. Conversely, a market order surrenders price choice entirely in exchange for certainty of execution: it will keep consuming book levels until filled, which in a thin book can mean surprisingly poor prices on later portions. That is the mechanical heart of the market-order-versus-limit-order decision covered in our guide to PSX trading sessions and order types: a limit order protects price but may never execute; a market order pursues execution but accepts the book as it finds it.

Sessions, opening prices and the auction idea

Matching runs continuously during the regular trading session — orders arrive, match instantly if compatible, otherwise rest. But a stock market also needs a way to start the day and to restart after interruptions. Exchanges handle these moments with call auction mechanisms: for a defined period, orders are collected without immediate matching, an indicative equilibrium price is calculated — the price at which the maximum volume can trade — and then all matches occur at that single price in one uncrossing. The PSX trading system has employed auction-style mechanisms for particular situations such as the pre-open and for certain segments or securities, with the details set by exchange rules and circulars. The virtue of an auction is fairness at moments of imbalance: after overnight news, hundreds of orders arriving in the first second would otherwise reward pure speed; an auction treats them as a batch and finds one price that clears the most volume. If your order fills at the opening print at a single uniform price alongside everyone else's, you have met the auction.

Related edge cases complete the picture. Orders carry time-in-force conditions — a day order dies at the session's end if unfilled. Certain order types hide or condition their display or execution; the menu of order types available to retail clients, including the stop-loss orders explained in the learn section, is defined by the exchange and your broker's system, and has changed over time as the trading platform has been upgraded. Trades executed are final records feeding the clearing process at the National Clearing Company of Pakistan (NCCPL), which nets obligations and arranges settlement on the cycle explained in our T+2 settlement guide. The matching engine decides who traded with whom at what price; clearing and settlement then make that promise good in cash and shares.

What matching rules mean for your behaviour

  • Your limit price is your queue ticket. Bidding one tick higher can move you ahead of a large queue; bidding at a crowded level means waiting behind everyone who arrived first, however large your order is.
  • Partial fills are normal, not errors. They mean the available quantity at your price ran out mid-order. The remainder rests (for a limit order) until matched, cancelled or expired.
  • Speed matters less than price for retail investors. Time priority rewards early orders at a level, but for an investor buying for months or years, a paisa of price improvement honestly outranks a second of speed. Do not let interface design convince you that investing is a reflex sport.
  • Thin books punish market orders. Where displayed depth is a few hundred shares, a market order for thousands will walk through several price levels. Split orders, use limits, or accept the cost knowingly.
  • Cancelling and replacing loses your place. Edit an order only when the new terms are worth the back of the queue.

There is a deeper point worth carrying away. Price-time priority is a public, neutral rule. It does not know your broker's size, your account value or your intentions, and in an anonymous central book, a housewife in Multan bidding the best price stands ahead of an institution bidding less. Most of what reformers mean by a "fair market" is engineered exactly here, in queue discipline, surveillance of the order flow and the integrity of the book — the plumbing examined throughout this series, from market makers and liquidity to the halts that pause the engine altogether in our guide to trading halts and suspensions. When Aktzien — currently being built — talks about clearer, low-cost access to the PSX, it is this same engine its users' orders will meet; no app changes the queue, and understanding the queue is understanding the market.

A note on currency of detail: trading hours, session structures, tick sizes, order types, price limits and auction arrangements on the PSX are set by exchange regulations and circulars and are revised from time to time, including with trading-system upgrades. Use this article for the enduring principles — price priority, time priority, resting-price execution — and verify today's operational specifics in the current PSX rulebook and your broker's documentation. This article is educational content, not financial advice.

Two refinements complete the picture for anyone watching executions closely. First, a single incoming order can generate several trades at several prices, as the worked example showed, and your broker's contract note will list them separately; this is the book being consumed level by level, not an error to dispute. Second, the matching engine processes orders in the sequence it receives them, down to fractions of a second, which is why two investors who swear they acted "at the same time" can receive different fills — one stood a moment earlier in the queue at the decisive price. For the long-term investor these refinements are trivia; for anyone placing large or urgent orders they are the difference between understanding a fill and resenting it. Either way, the remedy for a disappointing execution is rarely complaint — it is a better-chosen order: a different limit, a different size, a different moment in the session, or the patience to let the queue come to you instead of chasing it.

Key takeaway: PSX matching is price first, time second: the best price trades first, earlier orders win ties at the same price, trades print at the resting order's price — and every partial fill, queue and price improvement you have ever seen follows from those three rules.