How to Read the Order Book Like a Quant. Every Model, Every Formula, Runnable Code
Source: http://x.com/i/article/2033932705759481856
You'll learn how to build a high-octane simulation engine for trading prediction markets - stepping up from basic Monte Carlo methods to a full-scale market maker with inventory management, order flow modeling, and bulletproof protection against informed traders. Disclaimer: Not Financial Advice & Do Your Own Research Part I: The Spread That's Bleeding You Dry
You're looking at a Polymarket order book. The contract: "Will Trump win Pennsylvania?" Bid - $0.61, Ask - $0.65.
Spread of $0.04. Seems like nothing.
You buy at $0.65. The contract resolves YES. You collect $1.00. Profit of $0.35, right?
No.
You just paid $0.04 to the market maker on entry. If you want to exit before resolution - you'll pay another $0.04. Total: $0.08 on a $1.00 contract. That's 8% in implicit transaction costs. On the NYSE, the spread for liquid stocks is 0.01-0.02%. You're trading an instrument where the rake against you is 400 times the equity average, and you don't even know why.
Now it gets worse. The spread is not a constant. It shifts depending on:
- Time to resolution
- Trading volume
- Information asymmetry between participants
- The probability that the other side knows something you don't
That last point is Kyle's model. And it's where any real understanding of the order book begins.
Part II: Kyle's Lambda - The Price of Information
In 1985, Albert Kyle wrote a paper that explained why price impact exists at all.
Imagine three participants:
- The insider - knows the true asset value v
- The market maker - doesn't know v , but sees the aggregate order flow
- Noise traders - trade randomly (not from information)
The market maker sets a price by watching the total flow y = x + u, where x is the insider's order and u is random noise.
In Kyle's equilibrium, the price is a linear function of flow:
p=μ+λyp = μ + λy
where λ is Kyle's lambda - the price impact coefficient:
λ=σv2σu\lambda = \frac{\sigma_v}{2\sigma_u}
σv is uncertainty about the true value; σu is the standard deviation of noise orders What this means in practice: if you see a 5,000 order move a contract from $0.62 to $0.67, then $\lambda \approx 0.001per dollar of volume. High lambda = the market thinks large players have information. Low lambda = a liquid market where big orders are just noise.
Estimating Lambda From Data python
import numpy as np
import pandas as pd
from scipy.stats import linregress
def estimate_kyle_lambda(prices: np.ndarray, volumes: np.ndarray,
signs: np.ndarray) -> dict:
"""
Estimate Kyle's lambda via regression:
Δp_t = λ * Q_t + ε_t
prices: array of prices [p_0, p_1, ..., p_T]
volumes: size of each trade
signs: +1 (buy) / -1 (sell)
"""
# Signed order flow
signed_volume = volumes * signs
# Price changes
price_changes = np.diff(prices)
# Drop zero-change ticks (tied trades)
mask = price_changes != 0
x = signed_volume[1:][mask]
y = price_changes[mask]
slope, intercept, r_value, p_value, std_err = linregress(x, y)
return {
'lambda': slope,
'r_squared': r_value**2,
'std_error': std_err,
'p_value': p_value,
'interpretation': (
'High informed trading' if slope > 0.002
else 'Normal liquidity'
)
}
# Example: synthetic data
np.random.seed(42)
N = 500
true_prob = 0.65
prices = [0.50]
signs_list = []
vols_list = []
for _ in range(N):
sign = np.random.choice([-1, 1], p=[0.45, 0.55])
vol = np.random.exponential(500)
lambda_true = 0.0015
noise = np.random.normal(0, 0.005)
new_price = np.clip(prices[-1] + lambda_true * sign * vol / 1000 + noise, 0.01, 0.99)
prices.append(new_price)
signs_list.append(sign)
vols_list.append(vol)
result = estimate_kyle_lambda(
np.array(prices),
np.array(vols_list),
np.array(signs_list)
)
print(f"Kyle's λ = {result['lambda']:.6f}")
print(f"R² = {result['r_squared']:.4f}")
print(f"Verdict: {result['interpretation']}")
If R² exceeds 0.15 - informed traders are active. Widen your spread or stay out entirely.
Part III: Order Flow as a Point Process
Now: when do orders actually arrive?
The naive answer is uniformly over time. That's wrong. Real order flow clusters: a news event hits -> a burst of trades -> quiet -> another burst. This pattern is described by the Hawkes process - a self-exciting point process where each event raises the probability of the next one. Order flow intensity:
λ(t)=μ+∑ti<tα⋅e−β(t−ti)\lambda(t) = \mu + \sum_{t_i < t} \alpha \cdot e^{-\beta(t - t_i)}
Where:
- μ - baseline intensity (background flow)
- α - how much each trade excites future ones
- β - decay rate of that excitement
- Stationarity condition: α < β (otherwise: activity explosion)
The ratio α/β is the branching ratio: the fraction of orders generated by previous orders rather than exogenous news. On liquid markets it runs 0.5-0.9. Most trades are reactions to other trades - not to new information.
Fitting a Hawkes Process to Historical Orders python
import numpy as np
from scipy.optimize import minimize
def hawkes_log_likelihood(params, event_times, T):
"""
Log-likelihood for a univariate Hawkes process.
params: [mu, alpha, beta]
event_times: array of trade timestamps (in seconds)
T: length of observation window
"""
mu, alpha, beta = params
if mu <= 0 or alpha <= 0 or beta <= 0 or alpha >= beta:
return 1e10
n = len(event_times)
integral_term = mu * T
# Recursive computation — O(n) instead of O(n²)
R = np.zeros(n)
for i in range(1, n):
R[i] = np.exp(-beta * (event_times[i] - event_times[i-1])) * (1 + R[i-1])
integral_term += (alpha / beta) * np.sum(1 - np.exp(-beta * (T - event_times)))
log_terms = np.log(mu + alpha * R)
ll = np.sum(log_terms) - integral_term
return -ll
def fit_hawkes(event_times: np.ndarray, T: float) -> dict:
"""Fit a Hawkes process to observed event times."""
best_result = None
best_ll = np.inf
# Multiple starting points (landscape is non-convex)
for _ in range(10):
x0 = [np.random.uniform(0.1, 2.0),
np.random.uniform(0.1, 0.8),
np.random.uniform(1.0, 5.0)]
result = minimize(
hawkes_log_likelihood,
x0, args=(event_times, T),
method='Nelder-Mead',
options={'xatol': 1e-6, 'fatol': 1e-6, 'maxiter': 5000}
)
if result.fun < best_ll:
best_ll = result.fun
best_result = result
mu, alpha, beta = best_result.x
branching_ratio = alpha / beta
return {
'mu': mu,
'alpha': alpha,
'beta': beta,
'branching_ratio': branching_ratio,
'log_likelihood': -best_ll,
'avg_intensity': mu / (1 - branching_ratio),
'interpretation': (
f'{branching_ratio:.1%} of trades are generated by prior trades '
f'(not exogenous information)'
)
}
# Simulate a Hawkes process (true params: mu=1.0, alpha=0.6, beta=2.0)
def simulate_hawkes(mu, alpha, beta, T):
times = []
t = 0
while t < T:
if times:
lambda_bar = mu + alpha * sum(
np.exp(-beta * (t - ti)) for ti in times
)
else:
lambda_bar = mu
t += np.random.exponential(1 / max(lambda_bar, mu))
if t > T:
break
lambda_current = mu + alpha * sum(
np.exp(-beta * (t - ti)) for ti in times if ti < t
)
if np.random.uniform() < lambda_current / lambda_bar:
times.append(t)
return np.array(times)
np.random.seed(123)
T_obs = 3600 # 1 hour in seconds
true_times = simulate_hawkes(mu=1.0, alpha=0.6, beta=2.0, T=T_obs)
fitted = fit_hawkes(true_times, T_obs)
print(f"Fitted parameters:")
print(f" μ (baseline intensity) = {fitted['mu']:.4f} trades/sec")
print(f" α (excitation) = {fitted['alpha']:.4f}")
print(f" β (decay) = {fitted['beta']:.4f}")
print(f" Branching ratio = {fitted['branching_ratio']:.4f}")
print(f" {fitted['interpretation']}")
Why does this matter on Polymarket? If the branching ratio suddenly jumps from 0.6 to 0.85 - something happened and the market is running hot. A market maker should be widening the spread, not tightening it.
Part IV: Optimal Execution - Stop Donating Money
Say you want to buy a $10,000 position in a contract priced at $0.55.
You could buy it all at once. But your order will move the price - you'll be trading against yourself. Price impact scales with size:
Δp≈λ⋅QΔp≈λ⋅Q
The optimal execution problem: split QQ into pieces {q1,q2,...,qn} to minimize total transaction costs while accounting for market risk.
The Almgren-Chriss model (2001) solves this in closed form.
Let Xk be the remaining position at step k, τ = T/N the length of each interval.
The optimal execution trajectory:
Xk∗=X0⋅sinh(κ(T−tk))sinh(κT)X_{k}^* = X_{0} \cdot \frac{\sinh(\kappa(T - t_{k}))}{\sinh(\kappa T)}
where: κ is the urgency parameter, σ~2 is the risk-adjusted variance, and η is the permanent component of market impact.
python
import numpy as np
from dataclasses import dataclass
@dataclass
class AlmgrenChrissParams:
"""Parameters for the optimal execution model."""
total_shares: float # Total position size to execute ($)
T: float # Execution horizon (hours)
N: int # Number of intervals
sigma: float # Contract volatility (units/hour)
eta: float # Permanent impact component ($ per $ of volume)
gamma: float # Temporary impact component
risk_aversion: float # λ: 0=ignore risk, ∞=trade instantly
def almgren_chriss_schedule(p: AlmgrenChrissParams) -> dict:
"""
Compute the optimal execution schedule via Almgren-Chriss.
Returns: order sizes, prices, expected costs.
"""
tau = p.T / p.N
kappa_sq = p.risk_aversion * p.sigma**2 / p.eta
kappa = np.sqrt(kappa_sq)
times = np.linspace(0, p.T, p.N + 1)
# Optimal remaining position
remaining = p.total_shares * np.sinh(kappa * (p.T - times)) / np.sinh(kappa * p.T)
# Trade sizes (negative diff = selling)
trade_sizes = -np.diff(remaining)
# Expected costs
temporary_impact = p.gamma * np.sum(trade_sizes**2) / tau
permanent_impact = 0.5 * p.eta * p.total_shares**2
variance = p.sigma**2 * tau * np.sum(remaining[:-1]**2)
total_cost = temporary_impact + permanent_impact
return {
'times': times[:-1],
'remaining': remaining[:-1],
'trade_sizes': trade_sizes,
'expected_cost': total_cost,
'implementation_shortfall': total_cost / p.total_shares,
'kappa': kappa,
'urgency': 'High (trade fast)' if kappa > 2 else
'Low (trade slowly)' if kappa < 0.5 else 'Moderate'
}
params = AlmgrenChrissParams(
total_shares=10_000,
T=4.0,
N=8,
sigma=0.02,
eta=0.001,
gamma=0.005,
risk_aversion=1e-6
)
schedule = almgren_chriss_schedule(params)
print("Optimal execution schedule (Almgren-Chriss):")
print(f"Urgency: {schedule['urgency']}, κ = {schedule['kappa']:.4f}")
print(f"Expected cost: ${schedule['expected_cost']:.2f}")
print(f"Implementation shortfall: {schedule['implementation_shortfall']*100:.3f}%")
print()
print(f"{'Hour':>5} {'Order $':>10} {'Remaining $':>12}")
print("-" * 32)
for t, size, rem in zip(schedule['times'],
schedule['trade_sizes'],
schedule['remaining']):
print(f"{t:>5.1f} {size:>10.0f} {rem:>12.0f}")
Higher risk_aversion means trade faster - larger early orders. Lower means spread it evenly. This is not abstract: on Polymarket, sloppy execution of a $10k position can cost $200-$400 in avoidable slippage.
Part V: A Market Maker With Dynamic Hedging
Now we assemble everything into a working bot.
The Avellaneda-Stoikov market-making strategy (2008): the market maker quotes bid and ask around a "fair price" rt - not the market mid, but an inventory-adjusted reservation price:
rt=st−q⋅γ⋅σ2⋅(T−t)r_{t} = s_{t} - q \cdot \gamma \cdot \sigma^{2} \cdot (T - t)
where q is current inventory (positive = long YES tokens), St is the mid-price.
Then:
δbid=rt−st+1γln(1+γκ)δask=st−rt+1γln(1+γκ)\begin{align*} \delta^{bid} &= r_{t} - s_{t} + \frac{1}{\gamma} \ln \left(1 + \frac{\gamma}{\kappa}\right) \\ \delta^{ask} &= s_{t} - r_{t} + \frac{1}{\gamma} \ln \left(1 + \frac{\gamma}{\kappa}\right) \end{align*}
Here κ is the intensity of incoming executions, γ is the market maker's risk aversion.
python
import numpy as np
from dataclasses import dataclass, field
from typing import List, Tuple
@dataclass
class MarketMakerState:
"""Market maker state at each point in time."""
mid_price: float = 0.50
inventory: float = 0.0 # in $ (+ = long YES, - = short YES)
cash: float = 0.0
pnl_history: List[float] = field(default_factory=list)
quote_history: List[Tuple] = field(default_factory=list)
class AvellanedaStoikovMM:
"""
Market maker based on the Avellaneda-Stoikov model,
adapted for binary prediction markets.
"""
def __init__(
self,
gamma: float = 0.1,
kappa: float = 1.5,
sigma: float = 0.05,
T_horizon: float = 24.0,
max_inventory: float = 5000,
kyle_lambda: float = 0.001,
):
self.gamma = gamma
self.kappa = kappa
self.sigma = sigma
self.T = T_horizon
self.max_inventory = max_inventory
self.kyle_lambda = kyle_lambda
self.state = MarketMakerState()
def reservation_price(self, mid: float, inventory: float,
time_remaining: float) -> float:
"""
Inventory-adjusted fair price.
Large long inventory → shade price down to attract sells.
"""
return mid - inventory * self.gamma * self.sigma**2 * time_remaining
def optimal_spread(self, time_remaining: float) -> float:
"""Optimal half-spread in the A-S model."""
base_spread = self.gamma * self.sigma**2 * time_remaining
execution_term = (1 / self.gamma) * np.log(1 + self.gamma / self.kappa)
return base_spread + execution_term
def quote(self, mid: float, time_remaining: float) -> Tuple[float, float]:
"""Return (bid, ask) adjusted for inventory."""
r = self.reservation_price(mid, self.state.inventory, time_remaining)
half_spread = self.optimal_spread(time_remaining)
bid = r - half_spread
ask = r + half_spread
bid = np.clip(bid, 0.01, 0.99)
ask = np.clip(ask, 0.01, 0.99)
ask = max(ask, bid + 0.01)
# Inventory blowup protection
if abs(self.state.inventory) > self.max_inventory * 0.8:
if self.state.inventory > 0:
ask = min(ask, mid - 0.005) # aggressively unload longs
else:
bid = max(bid, mid + 0.005) # aggressively unload shorts
self.state.quote_history.append((bid, ask, r, time_remaining))
return bid, ask
def fill_bid(self, price: float, size: float):
"""Our bid was hit — we bought YES tokens."""
self.state.inventory += size
self.state.cash -= price * size
self._update_pnl(price)
def fill_ask(self, price: float, size: float):
"""Our ask was lifted — we sold YES tokens."""
self.state.inventory -= size
self.state.cash += price * size
self._update_pnl(price)
def _update_pnl(self, mid: float):
mark_to_market = self.state.inventory * mid + self.state.cash
self.state.pnl_history.append(mark_to_market)
def simulate_trading_session(true_prob: float = 0.65,
n_steps: int = 1000,
T_hours: float = 24.0) -> dict:
"""Simulate a trading session with the market maker and random takers."""
mm = AvellanedaStoikovMM(gamma=0.1, kappa=1.5, sigma=0.05, T_horizon=T_hours)
price = 0.50
prices = [price]
spreads = []
for step in range(n_steps):
time_remaining = T_hours * (1 - step / n_steps)
# Price evolves: random walk + mean reversion toward true_prob
drift = 0.001 * (true_prob - price)
shock = np.random.normal(0, 0.008)
price = np.clip(price + drift + shock, 0.01, 0.99)
prices.append(price)
bid, ask = mm.quote(price, time_remaining)
spread = ask - bid
spreads.append(spread)
# Takers arrive with probability ∝ 1/spread
trade_prob = min(0.3, 0.05 / spread)
if np.random.random() < trade_prob:
size = np.random.exponential(200)
if np.random.random() < 0.5:
mm.fill_bid(bid, size)
else:
mm.fill_ask(ask, size)
# Resolution
final_price = 1.0 if np.random.random() < true_prob else 0.0
final_pnl = mm.state.inventory * final_price + mm.state.cash
return {
'final_pnl': final_pnl,
'final_inventory': mm.state.inventory,
'avg_spread': np.mean(spreads),
'n_fills': len(mm.state.pnl_history),
'prices': prices,
'pnl_history': mm.state.pnl_history,
}
np.random.seed(42)
results = [simulate_trading_session(true_prob=0.65) for _ in range(100)]
pnls = [r['final_pnl'] for r in results]
print("Avellaneda-Stoikov Market Maker — 100 simulations:")
print(f" Mean P&L: ${np.mean(pnls):>8.2f}")
print(f" Median P&L: ${np.median(pnls):>8.2f}")
print(f" Std P&L: ${np.std(pnls):>8.2f}")
print(f" Sharpe (approx): {np.mean(pnls)/np.std(pnls):>8.3f}")
print(f" % profitable sessions: {np.mean([p > 0 for p in pnls])*100:.0f}%")
print(f" Average spread: {np.mean([r['avg_spread'] for r in results]):.4f}")
Part VI: Three Things That Will Break Your Model
Before building a production system, you need to know the three patterns where each of these models fails.
Adverse selection:
When an informed trader hits your quotes, you systematically sell cheap and buy expensive. The metric is the Volume-synchronized Probability of Informed Trading (VPIN):
VPIN=∣Vbuy−Vsell∣Vbuy+VsellVPIN = \frac{|\text{V}^{\text{buy}} - \text{V}^{\text{sell}}|}{\text{V}^{\text{buy}} + \text{V}^{\text{sell}}}
If VPIN exceeds 0.7 - informed money has entered the market. Widen your spread immediately.
Inventory death spiral:
The market maker accumulates a one-sided position and can't get out. The fix is dynamic hedging in a correlated contract. If you're holding $5,000 long in "Trump wins Pennsylvania", buy $2,000 short in "Trump wins Michigan" (high correlation).
Latency arbitrage:
Fast bots see a price move in a correlated contract and fill against your stale quote before you can update it. The only defense is speed and leading indicators.
python
def compute_vpin(buy_volumes: np.ndarray, sell_volumes: np.ndarray,
bucket_size: int = 50) -> np.ndarray:
"""
VPIN — Volume-synchronized Probability of Informed Trading.
High VPIN = high probability of informed order flow.
"""
n_buckets = len(buy_volumes) // bucket_size
vpin_values = []
for i in range(n_buckets):
start = i * bucket_size
end = start + bucket_size
V_buy = buy_volumes[start:end].sum()
V_sell = sell_volumes[start:end].sum()
V_total = V_buy + V_sell
if V_total > 0:
vpin = abs(V_buy - V_sell) / V_total
vpin_values.append(vpin)
return np.array(vpin_values)
np.random.seed(0)
# Normal trading: roughly balanced buy/sell
normal_buy = np.random.exponential(100, 500)
normal_sell = np.random.exponential(100, 500)
# Informed trading: large one-sided flow
informed_buy = np.random.exponential(300, 500)
informed_sell = np.random.exponential(50, 500)
vpin_normal = compute_vpin(normal_buy, normal_sell)
vpin_informed = compute_vpin(informed_buy, informed_sell)
print(f"VPIN (normal market): {vpin_normal.mean():.3f} (healthy: < 0.4)")
print(f"VPIN (informed trading): {vpin_informed.mean():.3f} (danger: > 0.7)")
print()
print("Rule: if VPIN > 0.65, double your spread")
print(" if VPIN > 0.80, pull your quotes entirely")
---
## Part VII: The Production Stack
The full system, from data feed to execution:
LAYER 1: DATA INGESTION
- WebSocket from Polymarket CLOB API (real-time L2 order book)
- Historical trade tape (ticker, timestamp, size, side)
- External signals (news, polls, on-chain data)
LAYER 2: PARAMETER ESTIMATION (recalculated every 15 minutes)
- Kyle's λ from regression on the last 200 trades
- Hawkes parameters (mu, alpha, beta) via MLE
- VPIN from a rolling 500-tick window
LAYER 3: QUOTING
- Avellaneda-Stoikov with adaptive parameters
- Inventory management: hard limits + soft skew
- Spread widening triggered by elevated VPIN
LAYER 4: EXECUTION
- Almgren-Chriss for large hedging orders
- Latency monitoring (target: < 50ms from tick to new quote)
- Circuit breakers: halt on P&L drawdown > 3%
LAYER 5: MONITORING
- P&L attribution (spread P&L vs inventory P&L vs hedge)
- Adverse selection ratio
- Fill rate by side (asymmetry = something is wrong)
Pre-Launch Checklist
Before running any of this with real money - answer these questions:
Do you know Kyle's λ for your contract?
Without it, you don't know what trading actually costs you.
Have you modeled the order flow?
A branching ratio above 0.8 is a danger zone.
Do you have hard inventory limits?
Without them, the first informed trader will zero you out.
Are you computing VPIN in real time?
It's your primary early-warning signal.
Have you tested resolution?
A simulation without modeling the final binary payoff is incomplete.
If you answered no to any of these - don't trade real money.
References
- Kyle, A.S. (1985). "Continuous Auctions and Insider Trading." Econometrica
- Avellaneda, M. & Stoikov, S. (2008). "High-frequency trading in a limit order book." Quantitative Finance
- Almgren, R. & Chriss, N. (2001). "Optimal execution of portfolio transactions." Journal of Risk
- Hawkes, A.G. (1971). "Spectra of some self-exciting and mutually exciting point processes." Biometrika
- Easley, D. et al. (2012). "Flow Toxicity and Liquidity in a High-frequency World." Review of Financial Studies
- Glosten, L. & Milgrom, P. (1985). "Bid, Ask, and Transaction Prices in a Specialist Market." JFE
- Farmer, Patelli & Zovko (2005). "The Predictive Power of Zero Intelligence." PNAS