1. Learning Objectives

By the end of this lesson, you will be able to:

  • Understand the role of market makers and the economics of providing liquidity, including the trade-off between adverse selection and inventory risk.

  • Formulate the market making problem as an optimal control problem and derive the Avellaneda-Stoikov model for optimal bid-ask quotes.

  • Implement reinforcement learning-based market making agents that adapt to changing market conditions.

  • Design high-frequency execution algorithms (VWAP, TWAP, Implementation Shortfall) and understand their mathematical foundations.

  • Apply reinforcement learning to optimal execution, including the use of deep Q-networks and actor-critic methods.

  • Analyze the regulatory and technological landscape of HFT and algorithmic trading.


2. Market Making: The Economics of Liquidity Provision

A market maker (MM) continuously provides liquidity by placing limit orders on both sides of the book. The MM earns the bid-ask spread on each trade but faces two risks:

  1. Adverse selection: Informed traders may trade against the MM, causing the MM to buy overvalued assets or sell undervalued assets.

  2. Inventory risk: The MM may accumulate a large inventory position, which is subject to price fluctuations.

The optimal market making strategy balances these risks by adjusting quotes dynamically based on the state of the order book, the inventory, and the volatility.

2.1 The Avellaneda-Stoikov Model

The Avellaneda-Stoikov model (2008) provides a closed-form solution for the optimal bid and ask quotes of a market maker. It assumes:

  • The MM has an inventory of q shares (can be positive or negative).

  • The asset price follows a Brownian motion: dS_t = σ dW_t.

  • The MM can place limit orders at prices S_t + δ^a (ask) and S_t - δ^b (bid).

  • The arrival rates of market orders are exponential in the spread:
    λ^a(δ^a) = A * exp(-κ * δ^a) (for buy orders hitting the ask)
    λ^b(δ^b) = A * exp(-κ * δ^b) (for sell orders hitting the bid)
    where A and κ are positive constants.

The MM’s objective is to maximize the expected utility of final wealth over a finite horizon T, with a risk penalty for inventory.

The optimal bid and ask spreads are:

δ^a(t, q) = (1/κ) * ln(1 + κ/γ) + (q * γ * σ² * (T-t)) / (2 * (1 + κ/γ))

δ^b(t, q) = (1/κ) * ln(1 + κ/γ) - (q * γ * σ² * (T-t)) / (2 * (1 + κ/γ))

where γ is the risk aversion coefficient. The term q * γ * σ² * (T-t) / 2 represents the inventory risk adjustment. When the MM has a positive inventory (long), the bid is lowered (less aggressive buying) and the ask is raised (more aggressive selling) to reduce the inventory. The half-spread is:

S_t = δ^a + δ^b = (2/κ) * ln(1 + κ/γ)

which is independent of inventory. The MM shifts the mid-price (the skew) to manage inventory:

skew = (δ^a - δ^b)/2 = (q * γ * σ² * (T-t)) / (2 * (1 + κ/γ))

A positive inventory increases the skew, making the MM more eager to sell.

2.2 Extensions to the Avellaneda-Stoikov Model

The basic model has been extended in several ways:

  • Reinforcement learning: Learn the optimal quoting policy directly from data, without assuming exponential order arrival.

  • Multiple assets: Model the correlation between different assets and the cross-sectional impact on inventory risk.

  • Non-linear utilities: Use more complex utility functions (e.g., power utility) to capture different risk preferences.

  • Continuous learning: Update the model parameters (A, κ, σ) in real-time using online estimation.


3. Reinforcement Learning for Market Making

Reinforcement learning provides a natural framework for market making. The agent observes the LOB state, its inventory, and market variables, and chooses the bid and ask prices (or spreads).

3.1 State Representation

The state space must capture the information needed to make optimal quotes. A typical state includes:

  • Inventory: q_t, the current position.

  • Time remaining: τ = T - t.

  • Mid-price change: The recent mid-price movement (e.g., last 10 returns).

  • Order book imbalance: I_t = (V^B - V^A) / (V^B + V^A) at the top levels.

  • Volatility: The rolling standard deviation of returns or the bid-ask spread.

  • Market liquidity: The average order size, the number of orders in the book.

3.2 Action Space

The action space can be:

  • Discrete: Choose the bid and ask spreads from a discrete set (e.g., 0.5, 1, 1.5, 2 ticks).

  • Continuous: Output the skew and half-spread directly.

The action space must be bounded to ensure the quotes are within a reasonable range.

3.3 Reward Function

The reward is the profit from executed trades, minus a penalty for inventory risk:

r_t = (Price_sell - Price_buy) * Volume - λ * q_t²

where λ is an inventory penalty coefficient. The squared inventory term encourages the MM to keep inventory near zero. The profit term is the spread earned on each trade.

3.4 Algorithm Selection

DQN (with a continuous action space via discretization) can be used for small action spaces. For continuous actions, PPO or TD3 are more suitable. The training is done in a simulated environment that replicates the market dynamics (e.g., using historical data or a stochastic model). After training, the agent can be deployed in a live market with a safety layer (e.g., maximum spread, stop-loss).


4. High-Frequency Execution Algorithms

Execution algorithms (or “algos”) are used to execute large orders with minimal market impact and at a favorable price. They are typically passive (non-aggressive) and spread the order over time.

4.1 Volume-Weighted Average Price (VWAP)

The VWAP is the average price of an asset over a given period, weighted by the volume. The VWAP algorithm aims to execute an order such that the average execution price is as close to the market VWAP as possible.

The VWAP over a period [0, T] is:

VWAP = (∑_{t=1}^{T} P_t * V_t) / (∑_{t=1}^{T} V_t)

