# final_integration_test.py - Final Integration Verification import sys import os import pandas as pd # Add project root to path project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.append(project_root) def test_api_integration(): """Test that the API integration works correctly""" print("šŸ”¬ Final Integration Test - API & Enhanced Engine") print("=" * 60) # Import the API route function directly from core.routes.api_backtest import save_backtest_result from core.backtesting.enhanced_engine import run_enhanced_backtest # Test data test_file = 'EURUSD_16385_data.csv' if not os.path.exists(test_file): print(f"āŒ Test file {test_file} not found") return False # Load test data df = pd.read_csv(test_file).tail(200) # Small sample for quick test print(f"šŸ“Š Testing with {len(df)} data points from {test_file}") # Simulate web interface parameters (what user would send) web_params = { 'lot_size': 1.5, # This gets mapped to risk_percent 'sl_pips': 2.5, # This gets mapped to sl_atr_multiplier 'tp_pips': 5.0 # This gets mapped to tp_atr_multiplier } print(f"šŸ”„ Web interface parameters: {web_params}") # Simulate the API parameter mapping (like the web interface does) enhanced_params = web_params.copy() enhanced_params['risk_percent'] = float(web_params['lot_size']) enhanced_params['sl_atr_multiplier'] = float(web_params['sl_pips']) enhanced_params['tp_atr_multiplier'] = float(web_params['tp_pips']) print(f"šŸ”„ Mapped parameters: {enhanced_params}") # Simulate engine config (like the API sets) engine_config = { 'enable_spread_costs': True, 'enable_slippage': True, 'enable_realistic_execution': True } # Extract symbol name (like the API does) symbol_name = test_file.replace('.csv', '').split('_')[0].upper() print(f"šŸŽÆ Detected symbol: {symbol_name}") # Run enhanced backtest (like the API does) print(f"\nšŸš€ Running enhanced backtest...") try: results = run_enhanced_backtest( 'MA_CROSSOVER', enhanced_params, df, symbol_name=symbol_name, engine_config=engine_config ) if 'error' in results: print(f"āŒ Backtest error: {results['error']}") return False print(f"āœ… Backtest successful!") print(f" šŸ’° Gross Profit: ${results.get('total_profit_usd', 0):.2f}") print(f" šŸ’ø Spread Costs: ${results.get('total_spread_costs', 0):.2f}") print(f" šŸ’µ Net Profit: ${results.get('net_profit_after_costs', 0):.2f}") print(f" šŸ“Š Total Trades: {results.get('total_trades', 0)}") print(f" šŸ“ˆ Win Rate: {results.get('win_rate_percent', 0):.1f}%") # Check enhanced engine features engine_config_result = results.get('engine_config', {}) print(f"\nšŸ”§ Enhanced Engine Features:") print(f" āœ… Spread costs enabled: {engine_config_result.get('spread_costs_enabled', False)}") print(f" āœ… Slippage enabled: {engine_config_result.get('slippage_enabled', False)}") print(f" āœ… Realistic execution: {engine_config_result.get('realistic_execution', False)}") # Check instrument config inst_config = engine_config_result.get('instrument_config', {}) print(f" šŸ”’ Max risk: {inst_config.get('max_risk_percent', 'N/A')}%") print(f" šŸ“ Max lot: {inst_config.get('max_lot_size', 'N/A')}") print(f" šŸ’ø Spread: {inst_config.get('typical_spread_pips', 'N/A')} pips") except Exception as e: print(f"āŒ Backtest exception: {e}") return False # Test database save function (like the API does) print(f"\nšŸ’¾ Testing database save...") try: strategy_name = results.get('strategy_name', 'MA_CROSSOVER') filename = test_file # This calls the same function the API uses save_backtest_result(strategy_name, filename, web_params, results) print(f"āœ… Database save successful") except Exception as e: print(f"āŒ Database save error: {e}") return False # Verify results contain all expected enhanced fields print(f"\nšŸ” Verifying enhanced results format...") required_fields = [ 'total_profit_usd', 'total_spread_costs', 'net_profit_after_costs', 'instrument', 'engine_config', 'wins', 'losses', 'total_trades' ] missing_fields = [field for field in required_fields if field not in results] if missing_fields: print(f"āŒ Missing required fields: {missing_fields}") return False else: print(f"āœ… All required enhanced fields present") # Check that spread costs are realistic spread_costs = results.get('total_spread_costs', 0) total_trades = results.get('total_trades', 0) if total_trades > 0 and spread_costs <= 0: print(f"āš ļø Warning: No spread costs despite {total_trades} trades") elif total_trades > 0: avg_spread_cost = spread_costs / total_trades print(f"āœ… Realistic spread costs: ${avg_spread_cost:.2f} per trade") print(f"\nšŸŽ‰ Final Integration Test: PASSED") print(f"\nšŸ“‹ Summary:") print(f" āœ… Enhanced engine integrated correctly") print(f" āœ… Parameter mapping works") print(f" āœ… Database integration functional") print(f" āœ… Spread costs calculated") print(f" āœ… Instrument protection applied") print(f" āœ… Web interface compatibility maintained") return True if __name__ == "__main__": # Change to lab directory lab_dir = os.path.dirname(os.path.abspath(__file__)) os.chdir(lab_dir) if test_api_integration(): print(f"\nšŸŽ‰ ALL TESTS PASSED - Integration Complete!") print(f"\nšŸš€ Enhanced Backtesting Engine is fully integrated and ready!") else: print(f"\nāŒ Some tests failed - check integration")