pub trait DualModeFeature<const N: usize = 32, const T: usize = 16>: Send + Sync {
// Required methods
fn calculate_incremental(
&mut self,
update: &MarketUpdate<N, T>,
) -> FeatureValue;
fn calculate_batch(&self, data: &[MarketData]) -> Vec<FeatureValue>;
fn name(&self) -> &'static str;
fn reset(&mut self);
}Expand description
Trait for features that support both real-time and batch calculation
This trait enables feature calculations to work in both real-time streaming mode and batch processing mode, ensuring identical results between modes.
§Type Parameters
-
N- The capacity for order book levels (default: 32)- Controls the maximum number of bid/ask price and quantity pairs that can be stored inline
- Uses
SmallVecto avoid heap allocation for typical order book depths - Default value: 32 levels, suitable for most market making strategies
- Common values:
5-10: Top-of-book strategies (L1 data only)16-32: Standard market making and arbitrage strategies64-128: Deep market making and full depth analysis
- Performance implications: Values up to N are stack-allocated; exceeding this causes heap allocation
-
T- The capacity for trades buffer (default: 16)- Controls the maximum number of trade updates that can be stored inline per market update
- Uses
SmallVecto avoid heap allocation for typical trade bursts - Default value: 16 trades, suitable for most market conditions
- Common values:
8-16: Normal market conditions and low-frequency strategies32-64: High-volume periods and burst trade handling128+: Extremely liquid assets or high-frequency aggregation
- Performance implications: Values up to T are stack-allocated; exceeding this causes heap allocation
§Buffer Sizing Guidelines
Choose buffer sizes based on your specific use case and market characteristics:
§Order Book Levels (N)
- Level 1 only: Use
N = 5for top-of-book strategies - Standard depth: Use
N = 16-32for typical market making - Deep analysis: Use
N = 64-128for full order book strategies - Monitor usage: Track actual order book depths to optimize N
§Trade Buffer (T)
- Low frequency: Use
T = 8-16for slower strategies - Standard flow: Use
T = 16-32for normal market conditions - High frequency: Use
T = 64-128for burst-heavy markets - Monitor usage: Track trade volumes per update to optimize T
§Performance Considerations
- Stack allocation: Values within capacity are stored inline (fastest)
- Heap allocation: Exceeding capacity triggers heap allocation (slower)
- Memory locality: Smaller buffers improve cache performance
- Compilation: Different const values create distinct types at compile time
- SIMD optimization: Powers-of-2 sizes may enable better vectorization
§Usage Examples
// Default configuration (32 levels, 16 trades) - suitable for most strategies
impl DualModeFeature for MyFeature {
fn calculate_incremental(&mut self, update: &MarketUpdate) -> FeatureValue {
// Implementation using default 32 levels, 16 trades
}
}
// Top-of-book strategy - minimal memory footprint
impl DualModeFeature<5, 8> for TopOfBookFeature {
fn calculate_incremental(&mut self, update: &MarketUpdate<5, 8>) -> FeatureValue {
// Implementation optimized for L1 data only
}
}
// Deep market making - full order book analysis
impl DualModeFeature<64, 32> for DeepMMFeature {
fn calculate_incremental(&mut self, update: &MarketUpdate<64, 32>) -> FeatureValue {
// Implementation for deep order book strategies
}
}
// High-frequency burst handling
impl DualModeFeature<32, 128> for BurstFeature {
fn calculate_incremental(&mut self, update: &MarketUpdate<32, 128>) -> FeatureValue {
// Implementation for handling trade bursts
}
}§Type Safety
The const generic parameters provide compile-time type safety:
- Different buffer sizes create distinct types
- Prevents accidental mixing of incompatible buffer sizes
- Enables compiler optimizations based on known buffer sizes
- Zero-cost abstractions - no runtime overhead for buffer size choices
Required Methods§
Sourcefn calculate_incremental(&mut self, update: &MarketUpdate<N, T>) -> FeatureValue
fn calculate_incremental(&mut self, update: &MarketUpdate<N, T>) -> FeatureValue
Calculate feature incrementally for real-time trading
Sourcefn calculate_batch(&self, data: &[MarketData]) -> Vec<FeatureValue>
fn calculate_batch(&self, data: &[MarketData]) -> Vec<FeatureValue>
Calculate feature in batch mode for backtesting