5D Library › High-Frequency Limit Order Book Microstructure, Hawkes Self-Exciting Point Processes and Adverse-Selection Flow Toxicity · 中文
Chapter 2 Inventory Risk and Optimal Market Making: the Avellaneda–Stoikov Equations
From information to inventory
The market maker of Chapter 1 is risk-neutral: she does not care how much stock she is carrying, only whether her counterparty is informed. A real market maker faces another risk as well: after a fill she holds more or less of the asset, and before she can sell or buy it back the price may move against her. Ho and Stoll first wrote this down as a dynamic model: the dealer lowers (or raises) both quotes to attract trades that run against her inventory (Ho and Stoll 1981). In 2008 Avellaneda and Stoikov put this idea into a limit-order-book framework and gave approximate formulas that are still widely cited today (Avellaneda and Stoikov 2008).
The model and the reservation price
Let the mid-price follow a driftless Brownian motion . The market maker bids at and offers at ; a quote at distance from the mid is hit by market orders with intensity
an exponential form that Avellaneda and Stoikov motivate by a power-law distribution of market-order sizes and a logarithmic form of price impact. With cash and inventory , the market maker maximises the exponential utility at time , being the coefficient of risk aversion.
Reservation prices. Consider first a market maker who posts no orders and simply holds inventory . The prices at which she is indifferent to buying one more unit and to selling one more unit are
and their average is the reservation (indifference) price
With positive inventory the reservation price lies below the mid, so she is willing to sell more cheaply; the longer the time remaining, the higher the volatility and the greater the risk aversion, the larger the shift. This formula in the author’s framework checks out.
The optimal spread. Writing the quotes symmetrically about , substituting into the Hamilton–Jacobi–Bellman equation, expanding the value function in powers of inventory (equation 22 of the paper) and replacing the order-arrival term by its first-order (linear) approximation in the quoted distances (equation 26) gives a total spread that does not depend on inventory:
As the second term tends to , the optimal spread of a risk-neutral market maker facing exponential intensities; the first term compensates for inventory risk. The paper says explicitly that this is an approximation: with a quadratic approximation of the order-arrival term, the second-order inventory coefficient would satisfy a non-linear partial differential equation. So “derive the optimal two-sided quoted spread” in the author’s framework should be understood as “derive an approximately optimal spread”.
The exact solution: Guéant, Lehalle and Fernandez-Tapia
Evidence
Guéant, Lehalle and Fernandez-Tapia added inventory limits to the same model and showed that a change of variables turns the Hamilton–Jacobi–Bellman equations into a linear system of ordinary differential equations (Guéant et al. 2013). With and ,
(with only one neighbouring term at the boundaries ), and the optimal quotes are
They also proved that far from the horizon the optimal quotes converge to time-independent limits, and gave the closed-form approximation
with replaced by for . They note that the optimal quotes are “rather constant, except near the time horizon” and “certainly not linearly decreasing with time”, unlike the Avellaneda–Stoikov approximation; the part of the latter that is linear in is therefore accurate only as approaches .
The program below uses the parameters of Avellaneda and Stoikov’s own simulations (, , , ), integrates the system over the time remaining by a fourth-order Runge–Kutta method (), and compares three sets of quotes: the approximation (AS), the exact solution (ODE) and the long-horizon closed form (GLFT).
Calculation
# Optimal market-making quotes with exponential utility (risk aversion g), Brownian mid
# (volatility sig) and fill intensity A exp(-k delta). Three versions:
# AS - Avellaneda-Stoikov (2008) approximation: reservation price s - q g sig^2 (T-t),
# total spread g sig^2 (T-t) + (2/g) ln(1 + g/k)
# ODE - exact solution of Gueant, Lehalle & Fernandez-Tapia (2013) with |q| <= Q:
# dv_q/dtau = -a q^2 v_q + eta (v_{q-1} + v_{q+1}), v_q(tau=0) = 1,
# delta_b = ln(v_q / v_{q+1}) / k + ln(1 + g/k) / g (delta_a: v_{q-1})
# GLFT- their large-horizon closed-form approximation.
# Parameters are the illustrative ones of Avellaneda & Stoikov's simulations.
import math
SIG, A, K, G = 2.0, 140.0, 1.5, 0.1
a = K * G * SIG ** 2 / 2; eta = A * (1 + G / K) ** -(1 + K / G)
C = math.log(1 + G / K) / G
def solve(tau_end, Q=30, h=2e-4, keep=()):
v = {q: 1.0 for q in range(-Q, Q + 1)}; out = {}; n = round(tau_end / h)
def f(w):
return {q: -a * q * q * w[q] + eta * (w.get(q - 1, 0.0) + w.get(q + 1, 0.0))
for q in w}
for i in range(1, n + 1): # RK4 in tau = T - t
k1 = f(v); k2 = f({q: v[q] + h / 2 * k1[q] for q in v})
k3 = f({q: v[q] + h / 2 * k2[q] for q in v})
k4 = f({q: v[q] + h * k3[q] for q in v})
v = {q: v[q] + h / 6 * (k1[q] + 2 * k2[q] + 2 * k3[q] + k4[q]) for q in v}
m = max(v.values()); v = {q: x / m for q, x in v.items()} # only ratios matter
if any(abs(i * h - t) < h / 2 for t in keep):
out[round(i * h, 6)] = dict(v)
return out
def ode_quotes(v, q):
return (math.log(v[q] / v[q + 1]) / K + C, math.log(v[q] / v[q - 1]) / K + C)
def as_quotes(q, tau):
half = (G * SIG ** 2 * tau + 2 * C) / 2; r = -q * G * SIG ** 2 * tau
return half - r, half + r # (delta_b, delta_a)
def glft_quotes(q):
w = math.sqrt(SIG ** 2 * G / (2 * K * A) * (1 + G / K) ** (1 + K / G))
return C + (2 * q + 1) / 2 * w, C - (2 * q - 1) / 2 * w
if __name__ == "__main__":
taus = (0.1, 1.0, 5.0)
sol = solve(5.0, keep=taus)
print(f"sigma {SIG}, A {A}, k {K}, gamma {G}; (1/g)ln(1+g/k) = {C:.4f}; Q = 30")
for tau in taus:
v = sol[tau]
print(f"T - t = {tau}: q bid / ask distance from mid total spread")
print(" AS exact (ODE) GLFT approx AS ODE")
for q in (-4, -2, 0, 2, 4):
ab, aa = as_quotes(q, tau); ob, oa = ode_quotes(v, q); gb, ga = glft_quotes(q)
print(f" {q:+d} {ab:6.3f} {aa:6.3f} {ob:6.3f} {oa:6.3f} {gb:6.3f}"
f" {ga:6.3f} {ab + aa:5.2f} {ob + oa:5.2f}")
print("reservation-price shift for q = +1 (AS: -g sig^2 (T-t); ODE: (d_a - d_b)/2):")
for tau in taus:
ob, oa = ode_quotes(sol[tau], 1); ab, aa = as_quotes(1, tau)
print(f" T - t = {tau}: AS {(aa - ab) / 2:+.3f} exact {(oa - ob) / 2:+.3f}")
# sigma 2.0, A 140.0, k 1.5, gamma 0.1; (1/g)ln(1+g/k) = 0.6454; Q = 30
# T - t = 0.1: q bid / ask distance from mid total spread
# AS exact (ODE) GLFT approx AS ODE
# -4 0.505 0.825 0.528 0.797 0.464 0.878 1.33 1.32
# -2 0.585 0.745 0.595 0.730 0.568 0.775 1.33 1.32
# +0 0.665 0.665 0.662 0.662 0.671 0.671 1.33 1.32
# +2 0.745 0.585 0.730 0.595 0.775 0.568 1.33 1.32
# +4 0.825 0.505 0.797 0.528 0.878 0.464 1.33 1.32
# T - t = 1.0: q bid / ask distance from mid total spread
# AS exact (ODE) GLFT approx AS ODE
# -4 -0.755 2.445 0.462 0.880 0.464 0.878 1.69 1.34
# -2 0.045 1.645 0.567 0.776 0.568 0.775 1.69 1.34
# +0 0.845 0.845 0.672 0.672 0.671 0.671 1.69 1.34
# +2 1.645 0.045 0.776 0.567 0.775 0.568 1.69 1.34
# +4 2.445 -0.755 0.880 0.462 0.878 0.464 1.69 1.34
# T - t = 5.0: q bid / ask distance from mid total spread
# AS exact (ODE) GLFT approx AS ODE
# -4 -6.355 9.645 0.462 0.880 0.464 0.878 3.29 1.34
# -2 -2.355 5.645 0.567 0.776 0.568 0.775 3.29 1.34
# +0 1.645 1.645 0.672 0.672 0.671 0.671 3.29 1.34
# +2 5.645 -2.355 0.776 0.567 0.775 0.568 3.29 1.34
# +4 9.645 -6.355 0.880 0.462 0.878 0.464 3.29 1.34
# reservation-price shift for q = +1 (AS: -g sig^2 (T-t); ODE: (d_a - d_b)/2):
# T - t = 0.1: AS -0.040 exact -0.034
# T - t = 1.0: AS -0.400 exact -0.052
# T - t = 5.0: AS -2.000 exact -0.052With 0.1 of time remaining the three sets of quotes differ little (0.665, 0.662 and 0.671 at zero inventory): the approximation is accurate near the horizon. With 1 unit remaining, the exact solution already almost coincides with the long-horizon closed form (within two thousandths), with a total spread of 1.34; the approximation’s total spread is 1.69, and at inventories of ±4 one of its quotes is even negative (−0.755, meaning a quote on the far side of the mid). With 5 units remaining the approximation’s spread is 3.29 and one quote 9.645, while the exact solution is identical to its value at 1 unit remaining. The last lines show the reservation-price shift at an inventory of 1: the approximation, , grows linearly from −0.040 to −2.000; the exact solution saturates at −0.052. Figure 2 plots the difference.
Re-running the paper’s simulation
Avellaneda and Stoikov compared two strategies by simulation: an “inventory strategy” quoting by the formulas above, and a “symmetric strategy” quoting symmetrically about the mid with a spread equal to the inventory strategy’s average. The set-up is , , , , , ; the mid moves up or down by with probability one-half each step; 1,000 simulations for each .
Evidence
In the version of the paper posted on the author’s website, the results are: with , the inventory strategy has mean profit 65.0, standard deviation 6.6 and a standard deviation of final inventory of 2.9; the symmetric strategy 68.4, 12.7 and 8.4. With the two give 68.6, 8.7 and 68.8, 12.8. With , the inventory strategy gives 31.4 and 5.0, the symmetric strategy 44.0 and 11.0 (Avellaneda and Stoikov 2008). The paper concludes that the inventory strategy trades a slightly lower mean profit for a much smaller variance of profit, and that with its profit is clearly lower than the symmetric strategy’s.
The program below re-runs the simulation with the same set-up and adds a third strategy: quoting by the exact (ODE) solution. All three strategies use the same random numbers within a session.
Calculation
# Re-running the simulation design of Avellaneda & Stoikov (2008): s0 = 100, T = 1,
# sigma = 2, dt = 0.005, A = 140, k = 1.5; the mid moves +-sigma sqrt(dt) each step, a
# quote at distance delta is filled with probability A exp(-k delta) dt. Strategies:
# inventory - AS quotes around the reservation price s - q g sig^2 (T - t)
# symmetric - quotes symmetric about the mid; spread = inventory strategy's mean spread
# exact - quotes from the exact ODE solution (Gueant et al. 2013, |q| <= 30)
# 1,000 sessions per row, common random numbers across strategies within a session.
import math, random, statistics
S0, T, SIG, DT, A, K = 100.0, 1.0, 2.0, 0.005, 140.0, 1.5
NSTEP = round(T / DT)
def exact_table(g, Q=30, sub=25):
a = K * g * SIG ** 2 / 2; eta = A * (1 + g / K) ** -(1 + K / g)
c = math.log(1 + g / K) / g; h = DT / sub; v = [1.0] * (2 * Q + 1); tab = [None]
def f(w):
return [-a * (i - Q) ** 2 * w[i] + eta * ((w[i - 1] if i else 0.0)
+ (w[i + 1] if i < 2 * Q else 0.0)) for i in range(2 * Q + 1)]
for _ in range(NSTEP): # tab[j]: tau = j * DT
for _ in range(sub):
k1 = f(v); k2 = f([x + h / 2 * y for x, y in zip(v, k1)])
k3 = f([x + h / 2 * y for x, y in zip(v, k2)])
k4 = f([x + h * y for x, y in zip(v, k3)])
v = [x + h / 6 * (p + 2 * q + 2 * r + s)
for x, p, q, r, s in zip(v, k1, k2, k3, k4)]
m = max(v); v = [x / m for x in v]
tab.append({q: (math.log(v[q + Q] / v[q + Q + 1]) / K + c if q < Q else 99.0,
math.log(v[q + Q] / v[q + Q - 1]) / K + c if q > -Q else 99.0)
for q in range(-Q, Q + 1)})
return tab
def run(g, kind, seed, half=None, tab=None):
rng = random.Random(seed); s, x, q, spreads = S0, 0.0, 0, []
for j in range(NSTEP):
tau = T - j * DT
if kind == "inventory":
r = s - q * g * SIG ** 2 * tau
sp = g * SIG ** 2 * tau + 2 / g * math.log(1 + g / K)
bid, ask = r - sp / 2, r + sp / 2
elif kind == "symmetric":
bid, ask = s - half, s + half
else:
db, da = tab[NSTEP - j][max(-30, min(30, q))]
bid, ask = s - db, s + da
spreads.append(ask - bid)
u1, u2, u3 = rng.random(), rng.random(), rng.random()
if u1 < A * math.exp(-K * (ask - s)) * DT:
q -= 1; x += ask
if u2 < A * math.exp(-K * (s - bid)) * DT:
q += 1; x -= bid
s += SIG * math.sqrt(DT) * (1 if u3 < 0.5 else -1)
return x + q * s, q, statistics.fmean(spreads)
if __name__ == "__main__":
print("gamma strategy mean spread profit sd(profit) final q sd(q) profit/sd")
for g in (0.01, 0.1, 1.0):
inv = [run(g, "inventory", sd) for sd in range(1000)]
half = statistics.fmean(r[2] for r in inv) / 2; tab = exact_table(g)
rows = {"inventory": inv,
"symmetric": [run(g, "symmetric", sd, half=half) for sd in range(1000)],
"exact ODE": [run(g, "exact", sd, tab=tab) for sd in range(1000)]}
for name, rs in rows.items():
p = [r[0] for r in rs]; qq = [r[1] for r in rs]
m, sd = statistics.fmean(p), statistics.stdev(p)
print(f" {g:<5} {name:10s} {statistics.fmean(r[2] for r in rs):6.2f} "
f" {m:6.1f} {sd:6.1f} {statistics.fmean(qq):+6.2f}"
f" {statistics.stdev(qq):5.1f} {m / sd:6.1f}")
# gamma strategy mean spread profit sd(profit) final q sd(q) profit/sd
# 0.01 inventory 1.35 68.6 9.3 -0.03 5.3 7.4
# 0.01 symmetric 1.35 69.2 14.5 +0.07 9.0 4.8
# 0.01 exact ODE 1.34 68.8 10.2 +0.01 5.6 6.8
# 0.1 inventory 1.49 64.8 6.5 +0.07 2.9 9.9
# 0.1 symmetric 1.49 68.6 14.3 +0.01 8.8 4.8
# 0.1 exact ODE 1.34 67.9 7.3 +0.04 3.0 9.2
# 1.0 inventory 3.03 31.2 4.8 -0.03 1.6 6.5
# 1.0 symmetric 3.03 44.0 10.8 -0.07 5.2 4.1
# 1.0 exact ODE 1.21 63.9 5.6 -0.02 1.6 11.4The first two strategies reproduce the paper closely: with the inventory strategy gives 64.8 and 6.5 (paper 65.0 and 6.6) and the symmetric strategy 68.6 and 14.3 (paper 68.4 and 12.7); with , 31.2 and 4.8 against 44.0 and 10.8 (paper 31.4, 5.0 and 44.0, 11.0). The symmetric strategy’s standard deviation is somewhat higher than in the paper, perhaps because of implementation details (such as the order in which fills on the two sides are processed within a step); the conclusions are unaffected.
The third strategy shows the cost of the approximation. With the approximation’s mean spread is 3.03, against only 1.21 for the exact solution; the exact solution’s mean profit is 63.9 with a standard deviation of 5.6, a ratio of profit to standard deviation of 11.4, against 31.2, 4.8 and 6.5 for the approximate inventory strategy. In other words, the paper’s finding that “with the inventory strategy earns clearly less” is largely due to the approximation setting the spread too wide when is large, not to the cost of controlling inventory as such. With the exact solution’s mean profit is also higher (67.9 against 64.8), but its standard deviation is slightly higher (7.3 against 6.5) and its ratio of profit to standard deviation slightly lower (9.2 against 9.9). This is a reminder that the exact solution maximises expected exponential utility, not the ratio of profit to standard deviation; which measure is used to judge a strategy is itself a choice that has to be written down in advance (Chapter 8).
The model’s limits
Note
There is no adverse selection in this model. The mid is an exogenous Brownian motion and fills depend only on how far the quote is from the mid, not on where the price goes next; so the losses of Chapter 1, being picked off by the informed, do not exist here, and inventory risk comes only from directionless volatility. A real market maker faces both risks, and they interact: the informed trader’s orders are exactly what make a market maker accumulate inventory “in the wrong direction”. Exponential intensities, constant parameters, a single asset and continuous quoting are also assumptions; the monographs of Cartea, Jaimungal and Penalva and of Guéant discuss extensions with drift, adverse selection, the shape of the book and several assets (Cartea et al. 2015; Guéant 2016). The pilot environment of Chapter 8 adds “toxic pulses” to the Avellaneda–Stoikov model precisely to test whether inventory control can partly defend against adverse selection.
Evidence
Menkveld studied a high-frequency trader making markets simultaneously in two European markets (an incumbent exchange and a new entrant): it took part in 8.1 % of trades on the incumbent and 64.4 % on the entrant; four out of five of its trades were passive (its quote was consumed by others); it lost money on its inventory and earned it on the bid–ask spread; and its Sharpe ratio was very sensitive to assumptions about the cost of capital (Menkveld 2013). This agrees with the decomposition in this chapter’s model: spread income and inventory profit and loss are two parts that can be measured separately (step 3 of Chapter 8’s checklist does exactly this).
Hypothesis · not established · Inventory skew as a defence
The author’s hypothesis (not established): when brief, one-sided informed order flow (“toxic pulses”) is mixed in, a market-making strategy that skews its quotes by inventory (of the Avellaneda–Stoikov kind) has a higher cross-session Sharpe ratio than one quoting a fixed symmetric spread, because skewing slows the accumulation of inventory in the wrong direction. What would show it wrong: in the independent, pre-registered simulated-matching experiment of Chapter 8, a confidence interval for the difference in Sharpe ratios that includes zero or is negative.
Chapter summary
The Avellaneda–Stoikov reservation price and total-spread formula check out, but the spread formula depends on exponential arrival intensities, an expansion in inventory and a first-order approximation of the order-arrival term. The exact solution of Guéant and co-authors shows that optimal quotes hardly depend on time away from the horizon; with the paper’s parameters and one unit of time remaining, the approximation already overstates the reservation-price shift at inventory 1 about eightfold (−0.400 against −0.052). Re-running the paper’s simulation gives results close to the paper’s; quoting by the exact solution raises mean profit at from 31.2 to 63.9. There is no adverse selection in the model, which is the starting point of the experiment designed in Chapter 8.
References
Fan, Y. (2026). High-Frequency Limit Order Book Microstructure, Hawkes Self-Exciting Point Processes and Adverse-Selection Flow Toxicity: From the Avellaneda–Stoikov Market-Making Equations to VPIN, Read Beside Classical Chinese Thought on Market Equilibrium [高频限价订单簿微观结构、霍克斯自激点过程与逆向选择毒流] (Version 1.0.0). 5D Library. https://doi.org/10.5281/zenodo.23828065 · Licensed under Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0).