work on new engine

This commit is contained in:
Boki 2025-07-04 11:24:27 -04:00
parent 44476da13f
commit a1e5a21847
126 changed files with 3425 additions and 6695 deletions

View file

@ -0,0 +1,353 @@
use crate::{Side, MarketMicrostructure, PriceLevel};
use chrono::{DateTime, Utc, Timelike};
#[derive(Debug, Clone)]
pub struct MarketImpactEstimate {
pub temporary_impact: f64,
pub permanent_impact: f64,
pub total_impact: f64,
pub expected_cost: f64,
pub impact_decay_ms: i64,
}
#[derive(Debug, Clone, Copy)]
pub enum ImpactModelType {
Linear,
SquareRoot,
PowerLaw { exponent: f64 },
AlmgrenChriss,
IStarModel,
}
pub struct MarketImpactModel {
model_type: ImpactModelType,
// Model parameters
temporary_impact_coef: f64,
permanent_impact_coef: f64,
spread_impact_weight: f64,
volatility_adjustment: bool,
}
impl MarketImpactModel {
pub fn new(model_type: ImpactModelType) -> Self {
match model_type {
ImpactModelType::Linear => Self {
model_type,
temporary_impact_coef: 0.1,
permanent_impact_coef: 0.05,
spread_impact_weight: 0.5,
volatility_adjustment: true,
},
ImpactModelType::SquareRoot => Self {
model_type,
temporary_impact_coef: 0.142, // Empirical from literature
permanent_impact_coef: 0.0625,
spread_impact_weight: 0.5,
volatility_adjustment: true,
},
ImpactModelType::AlmgrenChriss => Self {
model_type,
temporary_impact_coef: 0.314,
permanent_impact_coef: 0.142,
spread_impact_weight: 0.7,
volatility_adjustment: true,
},
ImpactModelType::PowerLaw { .. } => Self {
model_type,
temporary_impact_coef: 0.2,
permanent_impact_coef: 0.1,
spread_impact_weight: 0.5,
volatility_adjustment: true,
},
ImpactModelType::IStarModel => Self {
model_type,
temporary_impact_coef: 1.0,
permanent_impact_coef: 0.5,
spread_impact_weight: 0.8,
volatility_adjustment: true,
},
}
}
pub fn estimate_impact(
&self,
order_size: f64,
side: Side,
microstructure: &MarketMicrostructure,
orderbook: &[PriceLevel],
current_time: DateTime<Utc>,
) -> MarketImpactEstimate {
// Calculate participation rate
let intraday_volume = self.get_expected_volume(microstructure, current_time);
let participation_rate = order_size / intraday_volume.max(1.0);
// Calculate spread in basis points
let spread_bps = microstructure.avg_spread_bps;
// Calculate volatility adjustment
let vol_adjustment = if self.volatility_adjustment {
(microstructure.volatility / 0.02).sqrt() // Normalize to 2% daily vol
} else {
1.0
};
// Calculate temporary impact based on model type
let temp_impact_bps = match self.model_type {
ImpactModelType::Linear => {
self.temporary_impact_coef * participation_rate * 10000.0
},
ImpactModelType::SquareRoot => {
self.temporary_impact_coef * participation_rate.sqrt() * 10000.0
},
ImpactModelType::PowerLaw { exponent } => {
self.temporary_impact_coef * participation_rate.powf(exponent) * 10000.0
},
ImpactModelType::AlmgrenChriss => {
self.calculate_almgren_chriss_impact(
participation_rate,
spread_bps,
microstructure.volatility,
order_size,
microstructure.avg_trade_size,
)
},
ImpactModelType::IStarModel => {
self.calculate_istar_impact(
order_size,
microstructure,
orderbook,
side,
)
},
};
// Calculate permanent impact (usually smaller)
let perm_impact_bps = self.permanent_impact_coef * participation_rate.sqrt() * 10000.0;
// Add spread cost
let spread_cost_bps = spread_bps * self.spread_impact_weight;
// Apply volatility adjustment
let adjusted_temp_impact = temp_impact_bps * vol_adjustment;
let adjusted_perm_impact = perm_impact_bps * vol_adjustment;
// Calculate total impact
let total_impact_bps = adjusted_temp_impact + adjusted_perm_impact + spread_cost_bps;
// Calculate impact decay time (how long temporary impact lasts)
let impact_decay_ms = self.calculate_impact_decay_time(
order_size,
microstructure.daily_volume,
microstructure.avg_trade_size,
);
// Calculate expected cost
let mid_price = if !orderbook.is_empty() {
orderbook[0].price
} else {
100.0 // Default if no orderbook
};
let direction_multiplier = match side {
Side::Buy => 1.0,
Side::Sell => -1.0,
};
let expected_cost = mid_price * order_size * total_impact_bps / 10000.0 * direction_multiplier;
MarketImpactEstimate {
temporary_impact: adjusted_temp_impact,
permanent_impact: adjusted_perm_impact,
total_impact: total_impact_bps,
expected_cost: expected_cost.abs(),
impact_decay_ms,
}
}
fn calculate_almgren_chriss_impact(
&self,
participation_rate: f64,
spread_bps: f64,
volatility: f64,
order_size: f64,
avg_trade_size: f64,
) -> f64 {
// Almgren-Chriss model parameters
let eta = self.temporary_impact_coef; // Temporary impact coefficient
let gamma = self.permanent_impact_coef; // Permanent impact coefficient
let trading_rate = order_size / avg_trade_size;
// Temporary impact: eta * (v/V)^alpha * sigma
let temp_component = eta * participation_rate.sqrt() * volatility * 10000.0;
// Permanent impact: gamma * (X/V)
let perm_component = gamma * trading_rate * 10000.0;
// Add half spread
let spread_component = spread_bps * 0.5;
temp_component + perm_component + spread_component
}
fn calculate_istar_impact(
&self,
order_size: f64,
microstructure: &MarketMicrostructure,
orderbook: &[PriceLevel],
_side: Side,
) -> f64 {
// I* model - uses order book shape
if orderbook.is_empty() {
return self.temporary_impact_coef * 100.0; // Fallback
}
// Calculate order book imbalance
let mut cumulative_size = 0.0;
let mut impact_bps = 0.0;
// Walk through the book until we've "consumed" our order
for (_i, level) in orderbook.iter().enumerate() {
cumulative_size += level.size;
if cumulative_size >= order_size {
// Calculate average price impact to this level
let ref_price = orderbook[0].price;
let exec_price = level.price;
impact_bps = ((exec_price - ref_price).abs() / ref_price) * 10000.0;
break;
}
}
// Add participation rate impact
let participation_impact = self.temporary_impact_coef *
(order_size / microstructure.daily_volume).sqrt() * 10000.0;
impact_bps + participation_impact
}
fn get_expected_volume(
&self,
microstructure: &MarketMicrostructure,
current_time: DateTime<Utc>,
) -> f64 {
// Use intraday volume profile if available
if microstructure.intraday_volume_profile.len() == 24 {
let hour = current_time.hour() as usize;
let hour_pct = microstructure.intraday_volume_profile[hour];
microstructure.daily_volume * hour_pct
} else {
// Simple assumption: 1/6.5 of daily volume per hour (6.5 hour trading day)
microstructure.daily_volume / 6.5
}
}
fn calculate_impact_decay_time(
&self,
order_size: f64,
daily_volume: f64,
avg_trade_size: f64,
) -> i64 {
// Empirical formula for impact decay
// Larger orders relative to volume decay slower
let volume_ratio = order_size / daily_volume;
let trade_ratio = order_size / avg_trade_size;
// Base decay time in milliseconds
let base_decay_ms = 60_000; // 1 minute base
// Adjust based on order characteristics
let decay_multiplier = 1.0 + volume_ratio * 10.0 + trade_ratio.ln().max(0.0);
(base_decay_ms as f64 * decay_multiplier) as i64
}
pub fn calculate_optimal_execution_schedule(
&self,
total_size: f64,
time_horizon_minutes: f64,
microstructure: &MarketMicrostructure,
risk_aversion: f64,
) -> Vec<(f64, f64)> {
// Almgren-Chriss optimal execution trajectory
let n_slices = (time_horizon_minutes / 5.0).ceil() as usize; // 5-minute buckets
let tau = time_horizon_minutes / n_slices as f64;
let mut schedule = Vec::with_capacity(n_slices);
// Parameters
let volatility = microstructure.volatility;
let _daily_volume = microstructure.daily_volume;
let eta = self.temporary_impact_coef;
let _gamma = self.permanent_impact_coef;
let lambda = risk_aversion;
// Calculate optimal trading rate
let kappa = lambda * volatility.powi(2) / eta;
let alpha = (kappa / tau).sqrt();
for i in 0..n_slices {
let t = i as f64 * tau;
let t_next = (i + 1) as f64 * tau;
// Optimal trajectory: x(t) = X * sinh(alpha * (T - t)) / sinh(alpha * T)
let remaining_start = total_size * (alpha * (time_horizon_minutes - t)).sinh()
/ (alpha * time_horizon_minutes).sinh();
let remaining_end = total_size * (alpha * (time_horizon_minutes - t_next)).sinh()
/ (alpha * time_horizon_minutes).sinh();
let slice_size = remaining_start - remaining_end;
let slice_time = t + tau / 2.0; // Midpoint
schedule.push((slice_time, slice_size));
}
schedule
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_market_impact_models() {
let microstructure = MarketMicrostructure {
symbol: "TEST".to_string(),
avg_spread_bps: 2.0,
daily_volume: 10_000_000.0,
avg_trade_size: 100.0,
volatility: 0.02,
tick_size: 0.01,
lot_size: 1.0,
intraday_volume_profile: vec![0.04; 24], // Flat profile
};
let orderbook = vec![
PriceLevel { price: 100.0, size: 1000.0, order_count: Some(10) },
PriceLevel { price: 100.01, size: 2000.0, order_count: Some(15) },
];
let models = vec![
ImpactModelType::Linear,
ImpactModelType::SquareRoot,
ImpactModelType::AlmgrenChriss,
];
for model_type in models {
let model = MarketImpactModel::new(model_type);
let impact = model.estimate_impact(
1000.0,
Side::Buy,
&microstructure,
&orderbook,
Utc::now(),
);
assert!(impact.total_impact > 0.0);
assert!(impact.temporary_impact >= 0.0);
assert!(impact.permanent_impact >= 0.0);
assert!(impact.expected_cost > 0.0);
assert!(impact.impact_decay_ms > 0);
}
}
}

View file

@ -0,0 +1,5 @@
pub mod market_impact;
pub mod transaction_costs;
pub use market_impact::{MarketImpactModel, ImpactModelType, MarketImpactEstimate};
pub use transaction_costs::{TransactionCostModel, CostComponents};

View file

@ -0,0 +1,355 @@
use crate::{Side, Order, Fill, MarketMicrostructure};
use chrono::{DateTime, Utc};
#[derive(Debug, Clone)]
pub struct CostComponents {
pub spread_cost: f64,
pub market_impact: f64,
pub commission: f64,
pub slippage: f64,
pub opportunity_cost: f64,
pub timing_cost: f64,
pub total_cost: f64,
pub cost_bps: f64,
}
#[derive(Debug, Clone)]
pub struct TransactionCostAnalysis {
pub order_id: String,
pub symbol: String,
pub side: Side,
pub intended_size: f64,
pub filled_size: f64,
pub avg_fill_price: f64,
pub arrival_price: f64,
pub benchmark_price: f64,
pub cost_components: CostComponents,
pub implementation_shortfall: f64,
pub duration_ms: i64,
}
pub struct TransactionCostModel {
commission_rate_bps: f64,
min_commission: f64,
exchange_fees_bps: f64,
regulatory_fees_bps: f64,
benchmark_type: BenchmarkType,
}
#[derive(Debug, Clone, Copy)]
pub enum BenchmarkType {
ArrivalPrice, // Price when order was placed
VWAP, // Volume-weighted average price
TWAP, // Time-weighted average price
Close, // Closing price
MidpointAtArrival, // Mid price at order arrival
}
impl TransactionCostModel {
pub fn new(commission_rate_bps: f64) -> Self {
Self {
commission_rate_bps,
min_commission: 1.0,
exchange_fees_bps: 0.3, // Typical exchange fees
regulatory_fees_bps: 0.1, // SEC fees etc
benchmark_type: BenchmarkType::ArrivalPrice,
}
}
pub fn with_benchmark_type(mut self, benchmark_type: BenchmarkType) -> Self {
self.benchmark_type = benchmark_type;
self
}
pub fn analyze_execution(
&self,
order: &Order,
fills: &[Fill],
arrival_price: f64,
benchmark_prices: &BenchmarkPrices,
microstructure: &MarketMicrostructure,
order_start_time: DateTime<Utc>,
order_end_time: DateTime<Utc>,
) -> TransactionCostAnalysis {
// Calculate filled size and average price
let filled_size = fills.iter().map(|f| f.quantity).sum::<f64>();
let total_value = fills.iter().map(|f| f.price * f.quantity).sum::<f64>();
let avg_fill_price = if filled_size > 0.0 {
total_value / filled_size
} else {
arrival_price
};
// Get benchmark price based on type
let benchmark_price = match self.benchmark_type {
BenchmarkType::ArrivalPrice => arrival_price,
BenchmarkType::VWAP => benchmark_prices.vwap,
BenchmarkType::TWAP => benchmark_prices.twap,
BenchmarkType::Close => benchmark_prices.close,
BenchmarkType::MidpointAtArrival => benchmark_prices.midpoint_at_arrival,
};
// Calculate various cost components
let cost_components = self.calculate_cost_components(
order,
fills,
avg_fill_price,
arrival_price,
benchmark_price,
microstructure,
);
// Calculate implementation shortfall
let side_multiplier = match order.side {
Side::Buy => 1.0,
Side::Sell => -1.0,
};
let implementation_shortfall = side_multiplier * filled_size *
(avg_fill_price - arrival_price) +
side_multiplier * (order.quantity - filled_size) *
(benchmark_price - arrival_price);
// Calculate duration
let duration_ms = (order_end_time - order_start_time).num_milliseconds();
TransactionCostAnalysis {
order_id: order.id.clone(),
symbol: order.symbol.clone(),
side: order.side,
intended_size: order.quantity,
filled_size,
avg_fill_price,
arrival_price,
benchmark_price,
cost_components,
implementation_shortfall,
duration_ms,
}
}
fn calculate_cost_components(
&self,
order: &Order,
fills: &[Fill],
avg_fill_price: f64,
arrival_price: f64,
benchmark_price: f64,
microstructure: &MarketMicrostructure,
) -> CostComponents {
let filled_size = fills.iter().map(|f| f.quantity).sum::<f64>();
let total_value = filled_size * avg_fill_price;
// Spread cost (crossing the spread)
let spread_cost = filled_size * avg_fill_price * microstructure.avg_spread_bps / 10000.0;
// Market impact (price movement due to our order)
let side_multiplier = match order.side {
Side::Buy => 1.0,
Side::Sell => -1.0,
};
let market_impact = side_multiplier * filled_size * (avg_fill_price - arrival_price);
// Commission and fees
let gross_commission = total_value * self.commission_rate_bps / 10000.0;
let commission = gross_commission.max(self.min_commission * fills.len() as f64);
let exchange_fees = total_value * self.exchange_fees_bps / 10000.0;
let regulatory_fees = total_value * self.regulatory_fees_bps / 10000.0;
let total_fees = commission + exchange_fees + regulatory_fees;
// Slippage (difference from benchmark)
let slippage = side_multiplier * filled_size * (avg_fill_price - benchmark_price);
// Opportunity cost (unfilled portion)
let unfilled_size = order.quantity - filled_size;
let opportunity_cost = if unfilled_size > 0.0 {
// Cost of not executing at arrival price
side_multiplier * unfilled_size * (benchmark_price - arrival_price)
} else {
0.0
};
// Timing cost (delay cost)
let timing_cost = side_multiplier * filled_size *
(benchmark_price - arrival_price).max(0.0);
// Total cost
let total_cost = spread_cost + market_impact.abs() + total_fees +
slippage.abs() + opportunity_cost.abs() + timing_cost;
// Cost in basis points
let cost_bps = if total_value > 0.0 {
(total_cost / total_value) * 10000.0
} else {
0.0
};
CostComponents {
spread_cost,
market_impact: market_impact.abs(),
commission: total_fees,
slippage: slippage.abs(),
opportunity_cost: opportunity_cost.abs(),
timing_cost,
total_cost,
cost_bps,
}
}
pub fn calculate_pretrade_cost_estimate(
&self,
order: &Order,
microstructure: &MarketMicrostructure,
current_price: f64,
expected_fill_price: f64,
expected_fill_rate: f64,
) -> CostComponents {
let expected_filled_size = order.quantity * expected_fill_rate;
let total_value = expected_filled_size * expected_fill_price;
// Estimate spread cost
let spread_cost = expected_filled_size * expected_fill_price *
microstructure.avg_spread_bps / 10000.0;
// Estimate market impact
let side_multiplier = match order.side {
Side::Buy => 1.0,
Side::Sell => -1.0,
};
let market_impact = side_multiplier * expected_filled_size *
(expected_fill_price - current_price);
// Calculate commission
let gross_commission = total_value * self.commission_rate_bps / 10000.0;
let commission = gross_commission.max(self.min_commission);
let exchange_fees = total_value * self.exchange_fees_bps / 10000.0;
let regulatory_fees = total_value * self.regulatory_fees_bps / 10000.0;
let total_fees = commission + exchange_fees + regulatory_fees;
// Estimate opportunity cost for unfilled portion
let unfilled_size = order.quantity - expected_filled_size;
let opportunity_cost = if unfilled_size > 0.0 {
// Assume 10bps adverse movement for unfilled portion
unfilled_size * current_price * 0.001
} else {
0.0
};
// No slippage or timing cost for pre-trade estimate
let slippage = 0.0;
let timing_cost = 0.0;
// Total cost
let total_cost = spread_cost + market_impact.abs() + total_fees + opportunity_cost;
// Cost in basis points
let cost_bps = if total_value > 0.0 {
(total_cost / total_value) * 10000.0
} else {
0.0
};
CostComponents {
spread_cost,
market_impact: market_impact.abs(),
commission: total_fees,
slippage,
opportunity_cost,
timing_cost,
total_cost,
cost_bps,
}
}
}
#[derive(Debug, Clone)]
pub struct BenchmarkPrices {
pub vwap: f64,
pub twap: f64,
pub close: f64,
pub midpoint_at_arrival: f64,
}
impl Default for BenchmarkPrices {
fn default() -> Self {
Self {
vwap: 0.0,
twap: 0.0,
close: 0.0,
midpoint_at_arrival: 0.0,
}
}
}
// Helper to track and calculate various price benchmarks
pub struct BenchmarkCalculator {
trades: Vec<(DateTime<Utc>, f64, f64)>, // (time, price, volume)
quotes: Vec<(DateTime<Utc>, f64, f64)>, // (time, bid, ask)
}
impl BenchmarkCalculator {
pub fn new() -> Self {
Self {
trades: Vec::new(),
quotes: Vec::new(),
}
}
pub fn add_trade(&mut self, time: DateTime<Utc>, price: f64, volume: f64) {
self.trades.push((time, price, volume));
}
pub fn add_quote(&mut self, time: DateTime<Utc>, bid: f64, ask: f64) {
self.quotes.push((time, bid, ask));
}
pub fn calculate_benchmarks(
&self,
start_time: DateTime<Utc>,
end_time: DateTime<Utc>,
) -> BenchmarkPrices {
// Filter trades within time window
let window_trades: Vec<_> = self.trades.iter()
.filter(|(t, _, _)| *t >= start_time && *t <= end_time)
.cloned()
.collect();
// Calculate VWAP
let total_volume: f64 = window_trades.iter().map(|(_, _, v)| v).sum();
let vwap = if total_volume > 0.0 {
window_trades.iter()
.map(|(_, p, v)| p * v)
.sum::<f64>() / total_volume
} else {
0.0
};
// Calculate TWAP
let twap = if !window_trades.is_empty() {
window_trades.iter()
.map(|(_, p, _)| p)
.sum::<f64>() / window_trades.len() as f64
} else {
0.0
};
// Get close price (last trade)
let close = window_trades.last()
.map(|(_, p, _)| *p)
.unwrap_or(0.0);
// Get midpoint at arrival
let midpoint_at_arrival = self.quotes.iter()
.filter(|(t, _, _)| *t <= start_time)
.last()
.map(|(_, b, a)| (b + a) / 2.0)
.unwrap_or(0.0);
BenchmarkPrices {
vwap,
twap,
close,
midpoint_at_arrival,
}
}
}