Sub topic 2
Market orders: certain fill, uncertain price
This chapter assumes you can read a bid and an ask, know that a share is a unit of ownership in a company, and can run a short Python script. A paper-trading account works for every step.
Market orders: certain fill, uncertain price
A market order tells your broker to buy or sell immediately at the best prices available. In a liquid stock, it almost always fills. The price is not fixed, though. A buy takes shares from the lowest asks first, then moves up to higher asks until the full quantity is filled.
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: about (400, 50.055)> Note: the callout below is a note, not a quote, and it applies to thinly traded stocks as well.
Slippage: why you may not get the quoted price
Slippage is the difference between the price you expected, usually the best ask, and the average price you actually pay. It grows when your order is larger than the shares offered at the best price, or when the price moves between your quote and your fill.
| Order size | Average fill | Slippage vs. best ask of 50.02 |
|---|---|---|
| 100 shares | 50.02 | 0.00 |
| 300 shares | 50.04 | 0.02 |
| 400 shares | 50.055 | 0.035 |
Limit orders: certain price, uncertain fill
A limit order sets the worst price you will accept. A buy limit fills only at or below the limit, and a sell limit fills only at or above it. You control the price, but the order may fill partly or not at all. With a limit of 50.05, the same script fills 300 of 400 shares at an average of 50.04, and the remaining 100 shares stay open.
Time in force: how long an order stays active
Time in force sets how long an order remains working. A DAY order expires at the end of the trading session if unfilled. A good-til-canceled (GTC) order stays open across sessions until it fills or you cancel it. Brokers cap how long a GTC order lasts, so check your broker's limit.
From submitted to filled: order statuses and partial fills
An order moves through statuses as it is handled. Open means it is working and unfilled. Partially filled means some shares have traded and the rest remain open. Filled means every share has traded. Canceled means you or the system stopped the order, and any shares already filled remain yours. Rejected means the broker did not accept the order, usually because of a data or funds problem.
Exercise
Exercise
Using the asks above, what does a market buy of 250 shares fill at, and what is the average price?
Show answer
It fills 100 shares at 50.02 and 150 shares at 50.05, for a total cost of 12509.50. The average price is 50.038, so the order fills completely at about 50.04 per share.