46

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.

xyz
  • 775
  • 2
  • 7
  • 10
  • 2
    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. – Mesop May 25 '12 at 07:32
  • There is a similar question "If you put a sell limit order in 5% below where a stock is currently trading, what happens?": http://answers.yahoo.com/question/index?qid=20100729193903AAO0jw7 – Mesop May 25 '12 at 07:33
  • 1
    I was JUST going to ask this question, it's so nice when SE already has the answer, thanks to Dheer and xyz :) – Void Star Dec 02 '13 at 07: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. – Victor123 Jan 21 '14 at 03:25
  • On Quantitative Finance Stack Exchange: How are limit orders selected from the order book? – Flux Aug 29 '20 at 14:15

2 Answers2

52

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.

Buy-Sell

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.

bill
  • 1,027
  • 3
  • 11
  • 18
Dheer
  • 57,070
  • 18
  • 88
  • 169
  • Thanks for the explanation - I believe the key answer was in your final paragraph. Can you point to a source where I can verify this information? It's odd that the exchange would give an advantage to the party that comes later (since you're saying the trade executes at the price of whoever is just arriving, buyer or seller, assuming the limits overlap/are mutually satisfying). – xyz May 23 '12 at 09:26
  • 8
    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). – Mesop May 23 '12 at 11:33
  • @olchauvin: Thanks. Yes Price/Time priority, escaped my mind. – Dheer May 23 '12 at 11:41
  • @xyz: I don't know of a source. The knowledge is based on my collegues working on a stock exchange system in early days – Dheer May 23 '12 at 11:41
  • @olchauvin: I understand the price/time priority. The core of my question is that when there's a match, what price does that pair of orders agree on (since there may be an overlap in their acceptable range). Dheer's answer: whoever arrives last gets the best deal. Just wanted to figure out a more authoritative source where I can learn more. – xyz May 24 '12 at 22:29
  • 1
    @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. – Mesop May 25 '12 at 07:04
  • 4
    @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 May 25 '12 at 08:11
  • @xyz, Yes whoever comes last gets the best deal. What's wrong with that? – Pacerier Nov 30 '13 at 03:47
  • @Pacerier Er, isn't that the opposite of what HFT firms are racing for? Doesn't sound right. Source? – xyz Dec 01 '13 at 00:11
  • 1
    @xyz, What point are you trying to say? What do you mean by source, this is common sense isn't it? Whoever comes first gets the lousy deal, whoever comes second gets the best deal, and whoever comes later than the second gets no deal. HFT firms are racing for the last deal. – Pacerier Dec 01 '13 at 00:26
  • 2
    @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 Dec 02 '13 at 01:50
  • 2
    @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? – Pacerier Dec 02 '13 at 02:23
  • @xyz: Edited the answer, hope it clarifies. – Dheer Dec 02 '13 at 04:49
  • Thanks again to @Pacerier Dheer and olchauvin for explaining. Factoring in quantities / reminding me that the second mover is reacting, the scenario does make more sense to me. Wish I could upvote the answer more than once! – xyz Dec 03 '13 at 02:59
  • @Dheer, thanks for that detailed answer. Quick question. When Market Orders are matched against opposite Limit Orders, is the Market Price never taken into account? If the Market Price of a stock is $100 and there is a Limit Order of Buy@$90 for 50 stocks. There is also a Market Order for Buy for 50 stocks. Now, if a Market Order for Sell comes in, will it get matched with the Market Order @ $100 or Limit @ $90? – MovieMe Sep 27 '17 at 07:38
  • @Barry Orders never come in at exactly the same time, so one of those orders gets processed before the other. I don't know what will happen if you are able to drain the order book. But if you can, you probably will not like the prices you get anyway. – Patrick Huizinga Mar 16 '18 at 15:29
  • 1
    @Barry There is no market price. I think you mean last traded price is 100. There are zero orders on book (happens only on highly illiquid stock). There is a limit buy at 90, market buy. Then the 3rd market sell order hits the exchange. The market buy matches market sell. The price algorithm decides the price. If intra day, most exchange would price it at 90. Start of day rules are different – Dheer Mar 16 '18 at 16:10
  • 1
    I'm down-voting because there's no annotation in the answer that tells me this is actually how exchanges do things. – iheanyi Mar 16 '18 at 22:18
  • @Dheer thanks for the explanation. I am late to the party. I have one question - what happens if there comes a sell order with lowest ask price in the middle of an ongoing buy order matching? does the ongoing buy order matching jump to the new sell order? – biplavo Aug 04 '20 at 16:19
  • @biplavo a large buy order matches to all sell orders at that time, the processing is fraction of seconds, it doesn't wait or see a lower order when the processing starts... – Dheer Aug 05 '20 at 14:14
4

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?