""" 简单回测演示 使用模拟数据运行均线交叉策略 """ import sys import os import logging # 添加当前目录到路径 current_dir = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, current_dir) # 配置日志 logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s') def simple_demo(): """简单演示""" try: print("🚀 QTrade 简化回测演示") print("="*50) # 导入模块 from simple_backtest_engine import SimpleBacktestEngine from ma_cross_strategy import MACrossStrategy # 创建回测引擎 engine = SimpleBacktestEngine(initial_capital=1000000.0) # 设置股票池 stock_codes = ["STOCK_A", "STOCK_B"] engine.set_universe(stock_codes) # 设置回测期间 engine.set_period("2024-01-01", "2024-12-31") # 创建均线交叉策略 strategy_params = { 'short_period': 5, # 5日均线 'long_period': 20, # 20日均线 'stop_loss': 0.05, # 5%止损 'take_profit': 0.15, # 15%止盈 'position_size': 0.3 # 每次30%仓位 } strategy = MACrossStrategy(strategy_params) engine.set_strategy(strategy) # 进度回调函数 def progress_callback(current, total, message): progress = (current / total) * 100 print(f"\r进度: {progress:.1f}% - {message}", end="", flush=True) if current == total: print() # 运行回测 print("\n🔄 开始执行回测...") success = engine.run_backtest(progress_callback) if success: print("\n✅ 回测完成") # 显示结果 print("\n📊 回测结果:") engine.print_summary() return True else: print("\n❌ 回测失败") return False except Exception as e: print(f"❌ 演示失败: {e}") import traceback traceback.print_exc() return False if __name__ == "__main__": simple_demo()