feat(backend): Phase 2 研究引擎 — ResearchSpec / 因子 / 评估 / 低频回测 / 引擎抽象

- domain:ResearchSpec(universe/factors/selection/rebalance/costs 校验)+ 标准化 BacktestResult / FactorTestReport
- 因子引擎:注册表 + 元数据,内置 9 个行情因子(momentum/volatility/量比/乖离/反转),支持自定义注册;只用行情字段规避未来函数
- 评估:横截面 IC / RankIC(rank+pearson 免 scipy)/ ICIR / 分层收益
- 回测:TopK 等权低频,无未来函数记账(t 收盘成交、自 t+1 计收益),成本/涨跌停/停牌约束,未建模项显式写入 unimplemented(AGENT §24)
- 引擎抽象 QuantEngine + LocalEngine(pandas 默认实现);qlib_adapter 桥接占位 —— pyqlib 无 aarch64+cp312 wheel(ROADMAP 已备注)
- 真实链路冒烟:600519 2024 月度动量回测闭环产出标准结果
- 测试 60 passed / ruff clean
This commit is contained in:
Simon
2026-09-06 17:08:00 +08:00
parent 2da234220a
commit e9f59d3cf8
12 changed files with 1388 additions and 2 deletions
+39
View File
@@ -0,0 +1,39 @@
"""Qlib 后端引擎(桥接占位)。
当前开发平台为 Linux aarch64 + CPython 3.12:pyqlib 官方仅提供 x86_64 /
macOS / Windows 且最高 Python 3.8 的 wheel(实测 uv 解析不可满足),因此
Qlib 实现在此平台无法安装运行。
受支持平台(如 x86_64 Linux + Python 3.11)启用方式:
cd backend && uv pip install pyqlib==0.9.7
将本模块替换为真实实现:Parquet/本地行情 → QlibDataset → Alpha158 →
LightGBM 训练/预测 → 回测,并归一化为 domain.entities.research 输出。
业务层经 app.quant.engine.QuantEngine Protocol 注入引擎,切换无需改业务代码。
"""
from __future__ import annotations
import pandas as pd
from app.domain.entities.research import BacktestResult, FactorTestReport, ResearchSpec
from app.quant.engine import QuantEngine
_MSG = (
"Qlib 引擎需要 pyqlib,当前平台(Linux aarch64 + Python 3.12)无可用 wheel。"
"请改用 LocalEngine 或在受支持平台安装 pyqlib 后实现(见本模块 docstring)。"
)
class QlibEngine(QuantEngine):
"""pyqlib 后端占位:抛 NotImplementedError 并给出启用指引。"""
name = "qlib"
def run_factor_test(
self, daily: pd.DataFrame, spec: ResearchSpec, horizon_days: int = 21
) -> FactorTestReport:
raise NotImplementedError(_MSG)
def run_backtest(self, daily: pd.DataFrame, spec: ResearchSpec) -> BacktestResult:
raise NotImplementedError(_MSG)