# How do exchanges match limit orders?

Say a security has been trading at under \$20.10, and there are limit orders selling at \$20.10 and \$20.21. Then Simon issues a limit order to buy at \$20.21. According to this explanation, Simon would end up buying shares at \$20.10.

Why does the exchange favor the buyer here? I.e., why does the transaction execute at the seller's limit of \$20.10 instead of the buyer's limit of \$20.21? (I.e., why don't Inara/River get to sell at \$20.21 / above their limit, but Simon gets to buy at \$20.10 / below his limit?) Is the example flawed?

If it makes a difference, I'm mainly curious about NASDAQ and NYSE. I tried Googling for specs (e.g. this), but couldn't find anything. Only found that some random limit order book implementation executes the average of the buy/ask. Just curious how the system works.

• You can't fill the limit sell order at 20.21 before the limit sell order at 20.10 is executed. It's against the price priority. That's why the limit sell orders at 20.10 are executed first. In this case, because the limit buy order is above the lowest limit sell order, the limit buy order get executed until its fully filled, or that all the limit sell order below 20.21 are filled. Commented May 25, 2012 at 7:32
• There is a similar question "If you put a sell limit order in 5% below where a stock is currently trading, what happens?": answers.yahoo.com/question/index?qid=20100729193903AAO0jw7 Commented May 25, 2012 at 7:33
• I was JUST going to ask this question, it's so nice when SE already has the answer, thanks to Dheer and xyz :) Commented Dec 2, 2013 at 7:13
• In that article (thanks for the link!), he mentions that the best price will be filled first. Best price is from buyer's point of view. This does not answer your why, this is more like a justification I guess. Commented Jan 21, 2014 at 3:25
• On Quantitative Finance Stack Exchange: How are limit orders selected from the order book?
– Flux
Commented Aug 29, 2020 at 14:15

The Limit Order are matched based on amount and time. The orders are listed Highest to Lowest on the Buy Side. The orders are listed Lowest to Highest on the Sell Side. If there are 2 Sell orders for same amount the order which is first in time [fractions of milliseconds] is first.

The above is an example of how the orders would look on any exchange. Now the highest price the buyer is ready to pay is 20.21 and the lowest price a seller is ready to sell for is 20.25. Hence there is no trade.
Now if a new Buy order comes in at 20.25, it matches with the sell and the deal is made.
If a new Buy order comes in at 20.30, it still matches at 20.25.
Similarly if a Sell order come in at 20.21, it matches and a deal is made.
If a Sell order come in at 20.11, it still matches 20.21.

In case of market order, with the above example if there is a Buy order, it would match with the lowest sell order at 20.25, if there is not enough quantity , it would match the remaining quantity to the next highest at 20.31 and continue down. Similarly if there is a Sell market order, the it would match to the maximum a seller is ready to buy, ie 20.21, if there is not sufficient buy quantity at 20.21, it will match with next for 20.19

If say there are new buy order at 20.22 and sell orders at 20.24, these will sit first the the above queue to be matched.

In your above example the Lowest Sell order was at 20.10 at time t1 and hence any buy order after time t1 for amount 20.10 or greater would match to this and the price would be 20.10.
However if the Buy order was first ie at t1 there was a buy order for 20.21 and then at time later than t1, there is a sell order for say 20.10 [amount less than or equal to 20.21] it would match for 20.21.
Essentially the market looks at who was the first to sell at lower price or who was the first to buy at higher price and then decide the trade.

Edit [To Clarify xyz]:
Say if there is an Sell order at \$10 Qty 100. There is a buyer who is willing to pay Max \$20 and is looking for Qty 500. Your key assumption that the Buyer does not know the current SELL price of \$10 is incorrect.
Now there are multiple things, the Buyer knows the lowest Sell order is at \$10, he can put a matching Buy order at \$10 Qty 100, and say \$11 Qty 100 etc. This is painful.
Second, lets say he puts a Buy order at \$10 Qty 100, by the time the order hits the system someone else has put the trade at \$10 and his order is fulfilled. So this buyer has to keep looking at booking and keep making adjustments, if its a large order, it would be extremely difficult and frustrating for this Buyer. Hence the logic of giving preference.
The later Buy order says ... The Max I can pay is \$20, match eveything at the current price and get the required shares.

• The priority of order execution is called `Price/Time priority`. The order are executed at the best price, with the oldest limit order (at that best price) in the book (some exchanges introduced a membership that put the order of the member before the order of the non member to have a `Price/Member/Time priority`). Commented May 23, 2012 at 11:33
• @xyz In an order driven market, as soon as a market order is received by the exchange, it's matched with limit orders. So, basically the agreed price, is the price of the limit order that is filled. Also, big market orders will fill several limit orders, you will have several agreed prices (and for reporting purpose, the weighted average price of the market order can be used). Finally, Dheer said that the last Market order (ie, the one currently being executed) will get the best current price. While the oldest limit order in the book at that price will be filled first. Commented May 25, 2012 at 7:04
• @olchauvin Yes, but again, what is "the agreed price"? (Ignoring matches against multiple orders for now.) If you're selling for as low as \$10 and I'm buying for as high as \$20, then what do we transact at? \$10? \$20? \$15? Dheer's answer is that whoever came last gets the best deal. So if you were waiting and then I come in, then we transact at \$10, so you get the least amount of money. Hope my question is more clear.
– xyz
Commented May 25, 2012 at 8:11
• @Pacerier Sorry if my skepticism offended you—I'm perfectly willing to accept that this is just how the system is designed, it's just not common sense to me. If I am willing to sell at \$10 and you're willing to buy at \$20, then does it make sense to you that by posting your intentions first, you get \$10 instead of \$20? It suggests no rational players would post limit orders (that don't immediately execute), ever. If you're willing to elaborate or—even better—recommend a beginners' resource, I, and perhaps others, would tremendously appreciate it.
– xyz
Commented Dec 2, 2013 at 1:50
• @xyz ok So we have 2 people wanting to trade. One of them can afford to wait more than the other guy. It makes sense that the person who can afford to wait gets the better deal right? The person who urgently needs the order to get filled does not have much say in the price. Fair? Commented Dec 2, 2013 at 2:23

The total limit book is a composite of all the orders on all of the exchanges.

While it's uncommon for a limit order posted beyond the NBBO to fill outside of the NBBO, it does occur.

For example, the best ask may be on exchange X, but for some reason the smart order routing algorithm may select exchange Y if it judges the net trade to be less costly, malfunctions, etc, and HFTs will immediately arbitrage the order between two exchanges, or the best order on exchange X disappears causing the order to fill above the NBBO.

The system isn't perfect because there are multiple exchanges, but that eventuality is extremely rare with equities since nearly every exchange will have orders posted at the NBBO because exchange equity fee and rebate schedules are extremely competitive, nearly identical. It is however more common with options since less exchanges as a percentage of the total will have orders posted at the NBBO because of very wide exchange rebate and fee schedules.

How a single exchange handles a new order that crosses an existing limit order is already addressed here: How do exchanges match limit orders?

• Any sources to quote? Commented Nov 30, 2013 at 3:48