Files
NewQuant/strategy_manager/start.py
liaozhaorun 711b86d33f 1、新增傅里叶策略
2、新增策略管理、策略重启功能
2025-11-20 16:15:45 +08:00

81 lines
2.7 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.
import sys
import argparse
from pathlib import Path
from core.manager import StrategyManager, print_status_table
def main():
parser = argparse.ArgumentParser(
description="策略管理系统 - 批量管理你的交易策略",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
示例:
# 查看所有策略状态
python start.py status
# 启动所有策略
python start.py start --all
# 启动单个策略(使用标识符)
python start.py start -n DualModeTrendlineHawkesStrategy2_FG
# 停止单个策略
python start.py stop -n DualModeTrendlineHawkesStrategy2_FG
# 重启策略
python start.py restart -n DualModeTrendlineHawkesStrategy2_FG
# 查看日志最近50行
python start.py logs -n DualModeTrendlineHawkesStrategy2_FG -t 50
"""
)
parser.add_argument("action", choices=["start", "stop", "restart", "status", "logs"])
parser.add_argument("-n", "--name", help="策略标识符策略名_品种")
parser.add_argument("-a", "--all", action="store_true", help="对所有策略执行操作")
parser.add_argument("-c", "--config", default="config/main.json", help="主配置文件路径")
parser.add_argument("-t", "--tail", type=int, default=30, help="查看日志末尾行数")
args = parser.parse_args()
manager = StrategyManager(args.config)
if args.action == "status":
status = manager.get_status()
print_status_table(status)
elif args.action in ["start", "stop"]:
if args.all:
manager.start_all() if args.action == "start" else manager.stop_all()
elif args.name:
manager.start_strategy(args.name) if args.action == "start" else manager.stop_strategy(args.name)
else:
print("❌ 错误: 请指定策略名称 (-n) 或使用 --all")
sys.exit(1)
elif args.action == "restart":
if not args.name:
print("❌ 错误: 重启操作必须指定策略名称 (-n)")
sys.exit(1)
manager.restart_strategy(args.name)
elif args.action == "logs":
if not args.name:
print("❌ 错误: 查看日志必须指定策略名称 (-n)")
sys.exit(1)
log_file = Path("logs") / f"{args.name}.log"
if not log_file.exists():
print(f"⚠️ 日志文件不存在: {log_file}")
return
print(f"\n📄 {args.name} 的日志 (最近{args.tail}行):")
print("=" * 80)
with open(log_file, 'r') as f:
lines = f.readlines()[-args.tail:]
for line in lines:
print(line.rstrip())
print("=" * 80)
if __name__ == "__main__":
main()