A rigorous Avellaneda–Stoikov optimal market-making solver — the PDE value function with adverse selection, asymmetric fill rates, and inventory-aware quoting.
pip install avellaneda-stoikovfrom avellaneda_stoikov import ASParams, solve_value_function, optimal_depths
p = ASParams(sigma=0.6, lam_plus=1.0, lam_minus=1.0,
kappa_plus=100.0, kappa_minus=100.0,
phi=1e-4, alpha=1e-3, q_max=5, T=60.0, n_steps=600)
t_grid, h_grid = solve_value_function(p) # solve once
ask, bid = optimal_depths(h_grid, t_grid, p.q_max, t=0.0, q=2,
kappa_plus=p.kappa_plus, kappa_minus=p.kappa_minus)
print(ask, bid) # optimal ask/bid depths when long 2 units (ask < bid: skew to sell)Solves the Avellaneda–Stoikov / Cartea Ch. 10 market-making problem. Ansatz
H(t,x,S,q)=x+q·S+h(t,q); the HJB reduces to an ODE system in inventory q,
integrated backward from h(T,q) = −α·q². Optimal feedback depths:
δ⁺* = 1/κ⁺ + ε⁺ + h(t,q) − h(t,q−1) (ask)
δ⁻* = 1/κ⁻ + ε⁻ + h(t,q) − h(t,q+1) (bid)
Most open-source A-S implementations are minimal and unmaintained. This one adds:
- Adverse selection (
ε⁺/ε⁻, Cartea §10.4) — quote wider when fills predict mid moves. - Asymmetric fill rates
κ⁺ ≠ κ⁻solved directly in the PDE. - Finite-difference PDE solver (not just the closed-form approximation).
- Inventory skew, boundary and overshoot handling, validated by tests.
Typed, mypy --strict, tested, MIT.
ASParams(...)— model + strategy parameters (validate()).solve_value_function(p) -> (t_grid, h_grid)— solve once.optimal_depths(h_grid, t_grid, q_max, t, q, kappa_plus, kappa_minus, epsilon_plus=0, epsilon_minus=0)—(δ⁺, δ⁻),Noneon a boundary.reservation_price_and_spread(...)—(r, bid, ask).
- Avellaneda & Stoikov (2008), High-frequency trading in a limit order book.
- Cartea, Jaimungal & Penalva, Algorithmic and High-Frequency Trading, Ch. 10.
Extracted from a live trading bot. This is the solver — calibration (estimating λ, κ, ε), execution and risk are left to you; that's where the edge lives. Out of scope for now: regime switching, order-flow drift (§10.5), non-linear fill models.
MIT.
