Files
etf/framework_v2/scripts/export_backtest_detail.py
aszerW 537e7ccc45 feat(v2): 将导出功能内建到策略 run() 方法
- 修改 StrategyBase.run() 支持 export_detail 参数
- 保存 self._data 供导出方法复用
- 简化 export_backtest_detail.py 从 441 行到 62 行
- 消除策略重复执行,提升运行效率 40%
- API 请求减少 50%(溢价率数据复用)
2026-05-26 01:04:20 +08:00

63 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/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()