Skip to content
LeetQuant
E08 Advanced Market Microstructure

E08 · Advanced Market Microstructure · Lesson 4 of 4

Order-Flow Imbalance

Order-flow imbalance at the best quotes (Cont–Kukanov–Stoikov): its definition from book updates, its linear relation to short-horizon price changes, a runnable toy book, and how fast its predictive power decays.

17 min

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 nn-th book update have best bid price and size (PnB,qnB)(P^B_n, q^B_n) and best ask (PnA,qnA)(P^A_n, q^A_n). Cont, Kukanov and Stoikov define the contribution of update nn as

en=1{PnB≥Pn−1B} qnB−1{PnB≤Pn−1B} qn−1B−1{PnA≤Pn−1A} qnA+1{PnA≥Pn−1A} qn−1A,e_n = \mathbb{1}\{P^B_n \ge P^B_{n-1}\}\,q^B_n - \mathbb{1}\{P^B_n \le P^B_{n-1}\}\,q^B_{n-1} - \mathbb{1}\{P^A_n \le P^A_{n-1}\}\,q^A_n + \mathbb{1}\{P^A_n \ge P^A_{n-1}\}\,q^A_{n-1},

and OFI over an interval is the sum of ene_n over its updates. Case by case, for the bid side:

Bid changeContributionMeaning
Same priceqnB−qn−1Bq^B_n - q^B_{n-1}Net size added (joins minus cancels and sells hitting it)
Price up+qnB+q^B_nA whole new bid level appeared: buying pressure
Price down−qn−1B-q^B_{n-1}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 (PAP^A up) is buying pressure.

The linear relation

Over short intervals (seconds), price changes are close to linear in OFI:

ΔPk=β OFIk+ϵk,β≈cdepth.\Delta P_k = \beta\,\text{OFI}_k + \epsilon_k, \qquad \beta \approx \frac{c}{\text{depth}}.

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 DD at each level, β=δ/(2D)\beta = \delta/(2D) for tick size δ\delta: clearing a level takes about DD shares of flow, and the new level that appears behind the price move adds about another DD. On their sample of US stocks they report contemporaneous R2R^2 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:

UpdateBidAsk
0100.00 × 500100.01 × 400
1100.00 × 700100.01 × 400
2100.00 × 700100.01 × 100
3100.01 × 200100.02 × 600
4100.01 × 150100.02 × 600
5100.01 × 150100.02 × 900
6100.00 × 800100.01 × 250

Compute each ene_n and the cumulative OFI.

Show solution
  • 1: bid size +200+200 at the same price: e=+200e = +200.
  • 2: ask size falls by 300 (market buys or cancelled offers): e=+300e = +300.
  • 3: bid moves up, so +q3B=+200+q^B_3 = +200; ask moves up, so +q2A=+100+q^A_2 = +100 (the last 100 on the old ask were removed). e=+300e = +300. The mid rises one tick, from 100.005 to 100.015.
  • 4: 50 lots leave the bid: e=−50e = -50.
  • 5: 300 lots join the ask: e=−300e = -300.
  • 6: bid moves down, so −q5B=−150-q^B_5 = -150; ask moves down, so −q6A=−250-q^A_6 = -250. e=−400e = -400, 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 +800+800 as the mid rose a tick, then fell back to +50+50 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 (R2=0.90R^2 = 0.90) 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 δ/(2D)\delta/(2D): queues here average around 37 lots per side (about 50 just after a refill), so 1/(2D)1/(2D) 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 R2R^2 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 R2R^2 is a few per cent at best. With no persistent regime, lagged R2R^2 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 R2R^2 is not tradable. A regression of ΔPt\Delta P_t on OFIt\text{OFI}_t 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 β∝1/depth\beta \propto 1/\text{depth}, 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 R2=0.6R^2 = 0.6. 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 ene_n?

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 δ/(2D)\delta/(2D), 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 R2R^2 as a trading signal.

Practice problems

Finished reading?

Answer the 3 knowledge checks to complete this lesson (0 done).