#!/usr/bin/env python3
"""
导出 V2 框架回测逐日明细到 JSON（简化版）

现在直接调用 strategy.run(export_detail=True)
不再重复执行策略逻辑

用法:
    python framework_v2/scripts/export_backtest_detail.py
"""

import sys
from pathlib import Path

project_root = Path(__file__).parent.parent.parent
sys.path.insert(0, str(project_root))

from dotenv import load_dotenv
load_dotenv()

from framework_v2.config import load_config
from framework_v2.strategies.rotation.rotation import GlobalRotationStrategy


def main():
    print("=" * 80)
    print("   V2 回测逐日明细导出（GlobalRotationStrategy）")
    print("=" * 80)
    
    # 1. 加载配置
    config_file = project_root / 'framework_v2' / 'strategies' / 'rotation' / 'config_simple.yaml'
    print(f"\n[1] 加载配置: {config_file}")
    config = load_config(str(config_file))
    
    # 2. 初始化策略
    print("[2] 初始化策略...")
    strategy = GlobalRotationStrategy(config)
    
    # 3. 运行策略并导出明细
    output_path = project_root / 'framework_v2' / 'results' / 'backtest_detail_v2.json'
    
    print("[3] 运行策略并导出明细...")
    result = strategy.run(
        export_detail=True,
        detail_path=str(output_path)
    )
    
    # 4. 打印汇总
    print("\n" + "=" * 80)
    print("   回测汇总")
    print("=" * 80)
    print(f"  总收益: {result['metrics']['total_return'] * 100:.2f}%")
    print(f"  年化收益: {result['metrics']['annual_return'] * 100:.2f}%")
    print(f"  最大回撤: {result['metrics']['max_drawdown'] * 100:.2f}%")
    print(f"  夏普比率: {result['metrics']['sharpe_ratio']:.2f}")
    print(f"  调仓次数: {result['metrics']['rebalance_count']}")
    print(f"  交易天数: {result['metrics']['n_days']}")
    print(f"  输出文件: {output_path}")


if __name__ == '__main__':
    main()
