microstructural_features
Concept Overview
Section titled “Concept Overview”Features computed from bar-level order flow rather than from price alone, in three families: effective-spread proxies (Roll, Corwin-Schultz), price-impact coefficients (Kyle’s lambda, Amihud, Hasbrouck) and flow-toxicity or entropy measures (VPIN, plus Shannon, Lempel-Ziv and plug-in entropy over encoded tick signs). Together they estimate what OHLC bars omit: how expensive the instrument is to trade, and how likely it is that the counterparty knows something you do not.
When to Use
Section titled “When to Use”Use them as features when the edge or its cost depends on liquidity — execution models, regime detection, and any signal that decays with trade size. VPIN in particular is an early-warning indicator for flow toxicity ahead of liquidity events. Normalise within venue and time bucket before comparing across assets, since these are strongly regime-dependent, and freeze the symbol encoding used for entropy features or the values will not be comparable between training and production.
Mathematical Foundations
Section titled “Mathematical Foundations”Kyle / Amihud / Hasbrouck Impact Families
Section titled “Kyle / Amihud / Hasbrouck Impact Families”
Spread and Volatility Proxies
Section titled “Spread and Volatility Proxies”
Flow Toxicity and Entropy
Section titled “Flow Toxicity and Entropy”
where and are buy- and sell-initiated volume in bar (get_bvc_buy_volume will estimate the split when it is not observed), the current bar’s total volume, and the rolling window. The normaliser sits outside the sum because bars are not equal-volume: get_vpin averages the imbalance over the window and then scales by the latest bar. The equal-volume-bucket form used by streaming-hpc divides each term by the same constant bucket size instead; the two agree when bars carry equal volume. is the entropy of the tick-sign message, with the empirical frequency of symbol .
Usage Examples
Section titled “Usage Examples”End-to-end: Build Core Liquidity Feature Panel
Section titled “End-to-end: Build Core Liquidity Feature Panel”use openquant::microstructural_features::{ get_roll_measure, get_corwin_schultz_estimator, get_bar_based_kyle_lambda, get_bar_based_amihud_lambda, get_vpin,};
// 1) Inputs from bar constructionlet close = vec![100.0, 100.2, 100.1, 100.3, 100.25, 100.4];let high = vec![100.1, 100.25, 100.2, 100.35, 100.3, 100.45];let low = vec![99.9, 100.0, 99.95, 100.1, 100.05, 100.2];let volume = vec![1000.0, 1200.0, 900.0, 1100.0, 1300.0, 1250.0];let dollar_volume: Vec<f64> = close.iter().zip(volume.iter()).map(|(p, v)| p * v).collect();let buy_volume = vec![600.0, 700.0, 480.0, 650.0, 800.0, 760.0];
// 2) Liquidity and spread proxieslet roll = get_roll_measure(&close, 3);let cs_spread = get_corwin_schultz_estimator(&high, &low, 3);let kyle = get_bar_based_kyle_lambda(&close, &volume, 3);let amihud = get_bar_based_amihud_lambda(&close, &dollar_volume, 3);let vpin = get_vpin(&volume, &buy_volume, 3);
// 3) Feature panel is ready for regime model / execution modelassert_eq!(roll.len(), close.len());assert_eq!(vpin.len(), close.len());From Encoded Tick Signs to Entropy Features
Section titled “From Encoded Tick Signs to Entropy Features”use openquant::microstructural_features::{ encode_tick_rule_array, get_shannon_entropy, get_lempel_ziv_entropy, get_plug_in_entropy,};
let tick_rule = vec![1, 1, -1, -1, 1, -1, 1, 1, 1, -1];let msg = encode_tick_rule_array(&tick_rule)?;
let h_shannon = get_shannon_entropy(&msg);let h_lz = get_lempel_ziv_entropy(&msg);let h_plugin = get_plug_in_entropy(&msg, 2);
assert!(h_shannon.is_finite());assert!(h_lz.is_finite());assert!(h_plugin.is_finite());API Reference
Section titled “API Reference”Python API
Section titled “Python API”microstructural.get_roll_measuremicrostructural.get_roll_impactmicrostructural.get_corwin_schultz_estimatormicrostructural.get_bekker_parkinson_volmicrostructural.get_bar_based_kyle_lambdamicrostructural.get_bar_based_amihud_lambdamicrostructural.get_bar_based_hasbrouck_lambdamicrostructural.get_trades_based_kyle_lambdamicrostructural.get_trades_based_amihud_lambdamicrostructural.get_trades_based_hasbrouck_lambdamicrostructural.vwapmicrostructural.get_avg_tick_sizemicrostructural.get_vpinmicrostructural.get_bvc_buy_volumemicrostructural.encode_tick_rule_arraymicrostructural.quantile_mappingmicrostructural.sigma_mappingmicrostructural.encode_arraymicrostructural.get_shannon_entropymicrostructural.get_lempel_ziv_entropymicrostructural.get_plug_in_entropymicrostructural.get_konto_entropy
Rust API
Section titled “Rust API”get_roll_measureget_corwin_schultz_estimatorget_bar_based_kyle_lambdaget_vpinMicrostructuralFeaturesGenerator
Risk Notes and Caveats
Section titled “Risk Notes and Caveats”- Microstructure signals are highly regime-dependent; normalize and standardize within venue/time bucket before cross-asset comparison.
- Use shared bar definitions between training and live pipelines, otherwise feature drift is structural.
- Entropy features are sensitive to encoding; freeze symbol maps in production.