nano-lob crate provides comprehensive feature extraction from order book snapshots for machine learning models. These features capture market microstructure signals used to predict short-term price movements.
LobFeatureExtractor
The core feature extraction engine extracts 44+ features from the order book state.Initialization
nano-lob/src/features.rs:58-74
Feature Structure
nano-lob/src/features.rs:10-40
Core Features
Microprice
The microprice is a volume-weighted mid price that accounts for the liquidity at the best bid and ask:nano-lob/src/features.rs:96-102, 158-174
Weighted Mid Price
Weights multiple price levels by inverse distance and quantity:nano-lob/src/features.rs:176-206
Book Imbalance
Measures the imbalance between bid and ask liquidity:nano-lob/src/features.rs:104-106, 208-220
Advanced Features
Order Flow Imbalance (OFI)
OFI tracks changes in order flow between consecutive book states:-
Bid side:
- If bid price improved (higher):
+new_bid_qty - If bid price worsened (lower):
-old_bid_qty - If same price:
delta_qty
- If bid price improved (higher):
-
Ask side:
- If ask price improved (lower):
-new_ask_qty - If ask price worsened (higher):
+old_ask_qty - If same price:
-delta_qty
- If ask price improved (lower):
nano-lob/src/features.rs:222-261
VPIN (Volume-Synchronized Probability of Informed Trading)
VPIN estimates the probability of informed trading by analyzing volume buckets:nano-lob/src/features.rs:298-388
Trade Flow Tracking
Track cumulative trade flow over time:nano-lob/src/features.rs:390-451
ML Feature Vector
Convert all features to a flat array for ML model input:nano-lob/src/features.rs:264-295
Usage Example
Real-Time Feature Extraction
Temporal Features with History
Multi-Instrument Tracking
Performance Characteristics
Extraction Latency
- Full feature extraction: ~500-800ns
- Microprice only: ~50-100ns
- Book imbalance: ~100-200ns
- OFI calculation: ~200-400ns
- VPIN update: ~50-100ns
Memory Footprint
- LobFeatures: 440 bytes (44 × f64 + overhead)
- VpinCalculator: ~1KB (depends on num_buckets)
- TradeFlowTracker: 32 bytes
Feature Interpretation
Microprice vs Mid Price
- Mid price: Simple average, ignores liquidity
- Microprice: Weighted by BBO liquidity, better fair value estimate
- Use microprice for:
- Order placement decisions
- Fair value estimation
- Spread crossing decisions
Imbalance Signals
- Positive imbalance (>0.3): More bid liquidity → upward pressure
- Negative imbalance (<-0.3): More ask liquidity → downward pressure
- Neutral (-0.2 to 0.2): Balanced book
OFI Interpretation
- Positive OFI: Net aggressive buying → potential price increase
- Negative OFI: Net aggressive selling → potential price decrease
- OFI is a leading indicator (predicts next price move)
VPIN Thresholds
- VPIN < 0.3: Low informed trading, safer to provide liquidity
- VPIN 0.3-0.7: Moderate informed trading
- VPIN > 0.7: High informed trading, higher adverse selection risk