Sub topic 3
Limit orders: naming your price
This chapter assumes you have bought and sold crypto on an exchange, know trading pairs such as BTC/USDT, and can read simple Python. It also assumes you know what the order book, best bid, best ask and spread are, from the earlier chapters. Here we look at the order type that lets you set a worst acceptable price: the limit order.
What a limit order is
A limit order is an instruction to trade a given quantity at a given price or better.
- A buy limit order means "pay at most this price".
- A sell limit order means "receive at least this price".
The price is a boundary, not a target. A buy limit at 101.0 will happily fill at 100.0 if that is what the book offers. You can get a better price than your limit, but never a worse one.
The cost of that protection is certainty. A limit order may fill completely, partly, or not at all.
Two behaviours: resting and crossing
When a limit order arrives, the exchange compares your price with the best price on the opposite side of the book.
- A buy limit whose price is at or above the best ask is marketable. It crosses the spread and trades immediately against resting sell orders.
- A buy limit whose price is below the best ask is not marketable. It rests on the bid side of the book and waits for a seller to come to it.
- A sell limit mirrors this. It is marketable if its price is at or below the best bid, and rests otherwise.
An order can do both. If it is marketable but larger than the liquidity available at prices within your limit, it fills what it can, and the remainder rests on the book at your limit price.
Worked example: a buy limit order
Use the same ask side as in the market-order chapter. The function walks the asks from best to worst and stops as soon as a price is above your limit.
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)
print(limit_buy(asks, 99.0, 4.0)) # ([], 4.0)Read the three results:
- Limit 100.2: only the 100.0 level is within your limit. Two units fill, and two units rest as a bid at 100.2. That bid becomes the new best bid if it is higher than the existing best bid.
- Limit 100.5: both the 100.0 and 100.5 levels are within the limit, so all four units fill. The average price is 100.25, below your limit.
- Limit 99.0: nothing crosses. All four units rest at 99.0.
The second result shows price improvement. You fill at the resting orders' prices, not at your own limit. Your limit only decides how deep into the book you are willing to go.
The sell side
The sell side is the same logic with the comparisons reversed. The book is sorted from highest bid downward, and you stop when a bid is below your minimum price.
bids = [(99.5, 4.0), (99.0, 6.0)]
def limit_sell(bids, limit_price, qty):
"""Fill against bids priced at or above limit_price; the rest would rest on the ask side."""
fills = []
remaining = qty
for price, size in bids:
if price < limit_price or remaining == 0:
break
take = min(remaining, size)
fills.append((price, take))
remaining -= take
return fills, remaining
print(limit_sell(bids, 99.2, 5.0)) # ([(99.5, 4.0)], 1.0)
print(limit_sell(bids, 99.0, 10.0)) # ([(99.5, 4.0), (99.0, 6.0)], 0.0)With a minimum of 99.2, four units sell at 99.5 and the bid at 99.0 is too low. The remaining unit rests as an ask at 99.2.
Where a resting order stands in the queue
Resting orders at the same price are queued. Orders are generally served by price first, then by arrival time, so an order placed earlier at 99.5 fills before one placed later at 99.5. A better price jumps the queue; a later arrival at the same price waits behind. Chapter 5 covers this matching in detail, but the consequence for you is practical: placing a limit order at the current best bid puts you at the back of that line. If the price touches your level and trades only a small amount, orders ahead of you may absorb all of it and you get nothing.
Placing your price one tick better than the best bid moves you to the front of the book. It also narrows the spread and raises what you pay if you fill.
Tick size, lot size and minimums
Exchanges restrict the values you can enter.
- Tick size is the smallest allowed price step for a pair. In the example book it is 0.5, so a price of 100.2 would be rejected on a venue that enforced it. The examples above use 100.2 only to keep the arithmetic readable.
- Lot size (or quantity step) is the smallest allowed size increment.
- Minimum notional is the smallest order value, price times quantity, that the exchange accepts.
An order that breaks any of these is rejected before it reaches the book. When you write code against an exchange, round prices with Decimal rather than floats, because a value such as 0.1 + 0.2 is not exactly 0.3 in binary floating point and can fail a tick check.
from decimal import Decimal, ROUND_DOWN
def round_to_tick(price, tick):
"""Round a price down to a multiple of the tick size."""
price, tick = Decimal(str(price)), Decimal(str(tick))
return (price / tick).to_integral_value(rounding=ROUND_DOWN) * tick
print(round_to_tick(100.2, 0.5)) # 100.0
print(round_to_tick(100.74, 0.5)) # 100.5For a buy, rounding down keeps you at or below your intended maximum. For a sell you would round up, so that you never accept less than intended.
Your funds are reserved
On a centralized exchange, a resting limit order locks the assets that would pay for it. A buy of 2 units at 100.0 on BTC/USDT-style pairs reserves the quote currency (200 plus a fee allowance). A sell reserves the base asset. Reserved funds cannot back another order or be withdrawn until the order fills or is cancelled. Several open orders can therefore use up a balance faster than you expect, and a new order may be rejected for insufficient funds even though your total balance looks large.
Why a limit order may never fill
A limit order trades certainty of price for uncertainty of execution. Three situations commonly leave it unfilled.
- The market moves away. You rest a buy at 99.0 and the price rises and never returns. You avoided overpaying, but you also missed the move.
- The market touches your price but does not trade through it. Your order is behind others in the queue at that level.
- The order is partly filled and the rest sits idle while the price moves on.
There is also a subtler effect called adverse selection. Your resting buy often fills when the price is falling through your level, because that is when sellers are willing to hit it. Filled orders are therefore more likely to be ones where the price continues lower. This is not a reason to avoid limit orders, but it explains why "my limit order filled" and "my limit order was a good trade" are different statements.
Common errors
- Treating the limit as the fill price. You may fill better than your limit. Do not assume your cost equals your limit times your quantity; use the average of the actual fills.
- Setting a limit price on the wrong side by accident. A buy limit placed well above the best ask is marketable and fills immediately, acting almost like a market order with a cap. If you meant to wait, check the order preview shows "rests" rather than "fills now".
- Forgetting open orders. A resting order stays until it fills, is cancelled, or expires under its time-in-force rule. An old order can fill days later at a price you no longer want.
- Ignoring partial fills. Check the filled quantity, not only the order status, before placing a follow-up order.
- Typing the wrong number of digits. Many exchanges warn if a limit price is far from the market, but some do not. Check the price against the best bid and ask before submitting.
Summary of trade-offs
| Property | Limit order |
|---|---|
| Worst price | Guaranteed by your limit |
| Execution | Not guaranteed; may be partial or none |
| Price improvement | Possible when crossing the book |
| Funds | Reserved while the order rests |
| Queue position | Set by price, then arrival time |
| Fee type | Usually the lower maker rate when resting; taker rate when crossing |
Exercise
The ask side of a book is [(100.0, 2.0), (100.5, 3.0), (101.0, 5.0)]. You submit a buy limit for 6.0 units at 100.5. Using the rules in this chapter, how many units fill, at what average price, and what happens to the rest?
Show answer
Only the 100.0 and 100.5 levels are at or below your limit. You fill 2.0 at 100.0 and 3.0 at 100.5, which is 5.0 units. The cost is 2.0 100.0 + 3.0 100.5 = 501.5, so the average price is 501.5 / 5.0 = 100.3. The 101.0 level is above your limit and is not touched. The remaining 1.0 unit rests on the bid side at 100.5, and you fill at a price better than your limit on part of the order.
Exercise
The bids are [(99.5, 4.0), (99.0, 6.0)] and the best ask is 100.0. You place a sell limit for 3.0 units at 100.2. Does it cross the spread? What is the effect on the book?
Show answer
It does not cross. A sell limit is marketable only if its price is at or below the best bid (99.5), and 100.2 is above it. The order rests on the ask side at 100.2. The best ask stays 100.0, because 100.0 is lower than 100.2. Your order sits behind the existing sell orders at the better price and is filled only if buyers lift the asks up to 100.2.