feat(backend): 策略库重构为「选股策略 + 公共配置 + 回测组合」三件套
按用户目标把原来「一个策略 = 全套参数」拆开(已确认的设计决策):
- 公共配置 GlobalConfig(全局唯一):佣金/印花税/滑点/最低佣金/复权口径/基准
- 选股策略 SelectionStrategy(原 StrategyDefinition 改名):只剩股票池+因子+条件,
不再持有 selection/rebalance/costs/portfolio/区间/资金
- 回测组合 BacktestCombo:引用若干选股策略 + 回测时才定的参数
(起始资金、持仓数 N、持仓天数区间 [Tmin,Tmax]、调仓时机 日/周/月、区间)
引擎(app/quant/combo_engine.py,新增):
- 多策略打分 = 并集 + Borda 秩和(各策略 1/名次 求和;不假设不同策略分值可比,
能容纳各策略股票池不同);抽出纯函数 borda_combine 便于单测
- 持仓天数区间 [Tmin,Tmax]:Tmax **每个交易日**强制了结(安全阀,月频下也不超期);
Tmin 仅在调仓日保护(掉出 TopN 但未满 Tmin 暂留,防频繁换手);调仓日为增量调仓
(只卖超期/掉队且满 Tmin 的,从 TopN 补买至 N 只,不主动减持以尊重 Tmin)
- 调仓时机 daily/weekly/monthly(local_engine.rebalance_dates 新增日频分支)
- 产出与旧 runner 同构的 BacktestResult,前端可视化无需改动;config_snapshot 固化
ComboRunSpec(组合+当时各策略定义+当时成本/复权)保证可复现
数据层:
- 新表 global_config(默认行:万三/hfq/最低佣金5元)、backtest_combo
- 迁移 b4c5d6e7f8a9:建两表 + 把存量 strategy.config_json 的回测参数键剥掉、
spec_type 收敛为 selection(已在真实 MariaDB 验证:STG-16BFBF08 清洗后只剩
universe/factors/conditions)
- 仓储 SqlAlchemyGlobalConfigRepository / SqlAlchemyComboRepository + Protocol
API:
- /api/config GET/PUT;/api/combos CRUD + /{id}/run + /run(kind=combo 异步 Job)
- job_executor 新增 combo 分支:取齐策略+读公共配置→ComboService.run,归档 kind
记 backtest(结果结构相同)
- /api/strategies 切到 SelectionStrategy,移除已废弃的 /{id}/expand
- strategy_doc.describe_strategy 支持 SelectionStrategy(只讲「怎么选」,如实声明
资金/持仓/调仓/成本/区间在回测组合里定)
旧的 ResearchSpec + /api/backtests 保留(因子测试与既有契约自检仍用),
作为底层 escape hatch;用户产品路径改为回测组合。
测试:新增 test_combo_engine(6)/test_combo_service(3)/test_combo_api(5),
改写 test_strategies/test_strategy_doc 适配新模型。全量 403 passed(原 388)。
This commit is contained in:
+113
@@ -0,0 +1,113 @@
|
||||
"""公共配置 + 回测组合表,并把存量策略收敛为「选股条件组合」(2026-09 重构)
|
||||
|
||||
Revision ID: b4c5d6e7f8a9
|
||||
Revises: a3f8c21d9b47
|
||||
Create Date: 2026-09-30
|
||||
|
||||
背景:把原来「一个策略 = 全套参数」拆成三件事 ——
|
||||
1. global_config:费率/印花税/滑点/最低佣金/复权口径/基准(全局唯一一行);
|
||||
2. selection_strategy(复用 strategy 表):只剩股票池 + 因子 + 过滤条件;
|
||||
3. backtest_combo:引用若干选股策略 + 回测参数(资金/持仓数/持仓天数区间/调仓时机/区间)。
|
||||
|
||||
本迁移:
|
||||
- 新建 global_config(并插入默认行)与 backtest_combo 两张表;
|
||||
- 把 strategy.config_json 里**已废弃的回测执行参数键**剥掉(selection / rebalance /
|
||||
costs / portfolio / price_adjustment / *_interval_months),只留 universe/factors/conditions,
|
||||
并把 spec_type 标为 selection。旧数据不丢(归档里的 ResearchSpec 快照原样保留只读)。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from collections.abc import Sequence
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision: str = "b4c5d6e7f8a9"
|
||||
down_revision: str | None = "a3f8c21d9b47"
|
||||
branch_labels: str | Sequence[str] | None = None
|
||||
depends_on: str | Sequence[str] | None = None
|
||||
|
||||
# 选股策略不再承载的回测执行参数键(读出时也会被仓储丢弃,这里在存储侧也清掉)
|
||||
_LEGACY_KEYS = (
|
||||
"selection",
|
||||
"rebalance",
|
||||
"costs",
|
||||
"portfolio",
|
||||
"price_adjustment",
|
||||
"selection_interval_months",
|
||||
"rebalance_interval_months",
|
||||
)
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
# ---- 1. 公共配置(单例) ----
|
||||
op.create_table(
|
||||
"global_config",
|
||||
sa.Column("id", sa.String(length=32), nullable=False),
|
||||
sa.Column("commission_rate", sa.Numeric(10, 6), nullable=False, server_default="0.0003"),
|
||||
sa.Column("stamp_tax_rate", sa.Numeric(10, 6), nullable=False, server_default="0.0005"),
|
||||
sa.Column("slippage_rate", sa.Numeric(10, 6), nullable=False, server_default="0.001"),
|
||||
sa.Column("min_commission", sa.Numeric(10, 4), nullable=False, server_default="5"),
|
||||
sa.Column("price_adjustment", sa.String(length=8), nullable=False, server_default="hfq"),
|
||||
sa.Column("benchmark", sa.String(length=16), nullable=False, server_default="000300.SH"),
|
||||
sa.Column("updated_at", sa.DateTime(), nullable=True),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
)
|
||||
# 插入默认行(高股息场景默认后复权 hfq、最低佣金 5 元)
|
||||
op.execute(
|
||||
"INSERT INTO global_config (id, commission_rate, stamp_tax_rate, slippage_rate, "
|
||||
"min_commission, price_adjustment, benchmark) VALUES "
|
||||
"('default', 0.0003, 0.0005, 0.001, 5, 'hfq', '000300.SH')"
|
||||
)
|
||||
|
||||
# ---- 2. 回测组合 ----
|
||||
op.create_table(
|
||||
"backtest_combo",
|
||||
sa.Column("id", sa.String(length=32), nullable=False),
|
||||
sa.Column("name", sa.String(length=64), nullable=False),
|
||||
sa.Column("description", sa.String(length=300), nullable=False, server_default=""),
|
||||
sa.Column("strategy_ids_json", sa.Text(), nullable=False),
|
||||
sa.Column("initial_capital", sa.Numeric(20, 2), nullable=False, server_default="1000000"),
|
||||
sa.Column("hold_count", sa.Integer(), nullable=False, server_default="20"),
|
||||
sa.Column("hold_min_days", sa.Integer(), nullable=False, server_default="0"),
|
||||
sa.Column("hold_max_days", sa.Integer(), nullable=True),
|
||||
sa.Column("rebalance_freq", sa.String(length=12), nullable=False, server_default="monthly"),
|
||||
sa.Column("start_date", sa.Date(), nullable=False),
|
||||
sa.Column("end_date", sa.Date(), nullable=False),
|
||||
sa.Column("version", sa.String(length=16), nullable=False, server_default="1"),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=False),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
sa.UniqueConstraint("name", name="uq_backtest_combo_name"),
|
||||
)
|
||||
|
||||
# ---- 3. 存量策略 → 选股条件组合(剥掉回测执行参数键) ----
|
||||
conn = op.get_bind()
|
||||
rows = conn.execute(sa.text("SELECT id, config_json FROM strategy")).fetchall()
|
||||
for row_id, cfg_text in rows:
|
||||
try:
|
||||
data = json.loads(cfg_text) if cfg_text else {}
|
||||
except json.JSONDecodeError:
|
||||
continue # 损坏行不动它(读出时仓储也会容错)
|
||||
changed = False
|
||||
for key in _LEGACY_KEYS:
|
||||
if key in data:
|
||||
data.pop(key)
|
||||
changed = True
|
||||
# spec_type 收敛为 selection(旧值多为 backtest)
|
||||
if data.get("spec_type") != "selection":
|
||||
data["spec_type"] = "selection"
|
||||
changed = True
|
||||
if changed:
|
||||
conn.execute(
|
||||
sa.text("UPDATE strategy SET config_json = :cfg, spec_type = 'selection' WHERE id = :id"),
|
||||
{"cfg": json.dumps(data, ensure_ascii=False), "id": row_id},
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
# 回滚:删两张新表。strategy.config_json 被剥掉的键无法精确还原
|
||||
# (原始值未备份),故 downgrade 仅撤表结构,不承诺恢复旧策略的完整 config。
|
||||
op.drop_table("backtest_combo")
|
||||
op.drop_table("global_config")
|
||||
Reference in New Issue
Block a user