feat: 股息率案例口径 + 策略库与图表统一 + 回测存档完整化

汇总三轮未提交的开发(每轮均在本机 MariaDB + 真实浏览器上验证):

1) 股息率案例(全市场股息率最高 n 只,默认 20,每 m 月择股)
   - 新增日频估值表 daily_basic + 迁移;股息率因子(dv_ratio / dividend_yield / TTM)
   - 名称历史表 stock_name_history:剔除 ST 按**择股日当时名称**判定,消除
     「曾高股息后 ST」的股息陷阱(实测 3.70pp 偏差)
   - 区间择股/调仓双周期(m 择股 / y 调仓)、指数成分与白名单、停牌近似剔除
   - 复权因子口径核对(4,164,742 行、缺失 0.0%)、收盘价成交与涨跌停拦单
   - 案例实测:2020-01-01~2026-09-04 总收益 +24.86%(年化 3.52%、回撤 -28.58%)

2) 策略库与前端统一
   - strategy 表 + CRUD/PUT 原地更新 + `describe_strategy` 按 spec 真实推导
     「一句话说明 + 计算公式 + 执行步骤 + 注意事项」(与引擎实执行规则同源)
   - 任何出现股票代码处都成对显示名称且可点击进个股页
   - 全站图表基座统一 TradingView Lightweight Charts(ECharts 依赖、
     锁文件、组件与文档标注一并清除),买卖点标记只落在真实交易日上

3) 回测存档完整化(可往复查看)
   - 同步端点(POST /api/backtests、/api/factor-tests)此前完全不落库 → 现在同样归档,
     归档 id 经响应头 X-Experiment-Id 返回(不破坏 response_model)
   - data_version 首次真实写入(数据快照指纹:最新交易日 + 各表规模)
   - 个股收益曲线默认**全量保存**(此前硬截断 60 只);超出体积预算才裁剪,
     并写 archive_meta(机器可读)+ unimplemented(人可读)如实标注
   - 列表 kind/q 过滤 + X-Total-Count(此前 limit=50 静默截断)、DELETE 归档
   - 只读归档页 /experiments/{id}(Server Component,SSR 直出**选股条件**与
     **交易执行依据**);结果视图按 kind 分发(backtest/factor_test/selection),
     非回测归档不套用回测口径
   - 新增 CLI:prune_experiments(保留策略,默认 dry-run)、
     restore_experiment_from_job(从 Job 副本按原 id 重建被删的历史归档,默认 dry-run)

