refactor: 归档旧代码,保留新框架结构
归档内容: - core/ (数据源、因子计算、通用工具) → archive/legacy_core/ - strategies/rotation/engine.py, portfolio.py, report.py → archive/legacy_core/ - scripts/ (run_rotation, daily_scheduler) → archive/legacy_scripts/ - examples/ → archive/legacy_examples/ - tests/ (实验、对比测试) → archive/legacy_tests/ - 单独文件 (fetch_*.py, 动量.py, 全球市场.py等) → archive/single_files/ 保留新结构: - framework/ (抽象接口) - strategies/shared/ (定制组件) - strategies/rotation/strategy.py (新策略) - 外层配置: .env, .dockerignore, build-and-push.sh, hk_ecs.pem, README.md, requirements.txt - Docker相关: Dockerfile, Dockerfile_base, docker-compose.yml 更新README反映新框架架构
This commit is contained in:
@@ -1,302 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
ETF策略每日定时任务
|
||||
|
||||
功能:
|
||||
1. 每天15:00收盘后检查是否为交易日
|
||||
2. 如果是交易日,执行策略回测
|
||||
3. 上传报告图表到OSS
|
||||
4. 发送结果到钉钉
|
||||
|
||||
用法:
|
||||
python scripts/daily_scheduler.py
|
||||
python scripts/daily_scheduler.py --time 15:00
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
import time
|
||||
import argparse
|
||||
import subprocess
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
|
||||
# 添加项目根目录到路径
|
||||
project_root = Path(__file__).parent.parent
|
||||
sys.path.insert(0, str(project_root))
|
||||
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv(project_root / ".env")
|
||||
|
||||
from loguru import logger
|
||||
import schedule
|
||||
import tushare as ts
|
||||
from core.common.notify import DingTalkBot, send_to_all_groups
|
||||
from core.common.oss_utils import upload_image_to_oss
|
||||
|
||||
# 配置日志
|
||||
logger.add(
|
||||
project_root / "logs" / "scheduler_{time}.log",
|
||||
rotation="1 day",
|
||||
retention="7 days",
|
||||
level="INFO",
|
||||
)
|
||||
|
||||
|
||||
def is_trading_day(date_str: str = None) -> bool:
|
||||
"""
|
||||
检查指定日期是否为交易日
|
||||
|
||||
Args:
|
||||
date_str: 日期字符串 (YYYYMMDD),默认今天
|
||||
|
||||
Returns:
|
||||
bool: 是否为交易日
|
||||
"""
|
||||
if date_str is None:
|
||||
date_str = datetime.now().strftime("%Y%m%d")
|
||||
|
||||
try:
|
||||
token = os.getenv("TUSHARE_TOKEN")
|
||||
if not token:
|
||||
logger.error("TUSHARE_TOKEN 未配置")
|
||||
return False
|
||||
|
||||
pro = ts.pro_api(token)
|
||||
df = pro.trade_cal(
|
||||
exchange="SSE", start_date=date_str, end_date=date_str, is_open="1"
|
||||
)
|
||||
|
||||
is_open = len(df) > 0 and df.iloc[0]["is_open"] == 1
|
||||
logger.info(f"日期 {date_str} 是否为交易日: {is_open}")
|
||||
return is_open
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"检查交易日失败: {e}")
|
||||
# 失败时默认执行(避免错过交易日)
|
||||
return True
|
||||
|
||||
|
||||
def run_strategy(config_path: str = "config/strategies/rotation.yaml") -> dict:
|
||||
"""
|
||||
执行策略回测
|
||||
|
||||
Args:
|
||||
config_path: 配置文件路径
|
||||
|
||||
Returns:
|
||||
dict: 执行结果,包含报告路径等信息
|
||||
"""
|
||||
logger.info("开始执行策略回测...")
|
||||
|
||||
try:
|
||||
# 构建命令
|
||||
cmd = [
|
||||
sys.executable,
|
||||
str(project_root / "scripts" / "run_rotation.py"),
|
||||
"--config",
|
||||
config_path,
|
||||
"--save-path",
|
||||
f"results/report_{datetime.now().strftime('%Y%m%d')}",
|
||||
]
|
||||
|
||||
logger.info(f"执行命令: {' '.join(cmd)}")
|
||||
|
||||
# 执行策略
|
||||
result = subprocess.run(
|
||||
cmd,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
cwd=project_root,
|
||||
timeout=300, # 5分钟超时
|
||||
)
|
||||
|
||||
if result.returncode != 0:
|
||||
logger.error(f"策略执行失败:\n{result.stderr}")
|
||||
return {"success": False, "error": result.stderr}
|
||||
|
||||
logger.info("策略执行成功")
|
||||
logger.debug(result.stdout)
|
||||
|
||||
# 查找生成的报告文件
|
||||
report_date = datetime.now().strftime("%Y%m%d")
|
||||
chart_path = project_root / "results" / f"report_{report_date}_chart.png"
|
||||
|
||||
# 如果找不到带日期的,尝试默认路径
|
||||
if not chart_path.exists():
|
||||
chart_path = project_root / "results" / "report_chart.png"
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"stdout": result.stdout,
|
||||
"chart_path": str(chart_path) if chart_path.exists() else None,
|
||||
}
|
||||
|
||||
except subprocess.TimeoutExpired:
|
||||
logger.error("策略执行超时")
|
||||
return {"success": False, "error": "timeout"}
|
||||
except Exception as e:
|
||||
logger.error(f"策略执行异常: {e}")
|
||||
return {"success": False, "error": str(e)}
|
||||
|
||||
|
||||
def send_report_to_dingtalk(chart_path: str, summary_text: str = "") -> bool:
|
||||
"""
|
||||
上传报告到OSS并发送到钉钉
|
||||
|
||||
Args:
|
||||
chart_path: 图表文件路径
|
||||
summary_text: 摘要文本
|
||||
|
||||
Returns:
|
||||
bool: 是否发送成功
|
||||
"""
|
||||
logger.info("开始发送报告到钉钉...")
|
||||
|
||||
try:
|
||||
bot = DingTalkBot()
|
||||
|
||||
if not bot.webhook:
|
||||
logger.error("钉钉未配置,无法发送")
|
||||
return False
|
||||
|
||||
today_str = datetime.now().strftime("%Y-%m-%d")
|
||||
|
||||
# 向所有群发送图文消息
|
||||
success = send_to_all_groups(
|
||||
"send_image_via_oss",
|
||||
image_path=chart_path,
|
||||
title=f"ETF轮动策略调仓日报 ({today_str})",
|
||||
text=summary_text,
|
||||
expire_days=7,
|
||||
)
|
||||
|
||||
if success:
|
||||
logger.info("报告发送成功")
|
||||
else:
|
||||
logger.error("报告发送失败")
|
||||
|
||||
return success
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"发送报告异常: {e}")
|
||||
return False
|
||||
|
||||
|
||||
def setup_schedule(
|
||||
target_time: str = "15:30", config_path: str = "config/strategies/rotation.yaml"
|
||||
):
|
||||
"""
|
||||
设置定时任务
|
||||
|
||||
Args:
|
||||
target_time: 执行时间 (HH:MM)
|
||||
config_path: 配置文件路径
|
||||
"""
|
||||
logger.info(f"设置定时任务: 每天 {target_time} 执行")
|
||||
|
||||
# 清除所有现有任务
|
||||
schedule.clear()
|
||||
|
||||
# 添加每日任务
|
||||
schedule.every().day.at(target_time).do(daily_task, config_path)
|
||||
|
||||
logger.info("定时任务设置完成,等待执行...")
|
||||
|
||||
|
||||
def daily_task(config_path: str = "config/strategies/rotation.yaml"):
|
||||
"""
|
||||
每日任务主流程
|
||||
|
||||
Args:
|
||||
config_path: 配置文件路径
|
||||
"""
|
||||
today_str = datetime.now().strftime("%Y-%m-%d")
|
||||
logger.info(f"=" * 60)
|
||||
logger.info(f"开始执行每日任务: {today_str}")
|
||||
logger.info(f"=" * 60)
|
||||
|
||||
# 1. 检查是否为交易日
|
||||
if not is_trading_day():
|
||||
logger.info("今天不是交易日,跳过执行")
|
||||
return
|
||||
|
||||
# 2. 执行策略
|
||||
result = run_strategy(config_path)
|
||||
|
||||
if not result["success"]:
|
||||
# 发送错误通知
|
||||
# 发送错误通知到所有群
|
||||
send_to_all_groups("send_text", content=f"策略执行失败: {result.get('error', '未知错误')}")
|
||||
return
|
||||
|
||||
# 3. 发送报告
|
||||
if result.get("chart_path"):
|
||||
send_report_to_dingtalk(
|
||||
chart_path=result["chart_path"], summary_text=""
|
||||
)
|
||||
else:
|
||||
logger.warning("未找到报告图表")
|
||||
|
||||
logger.info("每日任务完成")
|
||||
|
||||
|
||||
def run_scheduler_loop():
|
||||
"""运行调度器循环"""
|
||||
logger.info("启动调度器循环,按 Ctrl+C 停止")
|
||||
try:
|
||||
while True:
|
||||
schedule.run_pending()
|
||||
time.sleep(1)
|
||||
except KeyboardInterrupt:
|
||||
logger.info("调度器已停止")
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="ETF策略每日定时任务")
|
||||
parser.add_argument(
|
||||
"--time",
|
||||
type=str,
|
||||
default="09:00",
|
||||
help="执行时间 (HH:MM),默认09:00",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--config",
|
||||
type=str,
|
||||
default="config/strategies/rotation.yaml",
|
||||
help="配置文件路径",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--run-now",
|
||||
action="store_true",
|
||||
help="立即执行一次并退出(不启动定时任务)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--no-daemon",
|
||||
action="store_true",
|
||||
help="非后台模式:执行一次后进入定时循环",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
# 创建日志目录
|
||||
(project_root / "logs").mkdir(exist_ok=True)
|
||||
|
||||
if args.run_now:
|
||||
# 立即执行一次并退出
|
||||
daily_task(args.config)
|
||||
elif args.no_daemon:
|
||||
# 非后台模式:执行一次后进入定时循环
|
||||
setup_schedule(args.time, args.config)
|
||||
logger.info("执行一次任务用于测试...")
|
||||
daily_task(args.config)
|
||||
logger.info("测试完成,启动定时任务循环(按 Ctrl+C 停止)...")
|
||||
run_scheduler_loop()
|
||||
else:
|
||||
# 默认:后台daemon模式
|
||||
setup_schedule(args.time, args.config)
|
||||
run_scheduler_loop()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,85 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
CCI技术指标筛选器入口
|
||||
|
||||
用法:
|
||||
python scripts/run_cci_screener.py
|
||||
python scripts/run_cci_screener.py --config config/strategies/cci.yaml
|
||||
python scripts/run_cci_screener.py --schedule # 定时模式
|
||||
"""
|
||||
|
||||
import sys
|
||||
import time
|
||||
import yaml
|
||||
import argparse
|
||||
import schedule
|
||||
from pathlib import Path
|
||||
from datetime import datetime
|
||||
|
||||
# 添加项目根目录到路径
|
||||
project_root = Path(__file__).parent.parent
|
||||
sys.path.insert(0, str(project_root))
|
||||
|
||||
from strategies.screener.cci import CCIScreener
|
||||
|
||||
|
||||
def load_config(config_path: str) -> dict:
|
||||
"""加载配置文件"""
|
||||
with open(config_path, "r", encoding="utf-8") as f:
|
||||
return yaml.safe_load(f)
|
||||
|
||||
|
||||
def run_screening(config: dict):
|
||||
"""执行一次筛选"""
|
||||
print(f"\n[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] 开始CCI筛选...")
|
||||
|
||||
screener = CCIScreener(config)
|
||||
signals = screener.run_screening()
|
||||
|
||||
return signals
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="CCI技术指标筛选")
|
||||
parser.add_argument(
|
||||
"--config",
|
||||
type=str,
|
||||
default="config/strategies/cci.yaml",
|
||||
help="配置文件路径",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--schedule",
|
||||
action="store_true",
|
||||
help="启用定时模式",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
# 加载配置
|
||||
config = load_config(args.config)
|
||||
print("=" * 60)
|
||||
print(" CCI技术指标筛选器")
|
||||
print("=" * 60)
|
||||
print(f"\n配置文件: {args.config}")
|
||||
print(f"日线周期: {config.get('day_period', 14)}")
|
||||
print(f"周线周期: {config.get('week_period', 14)}")
|
||||
print(f"筛选阈值: {config.get('threshold', -100)}")
|
||||
print(f"数据源: {config.get('data_source', 'postgresql')}")
|
||||
|
||||
if args.schedule:
|
||||
# 定时模式
|
||||
schedule_time = config.get("schedule_time", "19:00")
|
||||
print(f"\n定时模式已启用,每天 {schedule_time} 执行")
|
||||
print("按 Ctrl+C 停止\n")
|
||||
|
||||
schedule.every().day.at(schedule_time).do(run_screening, config)
|
||||
|
||||
while True:
|
||||
schedule.run_pending()
|
||||
time.sleep(1)
|
||||
else:
|
||||
# 单次执行
|
||||
run_screening(config)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,151 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
ETF轮动策略回测入口
|
||||
|
||||
用法:
|
||||
python scripts/run_rotation.py
|
||||
python scripts/run_rotation.py --config config/strategies/rotation.yaml
|
||||
"""
|
||||
|
||||
import sys
|
||||
import time
|
||||
import yaml
|
||||
import argparse
|
||||
from pathlib import Path
|
||||
|
||||
# 添加项目根目录到路径
|
||||
project_root = Path(__file__).parent.parent
|
||||
sys.path.insert(0, str(project_root))
|
||||
|
||||
from strategies.rotation.engine import RotationStrategy
|
||||
from strategies.rotation.portfolio import track_positions, save_trades
|
||||
from strategies.rotation.report import generate_performance_report
|
||||
from config.settings import DEFAULT_CODE_NAME_MAP, DEFAULT_BENCHMARK_NAME
|
||||
|
||||
|
||||
def load_config(config_path: str) -> dict:
|
||||
"""加载配置文件"""
|
||||
with open(config_path, "r", encoding="utf-8") as f:
|
||||
return yaml.safe_load(f)
|
||||
|
||||
|
||||
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/report",
|
||||
help="报告保存路径前缀",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
start_time = time.time()
|
||||
|
||||
print("=" * 60)
|
||||
print(" ETF轮动策略 回测系统")
|
||||
print("=" * 60)
|
||||
|
||||
# 加载配置
|
||||
config = load_config(args.config)
|
||||
|
||||
# 如果未设置 end_date,默认使用最新日期
|
||||
if not config.get('end_date'):
|
||||
from datetime import datetime
|
||||
config['end_date'] = datetime.now().strftime('%Y-%m-%d')
|
||||
|
||||
# 从配置中读取 code_list(新的配置格式:{代码: {name, etf, market}})
|
||||
code_list_config = config.get('code_list', {})
|
||||
|
||||
# 提取代码列表和名称映射
|
||||
if isinstance(code_list_config, dict):
|
||||
code_list = list(code_list_config.keys())
|
||||
# 构建 code_name_map: {代码: 名称}
|
||||
code_name_map = {}
|
||||
for code, cfg in code_list_config.items():
|
||||
if isinstance(cfg, dict):
|
||||
code_name_map[code] = cfg.get('name', code)
|
||||
else:
|
||||
# 兼容旧格式
|
||||
code_name_map[code] = cfg
|
||||
else:
|
||||
# 兼容旧格式(列表)
|
||||
code_list = code_list_config
|
||||
code_name_map = DEFAULT_CODE_NAME_MAP
|
||||
code_list_config = {}
|
||||
|
||||
benchmark_config = config.get('benchmark', {})
|
||||
benchmark_name = benchmark_config.get('name', DEFAULT_BENCHMARK_NAME)
|
||||
|
||||
print(f"\n配置文件: {args.config}")
|
||||
print(f"候选标的: {len(code_list)} 只")
|
||||
|
||||
# 统计ETF映射情况
|
||||
etf_count = sum(1 for cfg in code_list_config.values() if isinstance(cfg, dict) and cfg.get('etf'))
|
||||
crypto_count = sum(1 for cfg in code_list_config.values() if isinstance(cfg, dict) and cfg.get('market') == 'CRYPTO')
|
||||
print(f" - ETF映射: {etf_count} 只")
|
||||
print(f" - 直接交易: {crypto_count} 只(加密货币)")
|
||||
|
||||
print(f"回测区间: {config['start_date']} ~ {config['end_date']}")
|
||||
print(f"因子类型: {config['factor_type']}")
|
||||
print(f"窗口天数: {config['n_days']}")
|
||||
print(f"选中数量: {config['select_num']}")
|
||||
print(f"调仓周期: {config['rebalance_days']} 天")
|
||||
print(f"交易成本: {config['trade_cost']:.2%}")
|
||||
|
||||
# 保持 config 中的 code_list 为完整配置格式(用于引擎内部解析)
|
||||
# 不需要修改 config['code_list'],引擎会直接使用原始配置
|
||||
|
||||
# 创建策略实例
|
||||
strategy = RotationStrategy(config)
|
||||
|
||||
# 运行回测
|
||||
print("\n" + "=" * 60)
|
||||
print("开始回测...")
|
||||
print("=" * 60)
|
||||
|
||||
backtest_result = strategy.run()
|
||||
|
||||
# 持仓跟踪
|
||||
print("\n" + "=" * 60)
|
||||
print("持仓跟踪...")
|
||||
print("=" * 60)
|
||||
|
||||
trades_df, summary_df = track_positions(
|
||||
backtest_result,
|
||||
code_name_map=code_name_map,
|
||||
select_num=config["select_num"],
|
||||
)
|
||||
save_trades(trades_df, summary_df, save_path=args.save_path)
|
||||
|
||||
# 生成绩效报告
|
||||
print("\n" + "=" * 60)
|
||||
print("生成绩效报告...")
|
||||
print("=" * 60)
|
||||
|
||||
metrics = generate_performance_report(
|
||||
backtest_result,
|
||||
strategy.valid_codes,
|
||||
code_name_map=code_name_map,
|
||||
benchmark_name=benchmark_name,
|
||||
save_path=args.save_path,
|
||||
select_num=config["select_num"],
|
||||
code_config=code_list_config, # 传入完整配置以显示ETF映射
|
||||
index_data=strategy.index_data, # 传入指数数据
|
||||
etf_price_data=strategy.etf_data, # 传入ETF价格数据
|
||||
etf_nav_data_raw=strategy.etf_nav_data, # 传入ETF净值数据
|
||||
)
|
||||
|
||||
elapsed = time.time() - start_time
|
||||
print(f"\n总耗时: {elapsed:.1f}秒")
|
||||
|
||||
return metrics
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user