Skip to main content

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.
Defaults:
  • tick_size: 0.25 (ES futures)
  • qty_scale: 100.0 (normalization factor)

with_params

Creates a feature extractor with custom parameters.
Parameters:
  • tick_size: Minimum price increment for the instrument
  • qty_scale: Quantity normalization factor (divides quantities)
Example:

LobFeatures

Extracted features from an order book snapshot.

Feature Descriptions

Price Features

Imbalance Features

Formula: (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 Extraction Methods

extract

Extracts all features from an order book.
Parameters:
  • book: Order book to extract features from
Returns: Complete feature set Example:

microprice

Calculates volume-weighted microprice.
Formula: (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.
Parameters:
  • levels: Number of levels to include (typically 5-10)
Weighting: Each level is weighted by 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.
Parameters:
  • levels: Number of levels to include
Returns: Imbalance between -1 and +1 Research: Book imbalance is predictive of short-term price movement

order_flow_imbalance

Calculates Order Flow Imbalance (OFI) between two consecutive book states.
Parameters:
  • prev_book: Previous book state
  • curr_book: Current book state
Returns: Normalized OFI value Details:
  • Tracks changes in bid/ask prices and quantities
  • Positive OFI: More aggressive buying
  • Negative OFI: More aggressive selling
Research: OFI is a strong predictor of price changes (Cont et al. 2014)

to_array

Converts features to a flat array for ML model input.
Returns: 44-element feature vector Layout:
  • [0-3]: microprice, weighted_mid, spread, imbalance_l1
Example:

VpinCalculator

Calculates VPIN (Volume-Synchronized Probability of Informed Trading).

Constructor

new

Creates a new VPIN calculator.
Parameters:
  • 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.
Parameters:
  • quantity: Trade size
  • is_buy: true for buy-side trade, false for sell-side
Details:
  • Accumulates volume until bucket is complete
  • Maintains rolling window of buckets

calculate

Calculates current VPIN value.
Returns: VPIN value between 0 and 1 Formula: 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
Research: VPIN is used to measure order flow toxicity and predict flash crashes

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.
Formula: (buy_vol - sell_vol) / (buy_vol + sell_vol)

reset

Resets all counters.

Example

Complete Example: Feature Pipeline

See Also