门禁:pytest 388 passed、ruff All checks passed、tsc 0 错误、图表单测 7 passed、
next build 成功、契约脚本 verify_strategy_workspace 59/59(含按 kind 逐类验证归档页)。
This commit is contained in:
Simon
2026-09-20 07:31:04 +08:00
parent 7e15b7251e
commit 23972e7063
112 changed files with 17908 additions and 3893 deletions
@@ -0,0 +1,67 @@
"""add daily_basic table
Revision ID: 22d7380706f7
Revises: f5e0d1c2b3a4
Create Date: 2026-09-19 15:53:37.029097
每日指标表(Tushare daily_basic):估值 / 股息率 / 市值,幂等键 (symbol, trade_date)。
供高股息等横截面选股因子使用(研究侧按 trade_date <= as_of 取用,无未来函数)。
说明:本文件由 `alembic revision --autogenerate` 生成后**手工裁剪**。
自动生成时同时检出了 `index_weight` 的 drop —— 那是 `IndexWeightModel`
未在 `models/__init__.py` 注册导致的假差异(表实际存在),
已在同一提交中补上注册,此处只保留 daily_basic 的建表语句。
"""
from __future__ import annotations
from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op
revision: str = '22d7380706f7'
down_revision: str | None = 'f5e0d1c2b3a4'
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None
def upgrade() -> None:
op.create_table(
'daily_basic',
sa.Column(
'id',
sa.BigInteger().with_variant(sa.Integer(), 'sqlite'),
autoincrement=True,
nullable=False,
),
sa.Column('symbol', sa.String(length=12), nullable=False),
sa.Column('trade_date', sa.Date(), nullable=False),
sa.Column('source', sa.String(length=16), server_default='tushare', nullable=False),
sa.Column('close', sa.Numeric(precision=12, scale=4), nullable=True),
sa.Column('turnover_rate', sa.Numeric(precision=12, scale=4), nullable=True),
sa.Column('volume_ratio', sa.Numeric(precision=12, scale=4), nullable=True),
sa.Column('pe', sa.Numeric(precision=16, scale=4), nullable=True),
sa.Column('pe_ttm', sa.Numeric(precision=16, scale=4), nullable=True),
sa.Column('pb', sa.Numeric(precision=16, scale=4), nullable=True),
sa.Column('ps', sa.Numeric(precision=16, scale=4), nullable=True),
sa.Column('ps_ttm', sa.Numeric(precision=16, scale=4), nullable=True),
sa.Column('dv_ratio', sa.Numeric(precision=12, scale=4), nullable=True),
sa.Column('dv_ttm', sa.Numeric(precision=12, scale=4), nullable=True),
sa.Column('total_share', sa.Numeric(precision=24, scale=4), nullable=True),
sa.Column('float_share', sa.Numeric(precision=24, scale=4), nullable=True),
sa.Column('free_share', sa.Numeric(precision=24, scale=4), nullable=True),
sa.Column('total_mv', sa.Numeric(precision=24, scale=4), nullable=True),
sa.Column('circ_mv', sa.Numeric(precision=24, scale=4), nullable=True),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('symbol', 'trade_date', name='uq_basic_symbol_date'),
)
with op.batch_alter_table('daily_basic', schema=None) as batch_op:
batch_op.create_index(batch_op.f('ix_daily_basic_symbol'), ['symbol'], unique=False)
batch_op.create_index(batch_op.f('ix_daily_basic_trade_date'), ['trade_date'], unique=False)
def downgrade() -> None:
with op.batch_alter_table('daily_basic', schema=None) as batch_op:
batch_op.drop_index(batch_op.f('ix_daily_basic_trade_date'))
batch_op.drop_index(batch_op.f('ix_daily_basic_symbol'))
op.drop_table('daily_basic')
@@ -0,0 +1,47 @@
"""research result_json → MEDIUMTEXT(长回测结果落库)
Revision ID: 7b1c4e9a52d8
Revises: 22d7380706f7
Create Date: 2026-09-19
背景:全市场多年回测结果(净值/回撤曲线 + 逐笔成交 + Signal↔Fill + 个股收益曲线)
实测约 1.2MB,MySQL `TEXT`(64KB)会报 1406 Data too long → Job 归档失败
(2026-09 高股息案例实测:回测本身成功,落库失败)。
本迁移把 job.result_json / experiment.result_json 放宽到 MEDIUMTEXT(16MB)。
SQLite 等方言不区分 TEXT 长度,迁移里做方言判断,非 MySQL 直接跳过。
"""
from __future__ import annotations
from alembic import op
from sqlalchemy import text
revision = "7b1c4e9a52d8"
down_revision = "22d7380706f7"
branch_labels = None
depends_on = None
_COLUMNS = (("job", "result_json", True), ("experiment", "result_json", False))
def _dialect() -> str:
return op.get_bind().dialect.name
def upgrade() -> None:
if _dialect() != "mysql":
return # SQLite/TEXT 无长度限制,无需变更
for table, column, nullable in _COLUMNS:
null_clause = "NULL" if nullable else "NOT NULL"
op.execute(
text(f"ALTER TABLE {table} MODIFY COLUMN {column} MEDIUMTEXT {null_clause}")
)
def downgrade() -> None:
if _dialect() != "mysql":
return
for table, column, nullable in _COLUMNS:
null_clause = "NULL" if nullable else "NOT NULL"
op.execute(text(f"ALTER TABLE {table} MODIFY COLUMN {column} TEXT {null_clause}"))
@@ -0,0 +1,64 @@
"""add stock_name_history table
Revision ID: a3f8c21d9b47
Revises: 7b1c4e9a52d8
Create Date: 2026-09-19 21:40:12.000000
股票名称变更历史(Tushare namechange):时点 ST / 风险警示判定的依据。
**为什么需要**:`stock.name` 是最新名称快照,用它做 `exclude_st` 会把
「曾为高股息、后来才变 ST/退市」的标的在整段历史里都排除 —— 而那正是
「股息陷阱」样本。实测对照:同一 spec 仅改 exclude_st,收益 +35.71% → +32.01%,
即约 3.70pp 的收益被名称快照口径隐藏。
幂等键 (symbol, start_date)。时点查询:
`start_date <= as_of AND (end_date IS NULL OR end_date >= as_of)`。
"""
from __future__ import annotations
from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op
revision: str = 'a3f8c21d9b47'
down_revision: str | None = '7b1c4e9a52d8'
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None
def upgrade() -> None:
op.create_table(
'stock_name_history',
sa.Column(
'id',
sa.BigInteger().with_variant(sa.Integer(), 'sqlite'),
autoincrement=True,
nullable=False,
),
sa.Column('symbol', sa.String(length=12), nullable=False),
sa.Column('name', sa.String(length=64), nullable=False),
sa.Column('start_date', sa.Date(), nullable=False),
sa.Column('end_date', sa.Date(), nullable=True),
sa.Column('ann_date', sa.Date(), nullable=True),
sa.Column('change_reason', sa.String(length=32), nullable=True),
sa.Column('source', sa.String(length=16), server_default='tushare', nullable=False),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('symbol', 'start_date', name='uq_name_symbol_start'),
)
with op.batch_alter_table('stock_name_history', schema=None) as batch_op:
batch_op.create_index(batch_op.f('ix_stock_name_history_symbol'), ['symbol'], unique=False)
batch_op.create_index(
batch_op.f('ix_stock_name_history_start_date'), ['start_date'], unique=False
)
batch_op.create_index(
batch_op.f('ix_stock_name_history_end_date'), ['end_date'], unique=False
)
def downgrade() -> None:
with op.batch_alter_table('stock_name_history', schema=None) as batch_op:
batch_op.drop_index(batch_op.f('ix_stock_name_history_end_date'))
batch_op.drop_index(batch_op.f('ix_stock_name_history_start_date'))
batch_op.drop_index(batch_op.f('ix_stock_name_history_symbol'))
op.drop_table('stock_name_history')