Chapter 3 of 10
How orders match
Market, limit and stop orders, price-time priority, partial fills, and what a matching engine actually does.
At the centre of every exchange is a program called the matching engine. It holds the order book for each stock, receives a stream of new orders and cancellations, and decides, one message at a time, which buyer trades with which seller and at what price. It does not negotiate, guess or have opinions. It applies a short list of rules in a fixed order, very fast, and publishes what happened.
Those rules are worth knowing exactly, because they decide what you pay, whether your order fills, and who gets the trade when several people want it. This chapter goes through them, then lets you run an engine one step at a time.
Rule one: best price first
An incoming buy order is matched against the lowest ask first. An incoming sell order is matched against the highest bid first. Nobody can skip ahead by offering a worse price. If a seller at $10.02 and a seller at $10.03 are both waiting, a buyer will always reach the $10.02 shares first.
Rule two: at the same price, first come, first served
When several orders sit at the same price, the one that arrived earliest trades first. This is time priority, and together with price priority it gives the standard rule its name: price-time priority. Your place in the queue is fixed when your order arrives. If you change the price, or increase the size, most exchanges treat that as a brand-new order and send you to the back of the line. Reducing the size usually keeps your place.
This is why speed matters so much to professional traders. At a popular price level there may be thousands of shares queued ahead of you. Being early in the queue means you trade before the price moves away; being late means you may never trade at all. Some markets use other rules: many futures and options markets use pro-rata matching, where an incoming order is shared among all resting orders at the best price in proportion to their size. US stock exchanges use price-time priority, and so does the simulator on this site.
Rule three: the resting order sets the price
A trade always happens at the price of the order that was already in the book. If you send a limit buy at $10.03 and someone is offering at $10.02, you pay $10.02, not $10.03. Your limit is the worst price you will accept, not the price you will get. The incoming order is the one that "crosses the spread" and gets whatever is waiting there.
Step through a matching engine
Pick a scenario, send the order, then go one decision at a time. Each chip is one order, in the order it arrived.
The book is the same small $10 stock in every scenario, so you can compare them. Gold chips are yours.
Order types
Everything a broker offers is built from two primitives, market and limit orders, plus some conditions on when they become active and how long they live.
Market order
Trade immediately at the best available prices. You will almost certainly get filled in a liquid stock, but you do not control the price. In a thin book or a fast market, a market order can fill far from the last price you saw. Many brokers quietly protect you by converting market orders into limit orders a few percent away from the current price, so they cannot fill at absurd levels.
Limit order
Trade at this price or better. Whatever can fill right now does; the rest waits in the book until it is filled, cancelled, or expires. A limit buy placed above the best ask behaves almost like a market order with a safety cap, which is often the most sensible way to buy.
Stop order (stop-loss)
A stop order sits off the book, invisible to other traders, until a trade prints at or through its stop price. Then it turns into a market order. Investors use sell stops to cap losses: "if this falls to $9.97, get me out." Two things surprise people. First, the stop price is a trigger, not a promise: once it becomes a market order it fills wherever the book is, which in a fast fall may be well below the stop. Second, when many stops sit at similar prices, one triggered stop sells into the book, pushes the price lower, and triggers the next. The "Stop cascade" scenario shows exactly that.
Strictly speaking, stops are usually held by your broker, not the exchange. The broker watches the price and sends the order when it triggers. The effect is the same.
Stop-limit order
Like a stop, but it becomes a limit order instead of a market order. It will not sell below its limit, which protects you from a terrible fill, at the cost that in a fast fall it may not sell at all, leaving you holding the shares as the price keeps dropping.
How long an order lives
Every order also carries a time in force:
- Day: if it has not filled by the close, it is cancelled. This is the default at most brokers.
- Good-till-cancelled (GTC): it stays open across days until it fills or you cancel it. Brokers usually cap this, often at a few months.
- Immediate-or-cancel (IOC): take whatever can fill right now, cancel the rest. Nothing ever rests in the book.
- Fill-or-kill (FOK): fill the whole order right now or do nothing at all. No partial fills.
A partial fill, getting some of your shares now and the rest later or never, is normal for limit orders. Your order keeps its place in the queue for the shares still waiting.
Exchanges also offer more specialised types, such as hidden orders that do not show in the book, orders pegged to the midpoint of the best bid and ask, and orders that are only allowed to add liquidity, never take it. They are all variations on the same rules.
Makers, takers and fees
Every trade pairs a resting order (the maker, who added liquidity to the book) with an incoming order (the taker, who removed it). Most US stock exchanges charge the taker a small fee and pay the maker a slightly smaller rebate, keeping the difference. The fees are tiny, capped by regulation at a fraction of a cent per share (30 cents per 100 shares for most of the last two decades, with a lower cap adopted in 2024). Some exchanges invert the model and pay takers instead. For a long-term investor these amounts are invisible. For firms that trade billions of shares a year, which exchange to send each order to, and whether to make or take, is a business in itself.
Auctions: when everyone trades at once
Continuous matching runs through the trading day, from 9:30am to 4:00pm Eastern. The day starts and ends differently. Before the open, orders accumulate without trading. At 9:30 the listing exchange runs an opening auction: it finds the single price at which the most shares can trade, and all matched orders trade at that one price. The closing auction at 4:00 works the same way and is often the busiest moment of the day, because index funds and other large investors want to trade at the official closing price, which is the price their performance is measured against. A meaningful share of each day's volume in large stocks now happens in that one closing match.
Auctions are also used to restart trading after a halt. Exchanges pause a stock when news is pending, and "limit up-limit down" rules pause it briefly if its price moves outside a band around its recent average too quickly. When trading resumes, an auction finds a fair restart price instead of letting the first market order set it.
What the engine does not do
It does not check whether you can afford the trade, decide which exchange your order goes to, or move the money and shares. Your broker checks you before the order is sent, and other institutions settle the trade afterwards. A matching engine only answers one question: given these orders, who trades with whom, how many shares, and at what price. The next chapter is about the traders whose orders are almost always in that book: market makers.