Objective
Compute order-flow imbalance (OFI) from a stream of best-quote updates, explain why it explains contemporaneous price changes much better than trade imbalance does, measure the relation on a simulated book, and be precise about the difference between explaining a price change and predicting the next one. The order-flow lesson introduced trade-signed imbalance; this lesson uses the full book.
Intuition
The mid-price moves when a queue at the best bid or ask is exhausted. Anything that adds to the bid queue (a new limit buy) or removes from the ask queue (a market buy or a cancelled sell) pushes towards an up-move; the opposite pushes down. Trades are only one of these channels: most book changes are limit orders and cancellations. OFI counts all of them, in shares, at the best quotes.
Definition
Let the -th book update have best bid price and size and best ask . Cont, Kukanov and Stoikov define the contribution of update as
and OFI over an interval is the sum of over its updates. Case by case, for the bid side:
| Bid change | Contribution | Meaning |
|---|---|---|
| Same price | Net size added (joins minus cancels and sells hitting it) | |
| Price up | A whole new bid level appeared: buying pressure | |
| Price down | The old bid level was wiped out: selling pressure |
The ask side is the mirror image with the opposite sign: ask size growing is selling pressure, an ask level cleared ( up) is buying pressure.
The linear relation
Over short intervals (seconds), price changes are close to linear in OFI:
The slope is inversely proportional to market depth: the thicker the book, the more shares it takes to move the price a tick. In CKS's stylised model with depth at each level, for tick size : clearing a level takes about shares of flow, and the new level that appears behind the price move adds about another . On their sample of US stocks they report contemporaneous of around 65% on average for 10-second intervals, far above what trade imbalance alone achieves, and the slope varies through the day with depth.
The relation is contemporaneous: it explains the price change during the interval using flow from the same interval. Whether OFI predicts the next interval's move is a separate question, and the answer is much weaker.
Worked example
OFI from six book updates
Best quotes (bid price × size | ask price × size), in lots:
| Update | Bid | Ask |
|---|---|---|
| 0 | 100.00 × 500 | 100.01 × 400 |
| 1 | 100.00 × 700 | 100.01 × 400 |
| 2 | 100.00 × 700 | 100.01 × 100 |
| 3 | 100.01 × 200 | 100.02 × 600 |
| 4 | 100.01 × 150 | 100.02 × 600 |
| 5 | 100.01 × 150 | 100.02 × 900 |
| 6 | 100.00 × 800 | 100.01 × 250 |
Compute each and the cumulative OFI.
Show solutionHide solution
- 1: bid size at the same price: .
- 2: ask size falls by 300 (market buys or cancelled offers): .
- 3: bid moves up, so ; ask moves up, so (the last 100 on the old ask were removed). . The mid rises one tick, from 100.005 to 100.015.
- 4: 50 lots leave the bid: .
- 5: 300 lots join the ask: .
- 6: bid moves down, so ; ask moves down, so . , and the mid falls back to 100.005.
def ofi_event(prev, cur):
"""Cont-Kukanov-Stoikov contribution e_n from two snapshots (bid_px, bid_sz, ask_px, ask_sz)."""
pb0, qb0, pa0, qa0 = prev
pb1, qb1, pa1, qa1 = cur
e = 0
if pb1 >= pb0:
e += qb1
if pb1 <= pb0:
e -= qb0
if pa1 <= pa0:
e -= qa1
if pa1 >= pa0:
e += qa0
return e
book = [
(100.00, 500, 100.01, 400),
(100.00, 700, 100.01, 400),
(100.00, 700, 100.01, 100),
(100.01, 200, 100.02, 600),
(100.01, 150, 100.02, 600),
(100.01, 150, 100.02, 900),
(100.00, 800, 100.01, 250),
]
total = 0
for n in range(1, len(book)):
e = ofi_event(book[n - 1], book[n])
total += e
print(f"update {n}: e = {e:+5d} running OFI = {total:+5d}")
Output:
update 1: e = +200 running OFI = +200
update 2: e = +300 running OFI = +500
update 3: e = +300 running OFI = +800
update 4: e = -50 running OFI = +750
update 5: e = -300 running OFI = +450
update 6: e = -400 running OFI = +50
OFI peaked at as the mid rose a tick, then fell back to as it returned: the running OFI tracks the price path. No trade-signed measure could see updates 1, 4 and 5, which are pure limit-order activity.
Measuring it on a simulated book
The simulation below keeps only the best bid and ask on a one-tick grid. Limit orders join, cancellations leave, and market orders consume queues; when a queue empties the price shifts one tick and fresh queues appear. To give OFI something to predict, market-order direction is tilted by a hidden, persistent regime, a stand-in for a metaorder being worked. We compute OFI from consecutive snapshots in 50-event intervals and regress mid changes on it, at the same interval and at later ones.
import random
def ofi_event(prev, cur):
pb0, qb0, pa0, qa0 = prev
pb1, qb1, pa1, qa1 = cur
return (qb1 if pb1 >= pb0 else 0) - (qb0 if pb1 <= pb0 else 0) - (qa1 if pa1 <= pa0 else 0) + (qa0 if pa1 >= pa0 else 0)
def simulate_book(n_events, seed=11):
"""Best quotes on a one-tick grid; market-order direction follows a persistent hidden regime."""
rng = random.Random(seed)
bid, ask, qb, qa = 9999, 10000, 50, 50 # prices in ticks, sizes in lots
regime = 0 # -1 selling pressure, 0 neutral, +1 buying pressure
snaps = [(bid, qb, ask, qa)]
for _ in range(n_events):
if rng.random() < 0.002: # occasionally redraw the regime
regime = rng.choice((-1, 0, 1))
u = rng.random()
if u < 0.30: # a limit order joins one side
if rng.random() < 0.5:
qb += rng.randint(1, 10)
else:
qa += rng.randint(1, 10)
elif u < 0.60: # a cancellation
if rng.random() < 0.5:
qb = max(qb - rng.randint(1, 10), 1)
else:
qa = max(qa - rng.randint(1, 10), 1)
else: # a market order
size = rng.randint(1, 20)
if rng.random() < 0.5 + 0.1 * regime:
qa -= size
if qa <= 0: # ask cleared: price ticks up
bid, ask = bid + 1, ask + 1
qb, qa = rng.randint(20, 80), rng.randint(20, 80)
else:
qb -= size
if qb <= 0: # bid cleared: price ticks down
bid, ask = bid - 1, ask - 1
qb, qa = rng.randint(20, 80), rng.randint(20, 80)
snaps.append((bid, qb, ask, qa))
return snaps
def ols(x, y):
"""Slope and R^2 of a one-variable regression with intercept."""
n = len(x)
mx, my = sum(x) / n, sum(y) / n
sxx = sum((a - mx) ** 2 for a in x)
sxy = sum((a - mx) * (b - my) for a, b in zip(x, y))
syy = sum((b - my) ** 2 for b in y)
return sxy / sxx, sxy * sxy / (sxx * syy)
snaps = simulate_book(200_000)
window = 50 # events per interval
ofi, dmid = [], []
for start in range(0, len(snaps) - window, window):
seg = snaps[start:start + window + 1]
ofi.append(sum(ofi_event(seg[i - 1], seg[i]) for i in range(1, len(seg))))
dmid.append((seg[-1][0] + seg[-1][2]) / 2 - (seg[0][0] + seg[0][2]) / 2)
slope, r2 = ols(ofi, dmid)
print(f"{len(ofi)} intervals; same interval: slope {slope * 100:.2f} ticks per 100 lots, R^2 {r2:.2f}")
for lag in (1, 2, 5, 10, 20):
s, r = ols(ofi[:-lag], dmid[lag:])
print(f"OFI now vs move {lag:>2} intervals later: slope {s * 100:.3f}, R^2 {r:.3f}")
Output:
4000 intervals; same interval: slope 1.18 ticks per 100 lots, R^2 0.90
OFI now vs move 1 intervals later: slope 0.266, R^2 0.046
OFI now vs move 2 intervals later: slope 0.205, R^2 0.027
OFI now vs move 5 intervals later: slope 0.156, R^2 0.016
OFI now vs move 10 intervals later: slope 0.108, R^2 0.008
OFI now vs move 20 intervals later: slope 0.054, R^2 0.002
Reading it.
- Contemporaneous fit is excellent () because, in this book, price moves are mechanically caused by queues clearing. The slope, 1.18 ticks per 100 lots, is close to the stylised : queues here average around 37 lots per side (about 50 just after a refill), so is between 1.0 and 1.4 ticks per 100 lots. Real data are noisier (hidden orders, deeper levels, queue refills that are not random), which is why empirical is lower.
- Predictive power is small and decays. Lagged OFI predicts only because flow is persistent here (the regime). The slope falls by more than half within ten intervals and is a few per cent at best. With no persistent regime, lagged would be essentially zero.
Why predictive power decays, and what to do about it
- Prices absorb flow quickly. Market makers watch the same book. Whatever in OFI is predictable gets quoted into prices within seconds in liquid names, so the forecasting horizon is short and competition is about latency.
- Persistence of flow is the source. Where OFI does predict, it is because order flow is autocorrelated (metaorders split over hours, herding), the empirical cousin of the Kyle insider who trades gradually. Multi-level OFI (summing imbalance over several book levels, with weights) and normalising by depth add some forecasting power.
- Contemporaneous is not tradable. A regression of on uses information you only have at the end of the interval. Be ready for an interviewer to probe exactly this: it is a common form of look-ahead bias.
- Depth normalisation. Because , a fixed OFI matters more in a thin book. Scale OFI by recent average depth before pooling across times of day or across stocks.
- Uses: short-horizon fair-value adjustment for market makers (skew quotes with OFI), timing child orders in execution (pause passive selling when OFI is strongly negative), and as a feature in high-frequency return models, always evaluated out of sample and net of costs.
How this comes up in interviews
- "Define order-flow imbalance." Give the bid/ask case table, not only the formula, and say why cancellations and new limit orders count.
- "You regress 10-second returns on 10-second OFI and get . Can you trade it?" No: it is contemporaneous. Test lagged OFI, out of sample, after costs, and expect a far smaller number.
- "Why does the OFI coefficient vary through the day?" It scales inversely with depth, and depth has a strong intraday pattern.
Knowledge check
The book goes from bid 100.00 × 300 | ask 100.01 × 200 to bid 100.00 × 250 | ask 100.02 × 400. What is ?
Knowledge check
Why does OFI explain short-horizon price changes much better than signed trade volume?
Knowledge check
Average depth at the best quotes doubles while everything else stays the same. What happens to the OFI price-impact coefficient?
Key takeaways
- OFI sums, over book updates, bid-size increases and ask-size decreases at the best quotes, with whole-level jumps counted by the size that appeared or vanished.
- Contemporaneous price changes are close to linear in OFI with slope about , inversely proportional to depth.
- Contemporaneous fit is high; predictive power is small, comes from persistent flow and decays within a short horizon.
- Normalise by depth, test out of sample, and never treat a contemporaneous as a trading signal.
Practice problems
Finished reading?
Answer the 3 knowledge checks to complete this lesson (0 done).