Implement comprehensive Runpod deployment with S3 volume mount architecture for FP32 ML model training on Tesla V100 GPUs. ## Infrastructure Components ### Deployment Scripts (scripts/) - runpod_deploy.sh: Master deployment orchestrator (8-step workflow) - runpod_upload.sh: S3 upload for binaries and test data - upload_env_to_runpod.sh: Secure .env credentials upload - runpod_deploy_test.sh: Prerequisites validation ### Docker Configuration - Dockerfile.runpod: Multi-stage CUDA 12.1 runtime (~2GB, no binaries) - entrypoint.sh: Volume verification and training execution - Architecture: Volume mount (NO S3 downloads in pods) ### S3 Configuration - Bucket: se3zdnb5o4 (Iceland region: eur-is-1) - Endpoint: https://s3api-eur-is-1.runpod.io - Structure: binaries/, test_data/, models/, .env ### OpenTofu Infrastructure (terraform/runpod/) - main.tf: Pod and volume resources - variables.tf: Configuration variables - outputs.tf: Pod connection info - Security: NO credentials in state (uses volume .env) ## Deployment Assets Uploaded ### Training Binaries (77MB) - train_tft_parquet (23M) - TFT-225 features - train_mamba2_parquet (22M) - MAMBA-2 state space - train_dqn (22M) - Deep Q-Network - train_ppo (13M) - Proximal Policy Optimization ### Test Data (13.8 MB) - 9 Parquet files: ES.FUT, NQ.FUT, 6E.FUT, ZN.FUT (180-day datasets) ### Credentials - .env file (1.5 KB, private access, chmod 600) ## Documentation ### Deployment Guides - RUNPOD_DEPLOYMENT_READY_SUMMARY.md: Complete deployment status - RUNPOD_VOLUME_DEPLOYMENT_GUIDE.md: Step-by-step guide (42KB) - RUNPOD_DEPLOYMENT_QUICK_START.md: Quick reference - RUNPOD_UPLOAD_GUIDE.md: S3 upload instructions - RUNPOD_VOLUME_CONFIGURATION_COMPLETE.md: S3 setup report - RUNPOD_S3_PARQUET_UPLOAD_REPORT.md: Data upload verification ### Architecture Documentation - RUNPOD_VOLUME_MOUNT_ARCHITECTURE.md: Volume mount design - RUNPOD_S3_ARCHITECTURE_DIAGRAM.txt: S3 API vs filesystem access - DOCKERFILE_RUNPOD_FINAL_SUMMARY.md: Docker image specification ### Decision Documentation - RUNPOD_DEPLOYMENT_CHECKLIST.md: Go/no-go decision matrix (27KB) - RUNPOD_DEPLOYMENT_DECISION_TREE.md: Decision workflow - FP32_RUNPOD_DEPLOYMENT_READY.md: FP32 deployment readiness ## QAT Enhancements ### Core QAT Infrastructure - ml/src/memory_optimization/qat.rs: Enhanced QAT observer (+226 lines) - ml/src/memory_optimization/auto_batch_size.rs: OOM recovery (+84 lines) - ml/src/tft/qat_tft.rs: QAT TFT wrapper (+154 lines) - ml/src/trainers/tft.rs: QAT training integration (+433 lines) - ml/src/qat_metrics_exporter.rs: NEW - QAT metrics export ### QAT Testing - ml/tests/qat_integration_tests.rs: NEW - Integration test suite - ml/tests/qat_gradient_clipping_test.rs: NEW - Gradient clipping tests - ml/tests/qat_device_consistency_test.rs: Device mismatch tests (+205 lines) - ml/tests/qat_accuracy_validation_test.rs: Accuracy validation - ml/tests/qat_tft_integration_test.rs: TFT QAT integration ### QAT Documentation - ml/docs/QAT_GUIDE.md: Comprehensive QAT guide (+616 lines) - ml/docs/QAT_GRADIENT_CHECKPOINTING_WORKAROUND.md: NEW - Workaround guide - QAT_BLOCKERS_ROOT_CAUSE_ANALYSIS.md: P0 blocker analysis (44KB) - QAT_ACCURACY_VALIDATION_REPORT.md: Accuracy comparison - QAT_GRADIENT_CLIPPING_VALIDATION_REPORT.md: Clipping validation ### QAT Monitoring - config/grafana/dashboards/qat-training-metrics.json: NEW - Grafana dashboard ## AWS CLI Configuration ### Credentials Setup - ~/.aws/credentials: Runpod profile configured - Access Key: user_2xxA3XcIFj16yfL3aBon9niiSpr - Secret Key: (from RUNPOD_S3_SECRET) - ~/.aws/config: Iceland region (eur-is-1) ## Production Readiness ### FP32 Models: ✅ READY FOR DEPLOYMENT - DQN: 15-20s training, ~6MB GPU memory - PPO: 7-10s training, ~145MB GPU memory - MAMBA-2: 2-3 min training, ~164MB GPU memory - TFT-225: 3-5 min training, ~500MB GPU memory - Total GPU Budget: 815MB (fits on 4GB+ Tesla V100) ### QAT Models: 🔴 BLOCKED - 24 tests implemented but DO NOT COMPILE (11 errors) - 3 P0 blockers: device mismatch, gradient checkpointing, OOM recovery - Timeline: 1-2 weeks to fix (13h P0 fixes + validation) ### Wave D Features: ✅ OPERATIONAL - 225 features fully integrated - Feature extraction: 5.10μs/bar (196x faster than target) - Wave D backtest: Sharpe 2.00, Win Rate 60%, Drawdown 15% - Database migration 045: Applied cleanly, zero conflicts ## Cost Analysis ### One-Time Setup - Network Volume: $4/month (50GB SSD) - Upload costs: FREE (S3 API included) ### Per Training Run (TFT-225) - GPU: Tesla V100-PCIE-16GB @ $0.29/hr - Training Time: ~4 hours - Cost per run: $1.16 ### Monthly (20 Training Runs) - Storage: $4.00/month - Training: $23.20/month (20 runs × $1.16) - Total: $27.20/month ## Security ### Credentials Management - ✅ NO credentials in Docker image - ✅ NO credentials in Terraform state - ✅ .env gitignored and not committed - ✅ .env file private on S3 (HTTP 401 on public access) - ✅ Docker Hub repository PRIVATE (jgrusewski/foxhunt) ### Access Control - S3 API: Local client uploads only - Volume mount: Pod filesystem access only - Authentication: AWS CLI with Runpod profile required ## Next Steps 1. ✅ COMPLETE: Build Docker image 2. ⏳ PENDING: Push to Docker Hub 3. ⏳ PENDING: Deploy pod via Runpod console 4. ⏳ PENDING: Validate training on Tesla V100 ## Performance Targets - Build time: 5-10 min - Upload time: ~20 sec (90MB total) - Pod startup: ~30 sec - Training time: 3-5 min (TFT-225) - Total deployment: ~40 min from start to first training run ## Test Status - FP32 tests: 597/608 passing (98.2%) - QAT tests: 0/24 passing (compilation errors) - Overall: 2,062/2,086 passing (98.8% excluding QAT) 🤖 Generated with Claude Code (https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
593 lines
20 KiB
Rust
593 lines
20 KiB
Rust
//! TDD E2E Integration Tests for ML Trading Pipeline
|
|
//!
|
|
//! **Mission**: Comprehensive end-to-end tests for ML trading pipeline using strict TDD methodology
|
|
//! **Methodology**: RED (Failing Tests) → GREEN (Minimal Implementation) → REFACTOR (Quality)
|
|
//!
|
|
//! ## Test Coverage
|
|
//! 1. End-to-end ML trading pipeline (data → features → prediction → order → tracking)
|
|
//! 2. Ensemble consensus voting with disagreement handling
|
|
//! 3. Fallback to rule-based on low confidence
|
|
//! 4. Multi-symbol trading with ML predictions
|
|
//! 5. Performance tracking (accuracy, Sharpe ratio)
|
|
//! 6. Risk limits override ML signals
|
|
//! 7. Model comparison across 4 models
|
|
//!
|
|
//! ## TDD Protocol
|
|
//! - **RED Phase**: All tests are `#[ignore]` and WILL FAIL
|
|
//! - **GREEN Phase**: Remove `#[ignore]` and implement minimal code to pass
|
|
//! - **REFACTOR Phase**: Improve code quality without changing behavior
|
|
|
|
#![allow(unused_imports)]
|
|
|
|
use anyhow::{anyhow, Result};
|
|
use candle_core::Device;
|
|
use common::{CommonError, OrderSide, OrderType};
|
|
use sqlx::PgPool;
|
|
use std::collections::HashMap;
|
|
use std::path::PathBuf;
|
|
use uuid::Uuid;
|
|
|
|
// Import trading service ML components
|
|
use trading_service::{
|
|
ml_performance_metrics::MLMetricsStore, Action, EnsembleCoordinator, Order,
|
|
PaperTradingExecutor, SignalSource, TradingSignal,
|
|
};
|
|
|
|
// Import rand for random testing
|
|
use rand;
|
|
|
|
// ============================================================================
|
|
// Test Infrastructure & Helper Functions
|
|
// ============================================================================
|
|
|
|
/// Create test database pool
|
|
async fn get_test_db_pool() -> PgPool {
|
|
let database_url = std::env::var("DATABASE_URL").unwrap_or_else(|_| {
|
|
"postgresql://foxhunt:foxhunt_dev_password@localhost:5432/foxhunt".to_string()
|
|
});
|
|
|
|
PgPool::connect(&database_url)
|
|
.await
|
|
.expect("Failed to connect to test database")
|
|
}
|
|
|
|
/// Create test ensemble coordinator with all 4 models (DQN, PPO, MAMBA2, TFT)
|
|
fn create_test_ensemble() -> std::sync::Arc<EnsembleCoordinator> {
|
|
use std::sync::Arc;
|
|
|
|
let coordinator = Arc::new(EnsembleCoordinator::new());
|
|
|
|
// Note: In real usage, models would be loaded and registered with the coordinator
|
|
// For tests, we create a minimal ensemble coordinator without loaded models
|
|
|
|
coordinator
|
|
}
|
|
|
|
/// Create test ensemble with low confidence (for fallback testing)
|
|
fn create_test_ensemble_low_confidence() -> std::sync::Arc<EnsembleCoordinator> {
|
|
// Same as above, but prediction will be mocked to return low confidence
|
|
create_test_ensemble()
|
|
}
|
|
|
|
/// Create single-model coordinator (for model comparison tests)
|
|
fn create_single_model_coordinator(model: &str) -> std::sync::Arc<EnsembleCoordinator> {
|
|
use std::sync::Arc;
|
|
|
|
let coordinator = Arc::new(EnsembleCoordinator::new());
|
|
|
|
// Note: In real usage, only the specified model would be loaded
|
|
// For tests, we create a minimal ensemble coordinator
|
|
|
|
coordinator
|
|
}
|
|
|
|
/// Load test OHLCV data (50 bars for feature extraction)
|
|
fn load_test_ohlcv_data(_symbol: &str, num_bars: usize) -> Vec<(f64, f64, f64, f64, f64)> {
|
|
// Generate synthetic OHLCV data with realistic pattern
|
|
let mut data = Vec::new();
|
|
let mut base_price = 4500.0; // ES.FUT starting price
|
|
|
|
for i in 0..num_bars {
|
|
let trend = (i as f64 * 0.1).sin(); // Add sine wave trend
|
|
let open = base_price + trend * 10.0;
|
|
let high = open + (i as f64 % 5.0) + 5.0;
|
|
let low = open - (i as f64 % 3.0) - 3.0;
|
|
let close = open + trend * 5.0;
|
|
let volume = 1000.0 + (i as f64 * 10.0);
|
|
|
|
data.push((open, high, low, close, volume));
|
|
base_price = close; // Next bar starts from previous close
|
|
}
|
|
|
|
data
|
|
}
|
|
|
|
/// Load test data with model disagreement (divergent trends)
|
|
fn load_test_data_with_disagreement() -> Vec<(f64, f64, f64, f64, f64)> {
|
|
// Generate data that creates model disagreement
|
|
let mut data = Vec::new();
|
|
let mut base_price = 4500.0;
|
|
|
|
for i in 0..50 {
|
|
// Create choppy market with no clear trend
|
|
let noise = ((i * 7) % 13) as f64 * 2.0 - 13.0;
|
|
let open = base_price + noise;
|
|
let high = open + (i as f64 % 3.0) + 3.0;
|
|
let low = open - (i as f64 % 2.0) - 2.0;
|
|
let close = open + noise * 0.3;
|
|
let volume = 1000.0 + (i as f64 * 5.0);
|
|
|
|
data.push((open, high, low, close, volume));
|
|
base_price = close;
|
|
}
|
|
|
|
data
|
|
}
|
|
|
|
// ============================================================================
|
|
// TEST 1: End-to-End ML Trading Pipeline (RED Phase)
|
|
// ============================================================================
|
|
|
|
#[tokio::test]
|
|
#[ignore = "RED: This test will fail until implementation is complete"]
|
|
async fn test_e2e_ml_trading_pipeline() {
|
|
// RED: End-to-end test from feature extraction to order execution
|
|
let pool = get_test_db_pool().await;
|
|
|
|
// 1. Load real market data
|
|
let market_data = load_test_ohlcv_data("ES.FUT", 50);
|
|
assert_eq!(market_data.len(), 50, "Need 50 OHLCV bars");
|
|
|
|
// 2. Create ML engine (feature extraction happens inside PaperTradingExecutor)
|
|
let ensemble = create_test_ensemble();
|
|
|
|
// Note: Feature extraction is now handled internally by PaperTradingExecutor
|
|
// using ml::features::UnifiedFeatureExtractor (256-dim features)
|
|
|
|
// 3. Execute paper trading order
|
|
let mut executor = PaperTradingExecutor::new_with_ml(pool.clone(), ensemble)
|
|
.await
|
|
.expect("Failed to create executor with ML");
|
|
|
|
// Generate ML signal (includes feature extraction internally)
|
|
let signal = executor
|
|
.generate_ml_signal(&market_data)
|
|
.await
|
|
.expect("Failed to generate ML signal");
|
|
assert!(
|
|
signal.confidence >= 0.0,
|
|
"Signal should have valid confidence"
|
|
);
|
|
|
|
// 4. Execute order based on signal
|
|
let order = executor
|
|
.execute_ml_signal(&signal, "ES.FUT")
|
|
.await
|
|
.expect("Failed to execute ML signal");
|
|
|
|
// Verify order created
|
|
assert_ne!(order.id, Uuid::nil());
|
|
assert_eq!(order.symbol, "ES.FUT");
|
|
|
|
// 5. Verify prediction stored in database
|
|
let prediction = sqlx::query!(
|
|
"SELECT * FROM ml_predictions WHERE order_id = $1 ORDER BY id DESC LIMIT 1",
|
|
order.id
|
|
)
|
|
.fetch_one(&pool)
|
|
.await
|
|
.expect("Failed to fetch prediction");
|
|
|
|
assert_eq!(prediction.symbol, "ES.FUT");
|
|
assert!((prediction.confidence as f64 - ensemble.confidence).abs() < 0.01);
|
|
|
|
// 6. Simulate outcome and record
|
|
executor
|
|
.record_outcome(order.id, 150.0)
|
|
.await
|
|
.expect("Failed to record outcome"); // +$150 profit
|
|
|
|
// 7. Verify performance metrics updated
|
|
let metrics_store = MLMetricsStore::new(pool);
|
|
let stats = metrics_store
|
|
.get_accuracy_stats("Ensemble")
|
|
.await
|
|
.expect("Failed to get accuracy stats");
|
|
|
|
assert_eq!(stats.total_predictions, 1);
|
|
assert_eq!(stats.correct_predictions, 1);
|
|
assert!((stats.accuracy - 1.0).abs() < 0.01);
|
|
}
|
|
|
|
// ============================================================================
|
|
// TEST 2: Ensemble Consensus Voting with Disagreement (RED Phase)
|
|
// ============================================================================
|
|
|
|
#[tokio::test]
|
|
#[ignore = "RED: This test will fail until implementation is complete"]
|
|
async fn test_ml_ensemble_consensus() {
|
|
// RED: Test ensemble voting with disagreement
|
|
let pool = get_test_db_pool().await;
|
|
let ensemble = create_test_ensemble();
|
|
|
|
// Load market data where models disagree
|
|
let market_data = load_test_data_with_disagreement();
|
|
|
|
let mut executor = PaperTradingExecutor::new_with_ml(pool, ensemble)
|
|
.await
|
|
.expect("Failed to create executor");
|
|
|
|
let signal = executor
|
|
.generate_ml_signal(&market_data)
|
|
.await
|
|
.expect("Failed to generate signal");
|
|
|
|
// Ensemble should use weighted voting
|
|
assert!(signal.model_votes.is_some(), "Should have model votes");
|
|
let votes = signal.model_votes.unwrap();
|
|
|
|
// At least 3/4 models should agree for high confidence
|
|
let action_val = signal.action.expect("Should have action") as usize;
|
|
let consensus_count = votes
|
|
.iter()
|
|
.filter(|(_, action, _)| *action == action_val)
|
|
.count();
|
|
|
|
if signal.confidence > 0.8 {
|
|
assert!(
|
|
consensus_count >= 3,
|
|
"High confidence requires 3+ model agreement, got {}/{}",
|
|
consensus_count,
|
|
votes.len()
|
|
);
|
|
}
|
|
}
|
|
|
|
// ============================================================================
|
|
// TEST 3: Fallback to Rule-Based on Low Confidence (RED Phase)
|
|
// ============================================================================
|
|
|
|
#[tokio::test]
|
|
#[ignore = "RED: This test will fail until implementation is complete"]
|
|
async fn test_ml_fallback_on_low_confidence() {
|
|
// RED: Test fallback to rule-based when confidence < 0.6
|
|
let pool = get_test_db_pool().await;
|
|
|
|
let ensemble = create_test_ensemble();
|
|
let mut executor = PaperTradingExecutor::new_with_ml(pool, ensemble)
|
|
.await
|
|
.expect("Failed to create executor");
|
|
|
|
// Disable ML to force fallback
|
|
executor.disable_ml().await;
|
|
|
|
let market_data = load_test_ohlcv_data("ES.FUT", 50);
|
|
let signal = executor
|
|
.generate_signal(&market_data)
|
|
.await
|
|
.expect("Failed to generate signal");
|
|
|
|
assert_eq!(
|
|
signal.source,
|
|
SignalSource::RuleBased,
|
|
"Source should be RuleBased"
|
|
);
|
|
assert!(signal.action.is_some(), "Should still generate signal");
|
|
}
|
|
|
|
// ============================================================================
|
|
// TEST 4: Multi-Symbol ML Trading (RED Phase)
|
|
// ============================================================================
|
|
|
|
#[tokio::test]
|
|
#[ignore = "RED: This test will fail until implementation is complete"]
|
|
async fn test_ml_multi_symbol_trading() {
|
|
// RED: Test ML predictions for multiple symbols
|
|
let pool = get_test_db_pool().await;
|
|
let ensemble = create_test_ensemble();
|
|
|
|
let mut executor = PaperTradingExecutor::new_with_ml(pool.clone(), ensemble)
|
|
.await
|
|
.expect("Failed to create executor");
|
|
|
|
let symbols = vec!["ES.FUT", "NQ.FUT", "ZN.FUT"];
|
|
|
|
for symbol in &symbols {
|
|
let market_data = load_test_ohlcv_data(symbol, 50);
|
|
let signal = executor
|
|
.generate_ml_signal(&market_data)
|
|
.await
|
|
.expect("Failed to generate signal");
|
|
|
|
if signal.confidence >= 0.6 {
|
|
let order = executor
|
|
.execute_ml_signal(&signal, symbol)
|
|
.await
|
|
.expect("Failed to execute signal");
|
|
assert_eq!(order.symbol, *symbol);
|
|
}
|
|
}
|
|
|
|
// Verify predictions for all symbols
|
|
let predictions =
|
|
sqlx::query!("SELECT symbol, COUNT(*) as count FROM ml_predictions GROUP BY symbol")
|
|
.fetch_all(&pool)
|
|
.await
|
|
.expect("Failed to fetch predictions");
|
|
|
|
assert!(
|
|
predictions.len() >= 1,
|
|
"At least 1 symbol should have predictions"
|
|
);
|
|
}
|
|
|
|
// ============================================================================
|
|
// TEST 5: ML Performance Tracking - Accuracy Calculation (RED Phase)
|
|
// ============================================================================
|
|
|
|
#[tokio::test]
|
|
#[ignore = "RED: This test will fail until implementation is complete"]
|
|
async fn test_ml_performance_tracking_accuracy() {
|
|
// RED: Test accuracy calculation with mixed outcomes
|
|
let pool = get_test_db_pool().await;
|
|
let ensemble = create_test_ensemble();
|
|
|
|
let mut executor = PaperTradingExecutor::new_with_ml(pool.clone(), ensemble)
|
|
.await
|
|
.expect("Failed to create executor");
|
|
|
|
// Execute 10 ML trades
|
|
for i in 0..10 {
|
|
let market_data = load_test_ohlcv_data("ES.FUT", 50);
|
|
let signal = executor
|
|
.generate_ml_signal(&market_data)
|
|
.await
|
|
.expect("Failed to generate signal");
|
|
|
|
let order = executor
|
|
.execute_ml_signal(&signal, "ES.FUT")
|
|
.await
|
|
.expect("Failed to execute signal");
|
|
|
|
// Record outcome: 7 correct, 3 incorrect
|
|
let pnl = if i < 7 { 100.0 } else { -50.0 };
|
|
executor
|
|
.record_outcome(order.id, pnl)
|
|
.await
|
|
.expect("Failed to record outcome");
|
|
}
|
|
|
|
// Verify accuracy metrics
|
|
let metrics_store = MLMetricsStore::new(pool);
|
|
let stats = metrics_store
|
|
.get_accuracy_stats("Ensemble")
|
|
.await
|
|
.expect("Failed to get accuracy stats");
|
|
|
|
assert_eq!(stats.total_predictions, 10);
|
|
assert_eq!(stats.correct_predictions, 7);
|
|
assert!((stats.accuracy - 0.7).abs() < 0.01);
|
|
}
|
|
|
|
// ============================================================================
|
|
// TEST 6: Sharpe Ratio Calculation (RED Phase)
|
|
// ============================================================================
|
|
|
|
#[tokio::test]
|
|
#[ignore = "RED: This test will fail until implementation is complete"]
|
|
async fn test_ml_sharpe_ratio_calculation() {
|
|
// RED: Test Sharpe ratio with profit/loss series
|
|
let pool = get_test_db_pool().await;
|
|
let ensemble = create_test_ensemble();
|
|
|
|
let mut executor = PaperTradingExecutor::new_with_ml(pool.clone(), ensemble)
|
|
.await
|
|
.expect("Failed to create executor");
|
|
|
|
// Execute trades with varying P&L
|
|
let pnls = vec![100.0, -50.0, 200.0, -30.0, 150.0, 80.0, -20.0, 120.0];
|
|
|
|
for pnl in pnls {
|
|
let market_data = load_test_ohlcv_data("ES.FUT", 50);
|
|
let signal = executor
|
|
.generate_ml_signal(&market_data)
|
|
.await
|
|
.expect("Failed to generate signal");
|
|
|
|
let order = executor
|
|
.execute_ml_signal(&signal, "ES.FUT")
|
|
.await
|
|
.expect("Failed to execute signal");
|
|
|
|
executor
|
|
.record_outcome(order.id, pnl)
|
|
.await
|
|
.expect("Failed to record outcome");
|
|
}
|
|
|
|
// Calculate Sharpe ratio
|
|
let metrics_store = MLMetricsStore::new(pool);
|
|
let sharpe = metrics_store
|
|
.calculate_sharpe_ratio("Ensemble")
|
|
.await
|
|
.expect("Failed to calculate Sharpe ratio");
|
|
|
|
// Sharpe > 0 means profitable with controlled risk
|
|
assert!(sharpe > 0.0, "Sharpe ratio should be positive");
|
|
|
|
// Annualized Sharpe > 1.0 is good
|
|
if sharpe > 1.0 {
|
|
println!("✅ Good Sharpe ratio: {:.2}", sharpe);
|
|
}
|
|
}
|
|
|
|
// ============================================================================
|
|
// TEST 7: Risk Limits Override ML Signals (RED Phase)
|
|
// ============================================================================
|
|
|
|
#[tokio::test]
|
|
#[ignore = "RED: This test will fail until implementation is complete"]
|
|
async fn test_ml_risk_limits_override() {
|
|
// RED: Test that risk limits override ML signals
|
|
let pool = get_test_db_pool().await;
|
|
let ensemble = create_test_ensemble();
|
|
|
|
let mut executor = PaperTradingExecutor::new_with_ml(pool, ensemble)
|
|
.await
|
|
.expect("Failed to create executor");
|
|
|
|
// Set strict position limit
|
|
executor
|
|
.set_position_limit("ES.FUT", 5)
|
|
.await
|
|
.expect("Failed to set position limit");
|
|
|
|
// Execute 5 trades (hit limit)
|
|
for _ in 0..5 {
|
|
let market_data = load_test_ohlcv_data("ES.FUT", 50);
|
|
let signal = executor
|
|
.generate_ml_signal(&market_data)
|
|
.await
|
|
.expect("Failed to generate signal");
|
|
|
|
executor
|
|
.execute_ml_signal(&signal, "ES.FUT")
|
|
.await
|
|
.expect("Failed to execute signal");
|
|
}
|
|
|
|
// 6th trade should be rejected
|
|
let market_data = load_test_ohlcv_data("ES.FUT", 50);
|
|
let signal = executor
|
|
.generate_ml_signal(&market_data)
|
|
.await
|
|
.expect("Failed to generate signal");
|
|
|
|
let result = executor.execute_ml_signal(&signal, "ES.FUT").await;
|
|
|
|
assert!(
|
|
result.is_err(),
|
|
"6th trade should be rejected due to position limit"
|
|
);
|
|
|
|
let error_msg = result.unwrap_err().to_string();
|
|
assert!(
|
|
error_msg.to_lowercase().contains("position") || error_msg.to_lowercase().contains("limit"),
|
|
"Error should mention position limit, got: {}",
|
|
error_msg
|
|
);
|
|
}
|
|
|
|
// ============================================================================
|
|
// TEST 8: Model Comparison Across 4 Models (RED Phase)
|
|
// ============================================================================
|
|
|
|
#[tokio::test]
|
|
#[ignore = "RED: This test will fail until implementation is complete"]
|
|
async fn test_ml_model_comparison() {
|
|
// RED: Test comparing performance across 4 models
|
|
let pool = get_test_db_pool().await;
|
|
|
|
// Execute trades with each model individually
|
|
for model in &["DQN", "PPO", "MAMBA2", "TFT"] {
|
|
let ensemble = create_single_model_coordinator(model);
|
|
let mut executor = PaperTradingExecutor::new_with_ml(pool.clone(), ensemble)
|
|
.await
|
|
.expect("Failed to create executor");
|
|
|
|
for _ in 0..5 {
|
|
let market_data = load_test_ohlcv_data("ES.FUT", 50);
|
|
let signal = executor
|
|
.generate_ml_signal(&market_data)
|
|
.await
|
|
.expect("Failed to generate signal");
|
|
|
|
let order = executor
|
|
.execute_ml_signal(&signal, "ES.FUT")
|
|
.await
|
|
.expect("Failed to execute signal");
|
|
|
|
// Random outcome for testing
|
|
let pnl = if rand::random::<f64>() > 0.5 {
|
|
100.0
|
|
} else {
|
|
-50.0
|
|
};
|
|
executor
|
|
.record_outcome(order.id, pnl)
|
|
.await
|
|
.expect("Failed to record outcome");
|
|
}
|
|
}
|
|
|
|
// Compare model performance
|
|
let metrics_store = MLMetricsStore::new(pool);
|
|
let comparison = metrics_store
|
|
.compare_model_accuracy()
|
|
.await
|
|
.expect("Failed to compare model accuracy");
|
|
|
|
assert_eq!(comparison.len(), 4, "Should have all 4 models");
|
|
|
|
// Models should be ranked by accuracy
|
|
for i in 1..comparison.len() {
|
|
assert!(
|
|
comparison[i - 1].1 >= comparison[i].1,
|
|
"Models should be sorted by accuracy"
|
|
);
|
|
}
|
|
}
|
|
|
|
// ============================================================================
|
|
// TEST 9: Position Sizing Based on Confidence (RED Phase)
|
|
// ============================================================================
|
|
|
|
#[tokio::test]
|
|
#[ignore = "RED: This test will fail until implementation is complete"]
|
|
async fn test_position_sizing_confidence_mapping() {
|
|
// RED: Test position sizing scales with confidence
|
|
let pool = get_test_db_pool().await;
|
|
let ensemble = create_test_ensemble();
|
|
|
|
let executor = PaperTradingExecutor::new_with_ml(pool, ensemble)
|
|
.await
|
|
.expect("Failed to create executor");
|
|
|
|
use trading_service::paper_trading_executor::{Action, SignalSource, TradingSignal};
|
|
|
|
// High confidence signal (0.9)
|
|
let high_conf_signal = TradingSignal {
|
|
action: Some(Action::Buy),
|
|
confidence: 0.9,
|
|
source: SignalSource::ML,
|
|
model_votes: None,
|
|
};
|
|
|
|
// Low confidence signal (0.6)
|
|
let low_conf_signal = TradingSignal {
|
|
action: Some(Action::Buy),
|
|
confidence: 0.6,
|
|
source: SignalSource::ML,
|
|
model_votes: None,
|
|
};
|
|
|
|
// Convert both to orders
|
|
let high_conf_order = executor
|
|
.convert_signal_to_order(&high_conf_signal, "ES.FUT")
|
|
.await
|
|
.expect("High confidence order failed");
|
|
|
|
let low_conf_order = executor
|
|
.convert_signal_to_order(&low_conf_signal, "ES.FUT")
|
|
.await
|
|
.expect("Low confidence order failed");
|
|
|
|
// Higher confidence should result in larger position
|
|
assert!(
|
|
high_conf_order.quantity > low_conf_order.quantity,
|
|
"High confidence ({}) should have larger position than low confidence ({})",
|
|
high_conf_order.quantity,
|
|
low_conf_order.quantity
|
|
);
|
|
}
|