Sub topic 7
Partial fills and average fill price
This chapter assumes you can read an order book (bids, asks, depth), know the difference between a market order and a limit order, and can read short Python functions. It covers what happens when one order is filled by several counterparties at several prices, and how to compute and read the resulting average fill price.
What a partial fill is
A partial fill happens when only part of an order's quantity executes. The rest either keeps waiting on the book, is cancelled, or was never allowed to start. An order that has traded in full is a complete fill.
There are two ways an order ends up partially filled:
- Not enough liquidity at acceptable prices. A limit buy at 100.2 can only trade against asks priced at 100.2 or lower. If those levels hold less than your quantity, the rest cannot trade.
- Your order is bigger than the resting order it meets. A resting sell of 2 units that meets a buy of 5 fills 2 units. The buy has 3 units left.
A single order can also produce many fills. Each fill is one trade between your order and one resting order, at that resting order's price. Exchanges report these fills individually, so one order often shows up as several trade records.
Fills versus levels
Do not confuse a fill with a price level. One level can contain many resting orders, and your order can trade with several of them. One level can also be only partly consumed. The exchange records a fill per matched resting order, not per level, so the number of fills can be larger than the number of levels you crossed.
Computing the average fill price
The average fill price is the total cost of all fills divided by the total quantity filled. This is a quantity-weighted average, not a simple average of prices.
average fill price = sum(pricei × qtyi) / sum(qty_i)
Take the ask side below.
| Price | Size |
|---|---|
| 100.0 | 2.0 |
| 100.5 | 3.0 |
| 101.0 | 5.0 |
A market buy of 4 units takes 2 at 100.0 and 2 at 100.5. The cost is 2 × 100.0 + 2 × 100.5 = 401.0. Dividing by 4 gives 100.25. A simple average of the two prices would also give 100.25 here, but only because both fills had the same size. If the second fill were 1 unit, the simple average would still say 100.25 while the true average would be (200.0 + 100.5) / 3 = 100.1667.
# Ask side of a book: (price, size), sorted from best (lowest) price upward
asks = [(100.0, 2.0), (100.5, 3.0), (101.0, 5.0)]
def market_buy(asks, qty):
"""Walk the ask side and return (filled_qty, average_price)."""
remaining = qty
cost = 0.0
for price, size in asks:
take = min(remaining, size)
cost += take * price
remaining -= take
if remaining == 0:
break
filled = qty - remaining
avg = cost / filled if filled else None
return filled, avg
filled, avg = market_buy(asks, 4.0)
best_ask = asks[0][0]
print(filled, avg) # 4.0 100.25
print((avg - best_ask) / best_ask * 100) # 0.25 (percent slippage vs best ask)The function returns filled separately from the requested quantity. That matters: if the book is too thin, filled is smaller than qty, and the average price describes only the part that traded. The None return for zero fills avoids a division by zero.
Walking the book as a sequence of fills
The diagram shows the market buy of 4 units against the book above. Each step consumes some quantity and reduces what remains.
The level at 100.5 had 3 units but only 2 were taken. It now shows 1 unit. This leftover is what the next incoming order sees.
Partial fills with limit orders
A limit order adds a price bound, so partial fills are more common. The order trades against everything at or better than its limit, and whatever is left depends on its time in force (covered in a later chapter).
asks = [(100.0, 2.0), (100.5, 3.0), (101.0, 5.0)]
def limit_buy(asks, limit_price, qty):
"""Fill against asks priced at or below limit_price; the rest would rest on the bid side."""
fills = []
remaining = qty
for price, size in asks:
if price > limit_price or remaining == 0:
break
take = min(remaining, size)
fills.append((price, take))
remaining -= take
return fills, remaining # remaining > 0 means that quantity rests on the book
print(limit_buy(asks, 100.2, 4.0)) # ([(100.0, 2.0)], 2.0)
print(limit_buy(asks, 100.5, 4.0)) # ([(100.0, 2.0), (100.5, 2.0)], 0.0)With a limit of 100.2, only the 100.0 level qualifies. The order fills 2 units and 2 units remain. With a standard good-till-cancelled order, those 2 units rest on the bid side at 100.2. With an immediate-or-cancel order, they are cancelled.
A resting limit order can then be filled in pieces over time. Suppose your bid of 10 at 99.0 sits in the book. A seller of 3 hits it, then a seller of 4, then a seller of 3. You get three fills of 3, 4 and 3 units, all at exactly 99.0. Because a resting order always trades at its own price, its average fill price equals its limit price. The average only differs from the limit when the order crossed the spread and took liquidity at better prices.
A limit order that crosses the spread can mix both behaviors. A buy of 6 at 100.5 against the book above takes 2 at 100.0 and 3 at 100.5, then has 1 unit left. That unit now rests as a bid at 100.5, because the book has no asks at or below that price. The final result is a partial fill with an average of 100.3 on 5 units, plus a resting remainder.
Edge cases to know
- Price improvement. A limit buy at 101.0 against asks at 100.0 and 100.5 fills at 100.0 and 100.5, not at 101.0. The limit is a ceiling, not the price you pay.
- Dust remainders. After several fills, the leftover can fall below the exchange's minimum order size or lot size. Some exchanges cancel the remainder, others keep it open but it cannot be edited into something useful. Check your exchange's rules.
- Rounding. Fill quantities are rounded to the pair's quantity precision, and costs to the quote currency's precision. Your computed average can differ from the reported one in the last decimal places.
- Fees across fills. Fees are charged per fill, and the maker or taker rate can differ between fills of the same order. A limit order that partly crosses the spread pays the taker rate on the part that took liquidity and the maker rate on the part that later rested and filled. The effective cost is a weighted blend. Some exchanges also round each fill's fee separately, which adds small differences.
- Self-trade prevention. If a resting order of yours would be matched by your own incoming order, the exchange may cancel one side, cancel both, or reduce quantity. The result looks like a partial fill with no trade at all for the blocked portion.
- Stale snapshots. The book you see is a snapshot. Between your click and the matching engine, other orders can consume the levels you counted on. Your estimate is a forecast, and the real fills can be worse.
Reading fill reports
Exchange interfaces use slightly different names, but the same fields appear almost everywhere.
| Field | Meaning |
|---|---|
| Order quantity | What you asked for |
| Executed (filled) quantity | Sum of all fills so far |
| Remaining (leaves) quantity | Order quantity minus executed quantity |
| Average price | Total cost of fills divided by executed quantity |
| Status | For example new, partially filled, filled, cancelled, partially filled then cancelled |
Two checks catch most mistakes. First, executed plus remaining plus any cancelled quantity should equal the order quantity. Second, the average price must lie between the lowest and highest fill prices. If you rebuild the average from the individual trade records and it disagrees with the order summary, compare the fee treatment: some interfaces show an average that includes fees, others do not.
A status of "cancelled" does not mean nothing traded. An order that was partially filled and then cancelled keeps its fills. Those trades are final, and you hold the position they created. Always look at executed quantity, not only at status, before assuming you are flat.
Why partial fills matter for risk
A partial fill can leave you with a position you did not plan for. If you wanted to buy 4 units and only 2 filled, you may be under-hedged or under-exposed. A trading rule that assumes all-or-nothing execution will misbehave. In practice:
- Track filled quantity, not just whether an order was accepted.
- Size follow-up orders (for example a protective sell) from the filled quantity, not the requested quantity.
- Decide in advance whether to chase the remainder with a new order, leave it resting, or cancel it.
The worked cost of chasing is easy to estimate with the same walk-the-book logic. If your first order filled 2 at 100.0 and you then send a market order for the remaining 2 against the same book, the second order takes 2 at 100.5. The combined average is 100.25, the same as one market order would have given, but only if no one else touched the book in between.
Putting the pieces together in code
The next function combines both ideas. It takes a list of fills, which could be copied from an exchange's trade history, and returns the executed quantity, the average price and the total fee. Fee rates are passed per fill because maker and taker status can differ.
def summarize_fills(fills):
"""fills: list of (price, qty, fee_rate). Returns (qty, avg_price, total_fee)."""
qty = sum(q for _, q, _ in fills)
cost = sum(p * q for p, q, _ in fills)
total_fee = sum(p * q * r for p, q, r in fills)
avg = cost / qty if qty else None
return qty, avg, total_fee
# 2 units taken at 100.0 (taker), then 1 unit rested and filled at 100.0 (maker)
fills = [(100.0, 2.0, 0.0020), (100.0, 1.0, 0.0010)]
qty, avg, total_fee = summarize_fills(fills)
print(qty, avg, round(total_fee, 4)) # 3.0 100.0 0.5The taker fill pays 0.4 and the maker fill pays 0.1, so the total is 0.5. Divided by the traded value of 300.0, the blended rate is about 0.167%, between the two rates. The rates are illustrative, as in the earlier fee chapter.
Exercise
The ask side is [(50.0, 1.0), (50.4, 2.0), (51.0, 4.0)]. You submit a limit buy of 5 units at 50.4. How many units fill, at what average price, and what happens to the rest?
Show answer
Only asks at or below 50.4 qualify: 1 unit at 50.0 and 2 units at 50.4, so 3 units fill. The cost is 50.0 + 100.8 = 150.8, so the average is 150.8 / 3 = 50.2667. The remaining 2 units cannot trade at 51.0, which is above the limit. With a good-till-cancelled order they rest as a bid at 50.4. With immediate-or-cancel they are cancelled.
Exercise
An order for 10 units shows status "cancelled", executed quantity 4, and average price 200.0. Are you holding anything, and how much would you sell to close the position?
Show answer
Yes. Cancelling stopped the remaining 6 units, but the 4 executed units are final trades. You hold 4 units, so a closing order should be for 4 units, not 10.