Sub topic 6
Market orders: certain fill, uncertain price
This chapter covers how a market order works, what price you can expect, and where it goes wrong. It assumes you know what a share is, can read a bid, an ask and a spread, and have seen an order ticket. Slippage and limit orders get their own chapters, so they appear here only where they help explain market orders. This is educational material, not investment advice.
What a market order is
A market order tells the broker to buy or sell a given quantity right now at the best prices available. You do not name a price. You give up control of price in exchange for a high chance of getting filled.
- A market buy matches against the lowest asking prices (the asks).
- A market sell matches against the highest bid prices (the bids).
- The order is "marketable" by definition, so it normally executes within moments while the market is open.
The trade-off is the title of this chapter. You get fill certainty, which means the order will almost certainly execute. You do not get price certainty, because the price depends on what is resting in the order book when your order arrives.
How a market buy is filled
The exchange matches your order against resting sell orders, starting with the cheapest. If the cheapest price level has enough shares, you fill there. If not, the rest of your order moves to the next price level, and so on. This is often called "walking the book."
Suppose the asks for stock XYZ look like this:
| Ask price | Shares available |
|---|---|
| 50.02 | 100 |
| 50.05 | 200 |
| 50.10 | 500 |
A market buy for 400 shares takes 100 at 50.02, 200 at 50.05, and the last 100 at 50.10. Your cost is 100 × 50.02 + 200 × 50.05 + 100 × 50.10 = 20,022.00 dollars... more simply, 5,002 + 10,010 + 5,010 = 20,022. The average price is 20,022 / 400 = 50.055.
A market sell works the same way in the other direction. It takes the highest bids first and works down.
Simulating a fill
The function below simulates a buy against a list of asks. With no limit it behaves like a market order. The second call passes a limit price, which you will meet in the next chapters, to show the contrast: it stops taking shares above 50.05.
def fill_buy(asks, qty, limit=None):
"""Simulate a buy against a list of (price, size) asks.
limit=None behaves like a market order; otherwise like a limit order."""
filled, cost = 0, 0.0
for price, size in sorted(asks):
if limit is not None and price > limit:
break
take = min(size, qty - filled)
filled += take
cost += take * price
if filled == qty:
break
avg_price = cost / filled if filled else None
return filled, avg_price
asks = [(50.02, 100), (50.05, 200), (50.10, 500)]
print(fill_buy(asks, 400)) # market order
print(fill_buy(asks, 400, limit=50.05)) # limit orderThe market order fills all 400 shares at an average of about 50.055. The limit order at 50.05 fills only 300 shares at an average of about 50.04 and leaves 100 unfilled. The market order bought everything but paid more for the last 100 shares. That is the whole trade-off in two lines of output.
Estimating the cost before you click
You cannot know the exact fill price in advance, but you can bound it. Start from the quote.
def spread_info(bid, ask):
spread = round(ask - bid, 4)
mid = (bid + ask) / 2
return spread, round(spread / mid * 100, 3)
bid, ask = 49.98, 50.02
spread, spread_pct = spread_info(bid, ask)
print(spread, spread_pct) # 0.04 0.08
qty = 20
print(round(qty * ask, 2)) # 1000.4 estimated cost of a market buy at the askThe spread is 0.04, about 0.08% of the midpoint. A market buyer typically pays at least the ask, so 20 shares cost roughly 1,000.40 before fees. That is an estimate, not a guarantee. If the quote moves before your order reaches the market, or the ask level holds fewer than 20 shares, the real cost will be higher.
A useful habit is to compare your quantity with the size shown at the ask. If you want 20 shares and 5,000 are offered at the ask, the fill will almost certainly be at one price. If you want 20 shares and 8 are offered, you will walk the book.
Why the last price is not what you pay
Beginners often assume a market order fills at the "last price," which is the price of the most recent trade. That price is history. A market buy pays the current ask or higher, and a market sell receives the current bid or lower. Even when the last price sits between the two, you cross the spread to get filled.
Three things can separate your fill from the quote you saw:
- Spread. You pay the ask (or receive the bid), not the midpoint.
- Depth. If your size exceeds what is offered at the best price, you walk the book.
- Movement. Quotes change while your order travels to the market. In a fast market, the price you see on screen can already be stale.
When market orders work well
Market orders suit situations where getting in or out matters more than a few cents.
- Liquid stocks with tight spreads. A widely traded large company may have a one-cent spread and thousands of shares at each level. A small order fills at essentially the quoted price.
- Small quantities. Your order is a tiny fraction of the displayed size.
- Regular trading hours. Liquidity is deepest when the main market is open, typically the middle of the session rather than the first few minutes after the open.
- Urgent exits. If you must sell and cannot risk the order sitting unfilled, fill certainty is the point.
When market orders go wrong
The failure modes follow from the same mechanism. In each case the price you receive can be far from the price you expected.
- Thinly traded stocks. Few shares are offered, and the gaps between price levels are wide. A modest order can walk through several levels and pay a noticeably worse average price.
- Wide spreads. If the bid is 9.50 and the ask is 10.00, a round trip loses about 5% of the price before the stock has moved at all.
- Market open and news events. Prices jump and quotes change quickly. A market order can fill well away from the last quote you saw.
- Extended hours. Liquidity is thin and spreads are wide. Many brokers do not accept market orders outside regular hours at all, and ask you to use limit orders.
- Large orders. Your own size moves the price. The more you buy, the higher the average you pay.
Market orders when the market is closed
If you submit a market order after the close, most brokers either reject it or hold it until the next open, where it fills at whatever the opening price turns out to be. The opening price can differ noticeably from the previous close, especially after earnings or other news. The risk is the same as before, but you cannot even see the quote you will be filled against. Read the broker's confirmation message carefully, because the behavior varies by broker.
Reading the confirmation
After a market order fills, check the execution report. It lists the quantity, the average price, and often each partial fill separately. Compare the average price with the ask you saw when you submitted. The difference, multiplied by your quantity, is the cost of that order beyond the quote. Looking at this a few times builds a realistic sense of how much a market order costs in the stocks you trade.
A quick decision guide
| Situation | Market order reasonable? | Why |
|---|---|---|
| Large, liquid stock, small size, market open | Usually yes | Spread is tight and depth is large |
| Thinly traded stock | Usually no | Depth is low, so you may walk the book |
| First minutes after the open or after news | Use caution | Quotes move quickly |
| Extended hours | Usually no | Thin liquidity, and many brokers disallow it |
| Must exit now | Yes, accepting the price | Fill certainty matters most |
Exercise
A stock shows bid 20.00 and ask 20.04. The asks are 20.04 for 50 shares, 20.06 for 100 shares, and 20.12 for 400 shares. You send a market buy for 200 shares. What is the average fill price, and how much more than the quoted ask did you pay in total?
Show answer
Fill 50 at 20.04 = 1,002.00, 100 at 20.06 = 2,006.00, and 50 at 20.12 = 1,006.00. Total cost is 4,014.00, so the average is 4,014 / 200 = 20.07. At the quoted ask of 20.04, 200 shares would cost 4,008.00, so you paid 6.00 more because the order walked the book.
Exercise
Why can a market order fill at a price different from the last traded price, even when the stock is liquid and the market is calm?
Show answer
A market buy fills at the ask and a market sell fills at the bid, not at the last trade price. The last price is a past trade, and the order must cross the spread to execute, so the fill differs from it by up to the spread even in calm conditions.