Sub topic 9
Time in force: GTC, IOC, and FOK
This chapter assumes you can read an order book, know the difference between a limit order that rests and one that crosses the spread, and understand partial fills and maker/taker fees from the earlier chapters. You also need to read short Python functions.
What time in force controls
A limit order has a price and a size. It also needs a rule for what happens to any quantity that cannot trade right away. That rule is the time in force (TIF). It is set per order and decides how long the unfilled part may live and whether partial fills are acceptable.
Three values cover most cases:
- GTC (Good Till Canceled): match what you can now, then leave the rest on the book until it fills or you cancel it.
- IOC (Immediate Or Cancel): match what you can now, then cancel the rest. Partial fills are fine.
- FOK (Fill Or Kill): fill the entire quantity now, or do nothing at all. Partial fills are not allowed.
TIF only matters for the part of an order that does not fill on arrival. If a limit order crosses the spread and the book has enough liquidity at your price or better, all three values give the same result.
The decision at the matching engine
When an order arrives, the matching engine first tries to match it against the opposite side of the book, using price-time priority. Only after that does it apply the TIF to whatever is left.
FOK needs one extra detail. The engine must not execute trades and then discover the order cannot complete. It checks whether enough liquidity exists at or better than your limit price, and only then executes. The check and the execution happen as a single step, so you never end up with a partial FOK fill.
A runnable model
This code reuses the matching logic from the limit order chapter and adds the TIF rule on top. It returns the fills, the quantity left resting on the book, and the quantity canceled.
asks = [(100.0, 2.0), (100.5, 3.0), (101.0, 5.0)]
def match_buy(asks, limit_price, qty):
"""Fill against asks at or below limit_price. Returns (fills, remaining)."""
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
def submit_buy(asks, limit_price, qty, tif):
"""Return (fills, resting_qty, canceled_qty) for a limit buy with the given TIF."""
fills, remaining = match_buy(asks, limit_price, qty)
if tif == "FOK":
if remaining > 0:
return [], 0.0, qty # kill: no fills at all
return fills, 0.0, 0.0
if tif == "IOC":
return fills, 0.0, remaining # cancel whatever did not fill
if tif == "GTC":
return fills, remaining, 0.0 # remainder rests on the bid side
raise ValueError(f"unknown time in force: {tif}")
for tif in ("GTC", "IOC", "FOK"):
print(tif, submit_buy(asks, 100.2, 4.0, tif))
# GTC ([(100.0, 2.0)], 2.0, 0.0)
# IOC ([(100.0, 2.0)], 0.0, 2.0)
# FOK ([], 0.0, 4.0)
print("FOK", submit_buy(asks, 100.5, 4.0, "FOK"))
# FOK ([(100.0, 2.0), (100.5, 2.0)], 0.0, 0.0)The buy is for 4 units with a limit of 100.2. Only the 2 units at 100.0 are at an acceptable price. Each TIF handles the other 2 units differently:
- GTC keeps the 2 units that filled and rests the other 2 as a bid at 100.2.
- IOC keeps the 2 units that filled and cancels the other 2.
- FOK cancels all 4 units because it cannot get the full size.
Raise the limit to 100.5 and FOK succeeds, because 2 units at 100.0 plus 2 units at 100.5 cover the order.
GTC in practice
GTC is the default on most exchange interfaces when you place a limit order, and it suits orders you expect to wait for. A few behaviors catch people out.
- It locks funds. A resting buy reserves quote currency, such as USDT. A resting sell reserves the base asset. Those funds are unavailable for other orders until the order fills or is canceled.
- "Good till canceled" is not "forever." Exchanges can still cancel resting orders for reasons outside your control. Examples are a market halt, a delisting, or a maintenance restart. Some venues also expire very old orders. Check the venue's documentation, and do not assume an order you placed months ago is still live.
- A GTC order can be both taker and maker. If it crosses the spread, the crossing part fills immediately and pays the taker fee. The remainder rests and pays the maker fee when it later fills. Your total fee for one order is therefore a blend of the two rates.
- Stale orders are a real risk. A buy at 95 placed during a calm week can fill during a sharp drop weeks later, when you no longer want it. Review open orders regularly.
- Priority belongs to the order you placed. Under price-time priority, your place in the queue at a price level depends on when the order arrived. Most exchanges let you keep priority when you reduce size but send you to the back of the queue when you raise it or change the price. Check how your venue treats an amendment.
IOC in practice
IOC is for traders who want fills now, at a price they control, with no leftover order on the book. The most common use is a price-capped market order. A true market order walks the book with no price limit, so slippage is bounded only by book depth. An IOC limit order bounds it by your limit price instead.
# Reuses submit_buy and asks from the previous example
fills, resting, canceled = submit_buy(asks, 100.5, 10.0, "IOC")
filled_qty = sum(size for _, size in fills)
avg_price = sum(price * size for price, size in fills) / filled_qty
print(fills, resting, canceled) # [(100.0, 2.0), (100.5, 3.0)] 0.0 5.0
print(filled_qty, avg_price) # 5.0 100.3The order asks for 10 units but will not pay more than 100.5. It fills 5 units at an average of 100.3 and cancels the other 5. The deeper levels at 101.0 are never touched.
Keep two points in mind when you use IOC:
- You can end up with less than you wanted. The result is a partial position, and your code or your plan must handle that. If you were building a hedge of exactly 10 units, 5 units is a different outcome from FOK's all-or-nothing.
- IOC always takes liquidity. Any quantity it fills removes resting orders from the book, so it pays the taker fee. IOC never rests, so it cannot earn the maker rate.
FOK in practice
FOK is for situations where a partial fill is worse than no fill. Examples are one leg of a hedge that must match the size of the other leg, or a trade that is only worthwhile at full size. A partial fill would leave you with an exposure you did not plan for.
Know its limits:
- It protects size, not price. FOK says nothing about price on its own. The limit price still decides how far into the book the order may reach. A FOK buy with a very high limit can fill at poor prices, as long as the whole quantity fills.
- It fails often in thin books. If only a small quantity sits within your acceptable price, a large FOK will be rejected again and again. A failed FOK can look like an error in a trading bot. It is a normal outcome and your code should expect it.
- The check uses the book at arrival time. Your screen may show enough depth, but the order is evaluated when the engine receives it. Another trader may have taken that liquidity in the meantime, so a FOK that looked certain on screen can still be killed.
- It always takes liquidity. Like IOC, a successful FOK pays the taker fee.
- Venues report the kill differently. The final state may appear as canceled, expired, or rejected, depending on the exchange. Read the status values in the API documentation before you build logic on them.
Comparison
| Property | GTC | IOC | FOK |
|---|---|---|---|
| Rests on the book | Yes, the remainder | Never | Never |
| Accepts a partial fill | Yes | Yes | No |
| Fee on fills | Taker for the crossing part, maker for the resting part | Taker | Taker |
| Typical use | Waiting for a better price | Price-capped immediate execution | All-or-nothing execution |
| Main risk | Stale order fills later | Less size than intended | Repeated kills in thin books |
Choosing among the three
Ask two questions in order:
- Do you want the order to wait on the book if it cannot fill now? If yes, use GTC.
- If not, would you accept a partial fill? If yes, use IOC. If no, use FOK.
Then set the limit price to express how much slippage you will tolerate. TIF decides what happens to the leftover quantity. The limit price decides how far you are willing to trade at all.
Exercise
The ask side of a book is [(50.0, 1.0), (50.2, 2.0), (50.4, 4.0)]. You submit a limit buy for 5 units at a limit of 50.2. For each of GTC, IOC, and FOK, state how many units fill, how many rest on the book, and how many are canceled.
Show answer
Only the levels at 50.0 and 50.2 are within the limit, giving 1.0 + 2.0 = 3.0 units available. GTC fills 3.0, rests 2.0 as a bid at 50.2, and cancels 0. IOC fills 3.0, rests 0, and cancels 2.0. FOK fills 0 because the full 5.0 cannot be filled, rests 0, and cancels all 5.0.
Exercise
You need to buy exactly 6 units to match a short position elsewhere, and you will not pay more than 50.4. Which time-in-force value fits, and what could still go wrong?
Show answer
FOK fits, because a partial fill would leave your hedge mismatched. With the book above, 1.0 + 2.0 + 4.0 = 7.0 units are available at 50.4 or better, so the order should fill in full. It can still fail if another trader takes some of that liquidity before your order reaches the engine. The engine then kills the order, and your code must handle that outcome, for example by retrying with a new price or alerting you.