Move 17 library crates into crates/, CLI binary into bin/fxt, consolidate 10 test crates into testing/, split config crate from deployment config files. Root directory reduced from 38+ to ~17 directories. All Cargo.toml paths and build.rs proto refs updated. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
94 lines
3.5 KiB
Rust
94 lines
3.5 KiB
Rust
//! # Advanced Market Microstructure Models for HFT Alpha Generation
|
|
//!
|
|
//! Implements state-of-the-art machine learning models for market microstructure analysis
|
|
//! targeting <25μs inference latency. All models are optimized for real-time trading.
|
|
//!
|
|
//! ## Model Portfolio
|
|
//!
|
|
//! 1. **Order Flow Imbalance Prediction** - Predicts OFI using LSTM-Transformer hybrid
|
|
//! 2. **Liquidity Provision Optimization** - Optimal spread and size determination
|
|
//! 3. **Spread Prediction Models** - Real-time bid-ask spread forecasting
|
|
//! 4. **Market Impact Estimation** - Dynamic impact modeling with neural networks
|
|
//! 5. **Adverse Selection Detection** - Real-time toxic flow identification
|
|
//! 6. **Price Discovery Models** - Information incorporation efficiency analysis
|
|
//! 7. **Hidden Liquidity Detection** - Dark pool and iceberg order identification
|
|
|
|
use std::collections::{HashMap, VecDeque};
|
|
use std::sync::atomic::{AtomicU64, Ordering};
|
|
use std::time::{Duration, Instant};
|
|
|
|
use candle_core::Device;
|
|
use candle_core::{Tensor, Device, DType, Result as CandleResult};
|
|
use candle_nn::{Linear, LayerNorm, Dropout, Module, VarBuilder};
|
|
use ndarray::{Array1, Array2, Array3, ArrayView1, ArrayView2, s!};
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use crate::{MLAppResult, InferenceResult, ModelMetadata};
|
|
use super::*;
|
|
use super::{
|
|
|
|
|
|
#[test]
|
|
fn test_feature_extractor_creation() {
|
|
let extractor = MicrostructureFeatureExtractor::new(64, OFI_FEATURE_DIM);
|
|
assert_eq!(extractor.window_size, 64);
|
|
assert_eq!(extractor.feature_dim, OFI_FEATURE_DIM);
|
|
}
|
|
|
|
#[test]
|
|
fn test_feature_extraction() {
|
|
let mut extractor = MicrostructureFeatureExtractor::new(10, 16);
|
|
|
|
let update = MarketDataUpdate {
|
|
timestamp: 1000000000,
|
|
symbol: "AAPL".to_owned(),
|
|
price: 150_00000000, // $150.00 in scaled format
|
|
volume: 1000,
|
|
bid: 149_95000000, // $149.95
|
|
ask: 150_05000000, // $150.05
|
|
bid_size: 500,
|
|
ask_size: 600,
|
|
direction: Some(TradeDirection::Buy),
|
|
};
|
|
|
|
let features = extractor.extract_features(&update)?;
|
|
assert_eq!(features.len(), 16);
|
|
|
|
// Test feature values are reasonable
|
|
assert!(features[0] > 0.0); // Price feature
|
|
assert!(features[3] > 0.0); // Relative spread
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_liquidity_optimization_structure() {
|
|
let optimization = LiquidityOptimization {
|
|
optimal_bid_spread_bps: 10.0,
|
|
optimal_ask_spread_bps: 10.0,
|
|
optimal_bid_size: 1000.0,
|
|
optimal_ask_size: 1000.0,
|
|
expected_profitability: 0.001,
|
|
risk_score: 0.2,
|
|
confidence: 0.8,
|
|
inference_time_us: 20,
|
|
};
|
|
|
|
assert_eq!(optimization.optimal_bid_spread_bps, 10.0);
|
|
assert!(optimization.inference_time_us <= TARGET_INFERENCE_LATENCY_US);
|
|
}
|
|
|
|
#[test]
|
|
fn test_spread_prediction_structure() {
|
|
let prediction = SpreadPrediction {
|
|
current_spread_bps: 8.5,
|
|
predicted_spread_bps: 9.2,
|
|
spread_change_pct: 8.2,
|
|
prediction_horizon_seconds: 30,
|
|
spread_volatility: 0.15,
|
|
confidence: 0.75,
|
|
inference_time_us: 18,
|
|
};
|
|
|
|
assert!(prediction.predicted_spread_bps > prediction.current_spread_bps);
|
|
assert!(prediction.confidence > 0.0 && prediction.confidence < 1.0);
|
|
}
|
|
} |