Files
foxhunt/services/backtesting_service/tests/performance_metrics.rs
jgrusewski 2f57602f30 🚀 Wave 113 Phase 2+3: Complete coverage expansion and production readiness
SUMMARY: 39 agents, 90% production readiness (+7.5%)

PHASE 2: Service Coverage Expansion (Agents 27-34)
- 8,270 lines test code: trading (2,562), backtesting (1,740), compliance (1,462), data (2,506)
- 317 new tests across 16 test files

PHASE 3: Compilation Fixes & Validation (Agents 35-39)
- Fixed 49 errors (11 SQLx + 38 compliance API)
- 100% production code compilation
- 47.03% coverage baseline (+17.23%)
- 90.0% production readiness validated

METRICS:
- Tests: 700 → 1,532 (+119%)
- Coverage: 29.8% → 47.03% (+58%)
- Compliance: 0% → 83.3%
- Production readiness: 82.5% → 90.0%

🤖 Wave 113 Complete - Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-06 09:24:09 +02:00

459 lines
17 KiB
Rust

//! Comprehensive tests for performance metrics calculation
//!
//! Target Coverage: 70%+ for Sharpe ratio, drawdown, win rate, and all performance metrics
use anyhow::Result;
use chrono::{Duration, Utc};
use rust_decimal::Decimal;
mod mock_repositories;
use backtesting_service::performance::{PerformanceAnalyzer, PerformanceMetrics};
use backtesting_service::strategy_engine::{BacktestTrade, TradeSide};
use config::structures::BacktestingPerformanceConfig;
/// Helper to create a sample trade
fn create_trade(
id: u32,
symbol: &str,
side: TradeSide,
quantity: f64,
entry_price: f64,
exit_price: f64,
entry_offset_days: i64,
exit_offset_days: i64,
) -> BacktestTrade {
let base_time = Utc::now() - Duration::days(100);
let entry_time = base_time + Duration::days(entry_offset_days);
let exit_time = base_time + Duration::days(exit_offset_days);
let pnl = (exit_price - entry_price) * quantity;
let return_percent = pnl / (entry_price * quantity);
BacktestTrade {
trade_id: format!("trade_{}", id),
symbol: symbol.to_string(),
side,
quantity: Decimal::from_f64_retain(quantity).unwrap_or(Decimal::ZERO),
entry_price: Decimal::from_f64_retain(entry_price).unwrap_or(Decimal::ZERO),
exit_price: Decimal::from_f64_retain(exit_price).unwrap_or(Decimal::ZERO),
entry_time,
exit_time,
pnl: Decimal::from_f64_retain(pnl).unwrap_or(Decimal::ZERO),
return_percent: Decimal::from_f64_retain(return_percent).unwrap_or(Decimal::ZERO),
entry_signal: "buy_signal".to_string(),
exit_signal: "sell_signal".to_string(),
}
}
/// Test basic performance metrics calculation
#[test]
fn test_basic_performance_metrics() -> Result<()> {
let config = BacktestingPerformanceConfig::default();
let analyzer = PerformanceAnalyzer::new(&config)?;
let trades = vec![
create_trade(1, "AAPL", TradeSide::Buy, 100.0, 150.0, 155.0, 0, 10), // +$500
create_trade(2, "AAPL", TradeSide::Buy, 100.0, 155.0, 160.0, 10, 20), // +$500
create_trade(3, "AAPL", TradeSide::Buy, 100.0, 160.0, 158.0, 20, 30), // -$200
];
let initial_capital = 100000.0;
let metrics = analyzer.calculate_metrics(&trades, initial_capital);
// Total PnL should be $800
assert!((metrics.total_return - 0.8).abs() < 0.01, "Total return should be 0.8%");
// Should have 3 total trades
assert_eq!(metrics.total_trades, 3);
// Win rate should be 66.67% (2 wins, 1 loss)
assert!((metrics.win_rate - 66.67).abs() < 0.1);
// 2 winning trades, 1 losing trade
assert_eq!(metrics.winning_trades, 2);
assert_eq!(metrics.losing_trades, 1);
Ok(())
}
/// Test Sharpe ratio calculation
#[test]
fn test_sharpe_ratio_calculation() -> Result<()> {
let config = BacktestingPerformanceConfig::default();
let analyzer = PerformanceAnalyzer::new(&config)?;
// Create trades with consistent positive returns
let trades = vec![
create_trade(1, "AAPL", TradeSide::Buy, 100.0, 100.0, 105.0, 0, 1), // +5%
create_trade(2, "AAPL", TradeSide::Buy, 100.0, 100.0, 105.0, 1, 2), // +5%
create_trade(3, "AAPL", TradeSide::Buy, 100.0, 100.0, 105.0, 2, 3), // +5%
create_trade(4, "AAPL", TradeSide::Buy, 100.0, 100.0, 105.0, 3, 4), // +5%
create_trade(5, "AAPL", TradeSide::Buy, 100.0, 100.0, 105.0, 4, 5), // +5%
];
let metrics = analyzer.calculate_metrics(&trades, 100000.0);
// Sharpe ratio should be positive for consistent positive returns
assert!(metrics.sharpe_ratio > 0.0, "Sharpe ratio should be positive");
Ok(())
}
/// Test Sortino ratio calculation
#[test]
fn test_sortino_ratio_calculation() -> Result<()> {
let config = BacktestingPerformanceConfig::default();
let analyzer = PerformanceAnalyzer::new(&config)?;
// Mix of wins and losses
let trades = vec![
create_trade(1, "AAPL", TradeSide::Buy, 100.0, 100.0, 110.0, 0, 5), // +10%
create_trade(2, "AAPL", TradeSide::Buy, 100.0, 100.0, 95.0, 5, 10), // -5%
create_trade(3, "AAPL", TradeSide::Buy, 100.0, 100.0, 108.0, 10, 15), // +8%
create_trade(4, "AAPL", TradeSide::Buy, 100.0, 100.0, 92.0, 15, 20), // -8%
create_trade(5, "AAPL", TradeSide::Buy, 100.0, 100.0, 112.0, 20, 25), // +12%
];
let metrics = analyzer.calculate_metrics(&trades, 100000.0);
// Sortino ratio should be calculated (can be positive or negative depending on downside)
assert!(metrics.sortino_ratio.is_finite(), "Sortino ratio should be finite");
Ok(())
}
/// Test maximum drawdown calculation
#[test]
fn test_maximum_drawdown() -> Result<()> {
let config = BacktestingPerformanceConfig::default();
let analyzer = PerformanceAnalyzer::new(&config)?;
// Trades that create a significant drawdown
let trades = vec![
create_trade(1, "AAPL", TradeSide::Buy, 100.0, 100.0, 120.0, 0, 5), // +20% (peak)
create_trade(2, "AAPL", TradeSide::Buy, 100.0, 120.0, 110.0, 5, 10), // -10%
create_trade(3, "AAPL", TradeSide::Buy, 100.0, 110.0, 90.0, 10, 15), // -20% (trough)
create_trade(4, "AAPL", TradeSide::Buy, 100.0, 90.0, 115.0, 15, 20), // +25% (recovery)
];
let metrics = analyzer.calculate_metrics(&trades, 100000.0);
// Max drawdown should be significant
assert!(metrics.max_drawdown > 0.0, "Max drawdown should be positive");
assert!(metrics.max_drawdown < 100.0, "Max drawdown should be less than 100%");
Ok(())
}
/// Test win rate calculation
#[test]
fn test_win_rate_calculation() -> Result<()> {
let config = BacktestingPerformanceConfig::default();
let analyzer = PerformanceAnalyzer::new(&config)?;
// 7 wins, 3 losses = 70% win rate
let trades = vec![
create_trade(1, "AAPL", TradeSide::Buy, 100.0, 100.0, 105.0, 0, 1), // Win
create_trade(2, "AAPL", TradeSide::Buy, 100.0, 100.0, 103.0, 1, 2), // Win
create_trade(3, "AAPL", TradeSide::Buy, 100.0, 100.0, 95.0, 2, 3), // Loss
create_trade(4, "AAPL", TradeSide::Buy, 100.0, 100.0, 107.0, 3, 4), // Win
create_trade(5, "AAPL", TradeSide::Buy, 100.0, 100.0, 106.0, 4, 5), // Win
create_trade(6, "AAPL", TradeSide::Buy, 100.0, 100.0, 98.0, 5, 6), // Loss
create_trade(7, "AAPL", TradeSide::Buy, 100.0, 100.0, 104.0, 6, 7), // Win
create_trade(8, "AAPL", TradeSide::Buy, 100.0, 100.0, 102.0, 7, 8), // Win
create_trade(9, "AAPL", TradeSide::Buy, 100.0, 100.0, 97.0, 8, 9), // Loss
create_trade(10, "AAPL", TradeSide::Buy, 100.0, 100.0, 108.0, 9, 10), // Win
];
let metrics = analyzer.calculate_metrics(&trades, 100000.0);
assert_eq!(metrics.total_trades, 10);
assert_eq!(metrics.winning_trades, 7);
assert_eq!(metrics.losing_trades, 3);
assert!((metrics.win_rate - 70.0).abs() < 0.1, "Win rate should be 70%");
Ok(())
}
/// Test profit factor calculation
#[test]
fn test_profit_factor() -> Result<()> {
let config = BacktestingPerformanceConfig::default();
let analyzer = PerformanceAnalyzer::new(&config)?;
// Gross profit: $1000, Gross loss: $300 -> Profit factor: 3.33
let trades = vec![
create_trade(1, "AAPL", TradeSide::Buy, 100.0, 100.0, 110.0, 0, 5), // +$1000
create_trade(2, "AAPL", TradeSide::Buy, 100.0, 100.0, 97.0, 5, 10), // -$300
];
let metrics = analyzer.calculate_metrics(&trades, 100000.0);
assert!(metrics.profit_factor > 3.0 && metrics.profit_factor < 3.5,
"Profit factor should be ~3.33");
Ok(())
}
/// Test average win and loss calculation
#[test]
fn test_average_win_loss() -> Result<()> {
let config = BacktestingPerformanceConfig::default();
let analyzer = PerformanceAnalyzer::new(&config)?;
let trades = vec![
create_trade(1, "AAPL", TradeSide::Buy, 100.0, 100.0, 110.0, 0, 1), // +$1000
create_trade(2, "AAPL", TradeSide::Buy, 100.0, 100.0, 106.0, 1, 2), // +$600
create_trade(3, "AAPL", TradeSide::Buy, 100.0, 100.0, 95.0, 2, 3), // -$500
create_trade(4, "AAPL", TradeSide::Buy, 100.0, 100.0, 92.0, 3, 4), // -$800
];
let metrics = analyzer.calculate_metrics(&trades, 100000.0);
// Average win: ($1000 + $600) / 2 = $800
assert!((metrics.avg_win - 800.0).abs() < 1.0, "Average win should be $800");
// Average loss: -($500 + $800) / 2 = -$650
assert!((metrics.avg_loss + 650.0).abs() < 1.0, "Average loss should be -$650");
Ok(())
}
/// Test largest win and loss tracking
#[test]
fn test_largest_win_loss() -> Result<()> {
let config = BacktestingPerformanceConfig::default();
let analyzer = PerformanceAnalyzer::new(&config)?;
let trades = vec![
create_trade(1, "AAPL", TradeSide::Buy, 100.0, 100.0, 105.0, 0, 1), // +$500
create_trade(2, "AAPL", TradeSide::Buy, 100.0, 100.0, 115.0, 1, 2), // +$1500 (largest win)
create_trade(3, "AAPL", TradeSide::Buy, 100.0, 100.0, 95.0, 2, 3), // -$500
create_trade(4, "AAPL", TradeSide::Buy, 100.0, 100.0, 88.0, 3, 4), // -$1200 (largest loss)
];
let metrics = analyzer.calculate_metrics(&trades, 100000.0);
assert!((metrics.largest_win - 1500.0).abs() < 1.0, "Largest win should be $1500");
assert!((metrics.largest_loss + 1200.0).abs() < 1.0, "Largest loss should be -$1200");
Ok(())
}
/// Test Calmar ratio calculation
#[test]
fn test_calmar_ratio() -> Result<()> {
let config = BacktestingPerformanceConfig::default();
let analyzer = PerformanceAnalyzer::new(&config)?;
// Create trades over a year with known drawdown
let trades = vec![
create_trade(1, "AAPL", TradeSide::Buy, 100.0, 100.0, 120.0, 0, 90), // +20%
create_trade(2, "AAPL", TradeSide::Buy, 100.0, 120.0, 110.0, 90, 180), // -10%
create_trade(3, "AAPL", TradeSide::Buy, 100.0, 110.0, 130.0, 180, 365), // +20%
];
let metrics = analyzer.calculate_metrics(&trades, 100000.0);
// Calmar = Annualized Return / Max Drawdown
assert!(metrics.calmar_ratio > 0.0, "Calmar ratio should be positive");
Ok(())
}
/// Test VaR (Value at Risk) calculation
#[test]
fn test_var_calculation() -> Result<()> {
let config = BacktestingPerformanceConfig::default();
let analyzer = PerformanceAnalyzer::new(&config)?;
// Mix of returns for VaR calculation
let trades = vec![
create_trade(1, "AAPL", TradeSide::Buy, 100.0, 100.0, 105.0, 0, 1),
create_trade(2, "AAPL", TradeSide::Buy, 100.0, 100.0, 103.0, 1, 2),
create_trade(3, "AAPL", TradeSide::Buy, 100.0, 100.0, 98.0, 2, 3),
create_trade(4, "AAPL", TradeSide::Buy, 100.0, 100.0, 107.0, 3, 4),
create_trade(5, "AAPL", TradeSide::Buy, 100.0, 100.0, 95.0, 4, 5),
];
let metrics = analyzer.calculate_metrics(&trades, 100000.0);
assert!(metrics.var_95.is_some(), "VaR should be calculated");
let var = metrics.var_95.unwrap();
assert!(var < 0.0, "VaR should be negative (potential loss)");
Ok(())
}
/// Test expected shortfall calculation
#[test]
fn test_expected_shortfall() -> Result<()> {
let config = BacktestingPerformanceConfig::default();
let analyzer = PerformanceAnalyzer::new(&config)?;
let trades = vec![
create_trade(1, "AAPL", TradeSide::Buy, 100.0, 100.0, 105.0, 0, 1),
create_trade(2, "AAPL", TradeSide::Buy, 100.0, 100.0, 92.0, 1, 2),
create_trade(3, "AAPL", TradeSide::Buy, 100.0, 100.0, 108.0, 2, 3),
create_trade(4, "AAPL", TradeSide::Buy, 100.0, 100.0, 90.0, 3, 4),
];
let metrics = analyzer.calculate_metrics(&trades, 100000.0);
assert!(metrics.expected_shortfall.is_some(), "Expected shortfall should be calculated");
Ok(())
}
/// Test annualized return calculation
#[test]
fn test_annualized_return() -> Result<()> {
let config = BacktestingPerformanceConfig::default();
let analyzer = PerformanceAnalyzer::new(&config)?;
// Trades over 6 months with 10% total return
let trades = vec![
create_trade(1, "AAPL", TradeSide::Buy, 100.0, 100.0, 110.0, 0, 180), // +10% over 6 months
];
let metrics = analyzer.calculate_metrics(&trades, 100000.0);
// Annualized return should be higher than 10% (compound effect)
assert!(metrics.annualized_return > 10.0, "Annualized return should be > 10%");
Ok(())
}
/// Test volatility calculation
#[test]
fn test_volatility_calculation() -> Result<()> {
let config = BacktestingPerformanceConfig::default();
let analyzer = PerformanceAnalyzer::new(&config)?;
// High volatility trades
let trades = vec![
create_trade(1, "AAPL", TradeSide::Buy, 100.0, 100.0, 120.0, 0, 1), // +20%
create_trade(2, "AAPL", TradeSide::Buy, 100.0, 100.0, 85.0, 1, 2), // -15%
create_trade(3, "AAPL", TradeSide::Buy, 100.0, 100.0, 115.0, 2, 3), // +15%
create_trade(4, "AAPL", TradeSide::Buy, 100.0, 100.0, 90.0, 3, 4), // -10%
];
let metrics = analyzer.calculate_metrics(&trades, 100000.0);
assert!(metrics.volatility > 0.0, "Volatility should be positive");
Ok(())
}
/// Test edge case: no trades
#[test]
fn test_no_trades() -> Result<()> {
let config = BacktestingPerformanceConfig::default();
let analyzer = PerformanceAnalyzer::new(&config)?;
let trades: Vec<BacktestTrade> = vec![];
let metrics = analyzer.calculate_metrics(&trades, 100000.0);
assert_eq!(metrics.total_trades, 0);
assert_eq!(metrics.total_return, 0.0);
assert_eq!(metrics.win_rate, 0.0);
assert_eq!(metrics.sharpe_ratio, 0.0);
Ok(())
}
/// Test edge case: all winning trades
#[test]
fn test_all_winning_trades() -> Result<()> {
let config = BacktestingPerformanceConfig::default();
let analyzer = PerformanceAnalyzer::new(&config)?;
let trades = vec![
create_trade(1, "AAPL", TradeSide::Buy, 100.0, 100.0, 105.0, 0, 1),
create_trade(2, "AAPL", TradeSide::Buy, 100.0, 100.0, 103.0, 1, 2),
create_trade(3, "AAPL", TradeSide::Buy, 100.0, 100.0, 107.0, 2, 3),
];
let metrics = analyzer.calculate_metrics(&trades, 100000.0);
assert_eq!(metrics.win_rate, 100.0);
assert_eq!(metrics.winning_trades, 3);
assert_eq!(metrics.losing_trades, 0);
assert!(metrics.profit_factor.is_infinite(), "Profit factor should be infinite with no losses");
Ok(())
}
/// Test edge case: all losing trades
#[test]
fn test_all_losing_trades() -> Result<()> {
let config = BacktestingPerformanceConfig::default();
let analyzer = PerformanceAnalyzer::new(&config)?;
let trades = vec![
create_trade(1, "AAPL", TradeSide::Buy, 100.0, 100.0, 95.0, 0, 1),
create_trade(2, "AAPL", TradeSide::Buy, 100.0, 100.0, 93.0, 1, 2),
create_trade(3, "AAPL", TradeSide::Buy, 100.0, 100.0, 92.0, 2, 3),
];
let metrics = analyzer.calculate_metrics(&trades, 100000.0);
assert_eq!(metrics.win_rate, 0.0);
assert_eq!(metrics.winning_trades, 0);
assert_eq!(metrics.losing_trades, 3);
assert_eq!(metrics.profit_factor, 0.0, "Profit factor should be 0 with no wins");
Ok(())
}
/// Test equity curve generation
#[test]
fn test_equity_curve_generation() -> Result<()> {
let config = BacktestingPerformanceConfig::default();
let analyzer = PerformanceAnalyzer::new(&config)?;
let trades = vec![
create_trade(1, "AAPL", TradeSide::Buy, 100.0, 100.0, 105.0, 0, 1),
create_trade(2, "AAPL", TradeSide::Buy, 100.0, 100.0, 110.0, 1, 2),
create_trade(3, "AAPL", TradeSide::Buy, 100.0, 100.0, 108.0, 2, 3),
];
let initial_capital = 100000.0;
let equity_curve = analyzer.generate_equity_curve(&trades, initial_capital);
// Should have points for each trade + initial
assert!(equity_curve.len() >= 4, "Equity curve should have at least 4 points");
// First point should be initial capital
assert!((equity_curve[0].equity - initial_capital).abs() < 0.01);
// Drawdown at start should be 0
assert_eq!(equity_curve[0].drawdown, 0.0);
Ok(())
}
/// Test rolling metrics calculation
#[test]
fn test_rolling_metrics() -> Result<()> {
let config = BacktestingPerformanceConfig::default();
let analyzer = PerformanceAnalyzer::new(&config)?;
let trades = vec![
create_trade(1, "AAPL", TradeSide::Buy, 100.0, 100.0, 105.0, 0, 5),
create_trade(2, "AAPL", TradeSide::Buy, 100.0, 100.0, 103.0, 5, 10),
create_trade(3, "AAPL", TradeSide::Buy, 100.0, 100.0, 107.0, 10, 15),
create_trade(4, "AAPL", TradeSide::Buy, 100.0, 100.0, 102.0, 15, 20),
create_trade(5, "AAPL", TradeSide::Buy, 100.0, 100.0, 108.0, 20, 25),
];
let rolling = analyzer.calculate_rolling_metrics(&trades, 10);
assert!(!rolling.rolling_sharpe.is_empty(), "Rolling Sharpe should be calculated");
assert!(!rolling.rolling_volatility.is_empty(), "Rolling volatility should be calculated");
assert!(!rolling.rolling_returns.is_empty(), "Rolling returns should be calculated");
Ok(())
}