High-Frequency Trading and Latency Arbitrage › Key Parameters
Quantitative FinanceAll chapters High-Frequency Trading and Latency Arbitrage › Key ParametersOne section · Sections in this chapter12 / 18 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
The key parameters for HFT market maker simulation are:
- n_periods: Number of time periods simulated, with more periods producing more precise long-term performance estimates.
- true_price_vol: True price volatility per period (percentage), representing market uncertainty that creates both risk and opportunity for market makers.
- spread_bps: Bid-ask spread quoted by market maker (basis points), determining gross profit per round-trip trade and representing compensation for providing liquidity.
- inventory_limit: Maximum inventory position (shares), a risk control preventing excessive exposure to directional price movements.
- fill_probability: Probability of quote being filled each period (0 to 1), representing trading frequency and interaction with order flow.
- adverse_selection_bps: Loss per fill due to informed traders (basis points), representing the cost of providing liquidity to better-informed counterparties who trade when prices are about to move.


Order Anticipation and Signal Detection
As a sophisticated market maker, you analyze incoming order flow to detect patterns that predict future price movements. This involves:
Order book imbalance: When there are substantially more bids than asks (or vice versa), prices tend to move in the direction of the imbalance.
Trade flow toxicity: Large, aggressive orders often precede continued price movement in the same direction. Detecting toxic flow allows you to widen spreads or reduce exposure.
Cross-market signals: Price movements in related instruments (futures, options, correlated stocks) can predict movements in the target instrument.
def calculate_order_imbalance_signal(
bid_sizes: np.ndarray, ask_sizes: np.ndarray, n_levels: int = 5
):
"""
Calculate order book imbalance as a predictive signal.
The imbalance is calculated as:
$$
\text{Imbalance} = \frac{\sum_{i=1}^{n} w_i B_i - \sum_{i=1}^{n} w_i A_i}{\sum_{i=1}^{n} w_i B_i + \sum_{i=1}^{n} w_i A_i}
$$
where:
- $B_i$: bid size at level $i$ (number of shares in orders waiting to buy at the $i$-th best bid price)
- $A_i$: ask size at level $i$ (number of shares in orders waiting to sell at the $i$-th best ask price)
- $w_i = 1/i$: weight for level $i$ (dimensionless), giving more influence to levels closer to the current market
- $n$: number of price levels included (typically 5-10 levels)
- Numerator: weighted difference between bid and ask sizes (shares)
- Denominator: total weighted order book size (shares)
The formula constructs a normalized imbalance metric between negative 1 and positive 1:
**Numerator (weighted difference):**
The numerator computes the weighted difference between bid and ask sizes:
$$
\text{Weighted difference} = \sum_{i=1}^{n} w_i B_i - \sum_{i=1}^{n} w_i A_i
$$
This quantity has the following properties:
- Positive when bid sizes exceed ask sizes (more buying pressure)
- Negative when ask sizes exceed bid sizes (more selling pressure)
- Near zero when bid and ask sizes are balanced
**Denominator (total weighted size):**
The denominator sums the total weighted order book size on both sides of the market:
$$
\text{Total weighted size} = \sum_{i=1}^{n} w_i B_i + \sum_{i=1}^{n} w_i A_i
$$
This serves as the normalization factor. By dividing the weighted difference by the total weighted size, we obtain a metric that is comparable across different securities regardless of their typical order sizes. A stock with 100,000 shares in the book produces the same imbalance metric as a stock with 10,000 shares if the relative bid-ask balance is identical.
**Weighting scheme intuition:**
The weighting $w_i = 1/i$ reflects that nearby orders matter more for predicting immediate price movements. Consider why:
- A 1,000 share bid at level 1 (the best bid) is highly informative because it will execute first
- A 1,000 share bid at level 5 is less informative because four other price levels must trade first
- Orders closer to the current price have higher execution probability and thus more predictive power
The $1/i$ weighting implements this intuition:
- Level 1 receives weight 1.0 (maximum influence)
- Level 2 receives weight 0.5 (half the influence)
- Level 3 receives weight 0.33 (one-third the influence)
- And so on, with influence declining as distance from the market increases
**Interpreting the imbalance value:**
- Values near positive 1: strong buying pressure, predicting upward price movement
- Values near negative 1: strong selling pressure, predicting downward price movement
- Values near 0: balanced order flow with no clear directional signal
- Magnitude indicates strength: an imbalance of 0.8 suggests a stronger signal than an imbalance of 0.3
Parameters:
-----------
bid_sizes : Array of bid sizes at each price level
ask_sizes : Array of ask sizes at each price level
n_levels : Number of levels to include in calculation
"""
# Use top n levels with distance weighting
weights = 1 / np.arange(1, n_levels + 1)
weights = weights / weights.sum()
weighted_bid = np.sum(bid_sizes[:n_levels] * weights)
weighted_ask = np.sum(ask_sizes[:n_levels] * weights)
# Imbalance: positive means more bids (bullish), negative means more asks (bearish)
imbalance = (weighted_bid - weighted_ask) / (weighted_bid + weighted_ask)
return imbalance
# Example order book
np.random.seed(42)
bid_sizes = np.array([500, 800, 1200, 600, 900]) # More aggressive buying
ask_sizes = np.array([400, 500, 700, 800, 600])
imbalance = calculate_order_imbalance_signal(bid_sizes, ask_sizes)Order Book Imbalance Analysis ======================================== Order Book (top 5 levels): Level Bid Size Ask Size -------------------------------- 1 500 400 2 800 500 3 1200 700 4 600 800 5 900 600 Weighted Imbalance: 0.1506 Interpretation: Bullish (more bid pressure)
The weighted imbalance indicates directional pressure. The weighting gives more influence to the best prices (level 1 receives full weight, level 2 receives 50%, etc.), and this imbalance suggests short-term price pressure. Market makers would use this signal to adjust their quotes to lean against the expected move.
