重构 RotationStrategy:
- 添加 from_yaml() 从配置创建实例
- 添加 get_data() 获取数据
- 添加 compute_factors() 计算因子
- 添加 generate_signals() 生成信号
- 添加 run_backtest() 完整回测流程
简化 run_rotation.py:
- 从 264 行简化为 9 行
- 只做策略调用入口
执行方式:
python run_rotation.py --config config/strategies/rotation.yaml
python run_rotation.py --save-path results/my_rotation
代码方式:
strategy = RotationStrategy.from_yaml('config/strategies/rotation.yaml')
result = strategy.run_backtest()
49 lines
1.1 KiB
Python
49 lines
1.1 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
ETF轮动策略回测入口
|
|
|
|
用法:
|
|
python run_rotation.py
|
|
python run_rotation.py --config config/strategies/rotation.yaml
|
|
python run_rotation.py --save-path results/my_rotation
|
|
"""
|
|
|
|
import argparse
|
|
import time
|
|
from datetime import datetime
|
|
|
|
from strategies.rotation.strategy import RotationStrategy
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description="ETF轮动策略回测")
|
|
parser.add_argument(
|
|
"--config",
|
|
type=str,
|
|
default="config/strategies/rotation.yaml",
|
|
help="配置文件路径",
|
|
)
|
|
parser.add_argument(
|
|
"--save-path",
|
|
type=str,
|
|
default="results/rotation",
|
|
help="报告保存路径前缀",
|
|
)
|
|
args = parser.parse_args()
|
|
|
|
start_time = time.time()
|
|
|
|
# 从配置创建策略
|
|
strategy = RotationStrategy.from_yaml(args.config)
|
|
|
|
# 运行回测
|
|
result = strategy.run_backtest(save_path=args.save_path)
|
|
|
|
elapsed = time.time() - start_time
|
|
print(f"\n总耗时: {elapsed:.1f}秒")
|
|
|
|
return result
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |