Overview
The feature extraction module provides tools for extracting numerical features from order book states for use in machine learning models, technical analysis, and market microstructure research. Source:nano-lob/src/features.rs
LobFeatureExtractor
Extracts comprehensive features from order book snapshots.Constructor
new
Creates a feature extractor with default parameters.
tick_size: 0.25 (ES futures)qty_scale: 100.0 (normalization factor)
with_params
Creates a feature extractor with custom parameters.
tick_size: Minimum price increment for the instrumentqty_scale: Quantity normalization factor (divides quantities)
LobFeatures
Extracted features from an order book snapshot.Feature Descriptions
Price Features
| Feature | Description |
|---|---|
microprice | Volume-weighted mid price using BBO quantities |
weighted_mid | Depth-weighted mid price across multiple levels |
mid_price | Simple mid: (best_bid + best_ask) / 2 |
best_bid | Best bid price |
best_ask | Best ask price |
spread | Bid-ask spread in ticks |
Imbalance Features
| Feature | Description | Range |
|---|---|---|
imbalance_l1 | Level 1 book imbalance | -1 to +1 |
imbalance_total | Total book imbalance across all levels | -1 to +1 |
(bid_qty - ask_qty) / (bid_qty + ask_qty)
Interpretation:
- +1: Only bids (strong buying pressure)
- 0: Balanced
- -1: Only asks (strong selling pressure)
Depth Features
| Feature | Description |
|---|---|
bid_depth | Total bid quantity (normalized) |
ask_depth | Total ask quantity (normalized) |
bid_levels[i] | Quantity at bid level i (normalized) |
ask_levels[i] | Quantity at ask level i (normalized) |
bid_cumulative[i] | Cumulative bid quantity through level i |
ask_cumulative[i] | Cumulative ask quantity through level i |
Feature Extraction Methods
extract
Extracts all features from an order book.
book: Order book to extract features from
microprice
Calculates volume-weighted microprice.
(bid * ask_qty + ask * bid_qty) / (bid_qty + ask_qty)
Interpretation: The microprice is a better estimate of “fair value” than simple mid-price when there’s a quantity imbalance at the BBO.
Example:
weighted_mid
Calculates depth-weighted mid price using multiple levels.
levels: Number of levels to include (typically 5-10)
1 / (level_index + 1) and its quantity
Use case: Better fair value estimate using deeper book information
book_imbalance
Calculates order book imbalance at a given depth.
levels: Number of levels to include
order_flow_imbalance
Calculates Order Flow Imbalance (OFI) between two consecutive book states.
prev_book: Previous book statecurr_book: Current book state
- Tracks changes in bid/ask prices and quantities
- Positive OFI: More aggressive buying
- Negative OFI: More aggressive selling
to_array
Converts features to a flat array for ML model input.
- [0-3]: microprice, weighted_mid, spread, imbalance_l1
VpinCalculator
Calculates VPIN (Volume-Synchronized Probability of Informed Trading).Constructor
new
Creates a new VPIN calculator.
bucket_size: Volume per bucket (e.g., 5000 contracts)num_buckets: Rolling window size (e.g., 50 buckets)
Methods
add_trade
Adds a trade to the calculator.
quantity: Trade sizeis_buy:truefor buy-side trade,falsefor sell-side
- Accumulates volume until bucket is complete
- Maintains rolling window of buckets
calculate
Calculates current VPIN value.
sum(|buy_vol - sell_vol|) / sum(total_vol) across all buckets
Interpretation:
- 0: Balanced order flow
- 1: Completely one-sided flow (max toxicity)
-
0.3: High toxicity warning
Example
TradeFlowTracker
Tracks cumulative trade flow statistics.Constructor
new
Creates a new trade flow tracker.
Methods
record_trade
Records a trade.
net_flow
Returns net flow (buy volume - sell volume).
flow_imbalance
Returns flow imbalance between -1 and +1.
(buy_vol - sell_vol) / (buy_vol + sell_vol)
reset
Resets all counters.
Example
Complete Example: Feature Pipeline
See Also
- OrderBook - Order book data structure
- Market Data Feed - Parsing market data