Sub topic 3
Time in force, post-only, and stop orders on a crypto exchange
This chapter assumes you know how to place buy and sell orders, can read a pair such as BTC/USDT, understand the order book and limit orders from earlier chapters, and can read simple Python.
Time in force: GTC, IOC, and FOK
Time in force (TIF) sets how long an order stays active. Three common values:
- GTC (good till cancelled): the unfilled part rests on the book until you cancel it or it fills.
- IOC (immediate or cancel): fill what you can now at your limit price or better, then cancel the rest.
- FOK (fill or kill): fill the full quantity immediately, or cancel the whole order.
This code uses the ask side from the earlier example to show IOC and FOK behavior:
asks = [(100.0, 2.0), (100.5, 3.0), (101.0, 5.0)]
def fill_ioc(asks, limit_price, qty):
filled = 0.0
for price, size in asks:
if price > limit_price or filled == qty:
break
filled += min(qty - filled, size)
return filled # the unfilled rest is cancelled
def fill_fok(asks, limit_price, qty):
return qty if fill_ioc(asks, limit_price, qty) == qty else 0.0
print(fill_ioc(asks, 100.2, 4.0)) # 2.0
print(fill_fok(asks, 100.2, 4.0)) # 0.0
print(fill_fok(asks, 100.5, 4.0)) # 4.0Post-only orders
A post-only order is rejected if any part would match immediately. It always rests on the book as a maker, so it pays the lower maker fee instead of the taker fee. The trade-off is that it may never fill if the price does not reach it. Use it when fee savings matter more than immediate execution.
Stop-market and stop-limit orders
A stop order stays dormant until the last traded price reaches its stop price. Then it activates:
- A stop-market order becomes a market order. The fill is likely, but the price can slip well past the stop in a fast move.
- A stop-limit order becomes a limit order. The price is capped, but the order may never fill if the market jumps past the limit.
Order states from submission to completion
Check an order's state before assuming your position changed.
Execution risks and safeguards
- Thin liquidity: few units on the book mean large slippage.
- Fat-finger errors: a mistyped price or quantity. Recheck size and price before you submit.
- Volatility: prices move between placing and filling. Use limit prices and decide the maximum slippage you accept.
Choosing an order type for your goal
| Goal | Order type | Fill certainty | Price certainty |
|---|---|---|---|
| Trade now at any price | Market | High | Low |
| Trade now with a price cap | IOC limit | Partial | High |
| All or nothing at a price | FOK limit | Full or none | High |
| Wait for a price and pay lower fees | Post-only limit (GTC) | Low until price is reached | High |
| Exit if price falls | Stop-market | High once triggered | Low |
| Exit only within a price band | Stop-limit | Low | High |
Exercise
On the ask side from the code above, how many units does an IOC buy of 4 units at a limit of 100.2 fill, and what happens to the remaining units?
Show answer
It fills 2 units at 100.0. The remaining 2 units are cancelled, because the next ask level at 100.5 is above the limit.