Sub topic 2
Market and Limit Orders: What You Actually Ask For
Prerequisites and the basic choice
You should already know what a share represents, what bid and ask prices mean, and how to use a brokerage app. This chapter focuses on the instructions you give your broker when you buy or sell.
An order is a request to trade a specific quantity of a security. The two basic choices are a market order and a limit order. They make different promises:
- A market order prioritizes execution. It asks the market to trade the quantity as soon as possible at the best prices currently available.
- A limit order prioritizes price. It sets the worst price you will accept, but execution is not guaranteed.
| Order type | Main protection | Main risk |
|---|---|---|
| Market order | High chance of immediate execution | The final price can differ from the quote |
| Limit order | Price will not exceed your limit when buying | The order may execute only partly or not at all |
Market orders: execution first
Suppose a stock shows a bid of $10.00 and an ask of $10.02. A market buy order for 100 shares usually consumes available sell orders beginning at the $10.02 ask. If 100 shares are available there, the order can fill at $10.02.
The displayed ask may not contain enough shares. For example, the first 50 shares might be offered at $10.02 and the next 50 at $10.04. Your market order can then fill at both prices. This is called market impact or price slippage: your order changes the prices available to the rest of the order.
A market order does not guarantee the price you saw when you tapped Buy. Quotes can change while your order travels to a trading venue. Large orders, thinly traded stocks, and volatile markets increase this risk.
Limit orders: price first
A limit price is the least favorable price you accept.
- A buy limit at $10.02 means you will pay $10.02 or less.
- A sell limit at $10.02 means you will accept $10.02 or more.
A buy limit can execute immediately if sellers are offering at $10.02 or less. If no matching shares are available, the unfilled order rests in the market's order book, which is the list of pending buy and sell orders.
A limit order can receive a better price than its limit. If you submit a buy limit at $10.02 and a seller is already offering shares at $10.01, your order can execute at $10.01. Your limit is a maximum, not a required execution price.
Limit orders also have duration instructions. A day order expires when the trading session ends if it has not filled. A good-till-canceled order remains active longer, according to the broker's rules. Check the duration because an old order can execute after market conditions change.
Price-time priority
Most central order books use price-time priority:
- The best price is considered first. For buyers, the highest bid is best. For sellers, the lowest ask is best.
- Among orders at the same price, the order that arrived first is considered first.
This rule explains why a limit order may fill in several pieces and at different prices. The following simplified book matches a 120-share buy limit at $10.02 against the lowest available asks.
import heapq
from itertools import count
seq = count()
bids = [] # (-price, arrival_seq, quantity)
asks = [] # (price, arrival_seq, quantity)
def submit(side, price, quantity):
"""Match a limit order using price-time priority."""
fills = []
order_seq = next(seq)
if side == "buy":
while quantity and asks and asks[0][0] <= price:
ask_price, ask_seq, ask_quantity = heapq.heappop(asks)
traded = min(quantity, ask_quantity)
fills.append((ask_price, traded))
quantity -= traded
if ask_quantity > traded:
heapq.heappush(
asks, (ask_price, ask_seq, ask_quantity - traded)
)
if quantity:
heapq.heappush(bids, (-price, order_seq, quantity))
elif side == "sell":
while quantity and bids and -bids[0][0] >= price:
neg_bid_price, bid_seq, bid_quantity = heapq.heappop(bids)
traded = min(quantity, bid_quantity)
fills.append((-neg_bid_price, traded))
quantity -= traded
if bid_quantity > traded:
heapq.heappush(
bids, (neg_bid_price, bid_seq, bid_quantity - traded)
)
if quantity:
heapq.heappush(asks, (price, order_seq, quantity))
else:
raise ValueError("side must be 'buy' or 'sell'")
return fills
submit("sell", 10.02, 100)
submit("sell", 10.01, 50)
print(submit("buy", 10.02, 120))
# [(10.01, 50), (10.02, 70)]The buy order first takes the 50 shares offered at $10.01. It then takes 70 of the 100 shares offered at $10.02. The buyer's maximum was $10.02, but the first fill received a better price.
Exercise
The ask is $10.01 for 40 shares, followed by $10.03 for 100 shares. What happens to a buy limit order for 100 shares at $10.02?
Show answer
It fills 40 shares at $10.01. The remaining 60 shares cannot execute because $10.03 exceeds the $10.02 limit, so they rest in the order book or expire according to the order's duration.