where P_t is the price and V_t is the volume at time t.

The VWAP algorithm:

  1. Volume profile estimation: Estimate the expected volume distribution over the day (using historical patterns).

  2. Order slicing: Split the order into smaller slices, each proportional to the expected volume in that time bin.

  3. Execution: Place limit orders (or market orders if needed) to fill each slice.

The algorithm is passive; it does not seek to outperform the VWAP, only to track it. It is suitable for orders that are not too large relative to the market volume.

4.2 Time-Weighted Average Price (TWAP)

TWAP is a simpler version of VWAP that divides the order equally over time, regardless of volume. The execution time is divided into N intervals, and Q/N shares are executed in each interval. TWAP is easy to implement but can be more costly if market volume is uneven.

4.3 Implementation Shortfall (IS)

The IS algorithm aims to minimize the difference between the execution price and the price at the time of the decision (the arrival price). The objective is to minimize:

IS = ∑_{t} (P_t - P_0) * q_t + (P_T - P_0) * Q_remaining

where:

  • P_0 is the arrival price.

  • P_t is the execution price at time t.

  • q_t is the quantity executed at time t.

  • Q_remaining is the quantity not executed by the end of the horizon.

The IS algorithm trades off between urgency (to avoid price drift) and cost (market impact). It is often used for urgent orders where the price direction is a concern.

4.4 Adaptive Execution with Reinforcement Learning

RL can be used to learn an optimal execution policy that adapts to market conditions. The state includes:

  • The remaining quantity.

  • The time remaining.

  • The current price and volatility.

  • The market impact model (estimated from historical data).

The action is the quantity to execute at the current step. The reward is the negative of the cost (or the profit from the trade). The agent learns a policy that minimizes the expected execution cost, dynamically adjusting to market liquidity and volatility.

The Bellman equation for optimal execution:

V(t, q, state) = max_{q_t} E[ -C_t(q_t) + V(t+1, q - q_t, state') ]

where C_t is the cost of executing q_t shares (market impact + spread + opportunity cost). The agent can use a deep Q-network to approximate V.


5. Statistical Arbitrage and Pair Trading

Statistical arbitrage (StatArb) exploits mean-reverting relationships between assets. The classic implementation is pair trading:

  1. Select a pair: Choose two historically correlated stocks (e.g., from the same sector).

  2. Compute the spread: The spread is the difference in prices (or returns): s_t = P_{1,t} - β * P_{2,t}, where β is the hedge ratio (estimated via cointegration or regression).

  3. Test for stationarity: The spread should be stationary (mean-reverting). Use the ADF test.

  4. Trading signals: When the spread deviates from its mean by more than k * σ, take a long-short position. For example, if the spread is too high, short asset 1 and long asset 2. Close the position when the spread reverts to the mean.

5.1 Cointegration vs. Correlation

Correlation does not imply cointegration. Cointegration is a stronger property: a linear combination of two (or more) non-stationary series is stationary. The Engle-Granger test is used to test for cointegration.

The Error Correction Model (ECM): If two series are cointegrated, we can model the dynamics as:

ΔP_{1,t} = α_1 (P_{1,t-1} - β P_{2,t-1}) + ε_{1,t}
ΔP_{2,t} = α_2 (P_{1,t-1} - β P_{2,t-1}) + ε_{2,t}

The term (P_{1,t-1} - β P_{2,t-1}) is the error correction term. The coefficients α₁ and α₂ capture the speed of reversion.

5.2 Machine Learning for StatArb

Instead of a fixed pair, we can use machine learning to identify mean-reverting relationships in a high-dimensional setting:

  • Clustering: Group assets with similar price dynamics.

  • PCA: Extract the common factors and use the residuals as mean-reverting strategies.

  • Reinforcement learning: Learn a policy that dynamically selects assets with the most favorable spread.


6. Regulatory and Technological Landscape

6.1 Regulations Affecting HFT
  • MiFID II (Europe): Requires firms to keep records of all trades and to have risk controls in place. It also restricts the use of certain order types (e.g., “stub quotes”).

  • Reg NMS (US): Prohibits trade-throughs (executing at a price worse than the best available price). It also encourages the competition of exchanges.

  • Market abuse regulations: Prohibit spoofing (placing orders with no intention of executing) and layering (multiple orders to manipulate prices).

6.2 Technology Infrastructure

HFT requires low-latency infrastructure:

  • Colocation: Placing servers in the same data center as the exchange to minimize latency (typically < 1 ms).

  • FPGA (Field-Programmable Gate Arrays): Custom hardware for high-speed order processing.

  • Kernel bypass: Using network cards that bypass the operating system kernel to reduce latency.

  • Coherent data feeds: Receiving and processing market data from multiple exchanges simultaneously.

6.3 Ethical Considerations
  • Front-running: HFT firms have been accused of front-running orders by detecting large orders and trading ahead of them. This is illegal in many jurisdictions.

  • Flash crashes: High-frequency trading can amplify volatility, as seen in the 2010 Flash Crash.

  • Fairness: HFT firms with better technology have an advantage, raising questions about market fairness.


7. Summary for the AI Practitioner

  • Market making is a fundamental financial activity that involves providing liquidity in exchange for the spread. The Avellaneda-Stoikov model provides a benchmark, but RL agents can outperform by learning dynamic policies.

  • Execution algorithms (VWAP, TWAP, IS) are essential for large-order execution; RL can improve them by adapting to market conditions.

  • Statistical arbitrage exploits mean-reverting relationships; cointegration testing is a rigorous method for identifying such relationships.

  • HFT operates at the intersection of finance, technology, and regulation. AI models must be designed to operate within the regulatory framework and with robust risk controls.