汇总三轮未提交的开发(每轮均在本机 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 逐类验证归档页)。
163 lines
7.4 KiB
Python
163 lines
7.4 KiB
Python
"""B1-1 指数历史成分测试:Repository 快照与历史成分查询(Survivorship-free)、
|
||
Universe.index_code 过滤接线(选股/回测只用 as_of 当日成分)。"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from datetime import date
|
||
from decimal import Decimal
|
||
|
||
import pytest
|
||
from app.domain.entities.index import IndexWeight
|
||
from app.infrastructure.persistence.sqlalchemy.base import Base
|
||
from app.infrastructure.persistence.sqlalchemy.repositories.index_impl import (
|
||
SqlAlchemyIndexConstituentRepository,
|
||
)
|
||
from sqlalchemy import create_engine
|
||
from sqlalchemy.orm import sessionmaker
|
||
|
||
_CODE = "000300.SH"
|
||
|
||
|
||
@pytest.fixture()
|
||
def session(tmp_path):
|
||
engine = create_engine(f"sqlite:///{tmp_path / 'idx.db'}", future=True)
|
||
Base.metadata.create_all(engine)
|
||
Session = sessionmaker(bind=engine, expire_on_commit=False)
|
||
with Session() as s:
|
||
yield s
|
||
|
||
|
||
def _rows() -> list[IndexWeight]:
|
||
# 2024-06 期成分:A,C(B 被剔除);2024-11 期成分:A,B(C 新晋替换场景)
|
||
return [
|
||
IndexWeight(index_code=_CODE, index_name="沪深300", trade_date=date(2024, 6, 28),
|
||
symbol="600000.SH", weight=Decimal("1.0")),
|
||
IndexWeight(index_code=_CODE, index_name="沪深300", trade_date=date(2024, 6, 28),
|
||
symbol="600002.SH", weight=Decimal("1.0")),
|
||
IndexWeight(index_code=_CODE, index_name="沪深300", trade_date=date(2024, 11, 29),
|
||
symbol="600000.SH", weight=Decimal("1.5")),
|
||
IndexWeight(index_code=_CODE, index_name="沪深300", trade_date=date(2024, 11, 29),
|
||
symbol="600001.SH", weight=Decimal("1.0")),
|
||
]
|
||
|
||
class TestIndexConstituentRepository:
|
||
def test_upsert_and_members_at_history(self, session) -> None:
|
||
repo = SqlAlchemyIndexConstituentRepository(session)
|
||
assert repo.upsert_many(_rows()) == 4
|
||
session.commit()
|
||
# 2024-07(最近快照 2024-06)→ {A,C};2025(最近 2024-11)→ {A,B}
|
||
assert repo.members_at(_CODE, date(2024, 7, 15)) == {"600000.SH", "600002.SH"}
|
||
assert repo.members_at(_CODE, date(2025, 1, 10)) == {"600000.SH", "600001.SH"}
|
||
# 快照之前 → 空集(不返回未来成分)
|
||
assert repo.members_at(_CODE, date(2024, 1, 1)) == set()
|
||
assert repo.latest_date(_CODE) == date(2024, 11, 29)
|
||
|
||
def test_upsert_idempotent(self, session) -> None:
|
||
repo = SqlAlchemyIndexConstituentRepository(session)
|
||
repo.upsert_many(_rows())
|
||
session.commit()
|
||
repo.upsert_many([_rows()[2]]) # 重复
|
||
session.commit()
|
||
assert len(repo.members_at(_CODE, date(2024, 12, 31))) == 2
|
||
|
||
|
||
class TestUniverseIndexCodeFilter:
|
||
def _build(self, tmp_path):
|
||
engine = create_engine(f"sqlite:///{tmp_path / 'api.db'}", future=True)
|
||
Base.metadata.create_all(engine)
|
||
Session = sessionmaker(bind=engine, expire_on_commit=False)
|
||
from app.application.services.selection_service import SelectionService
|
||
from app.domain.entities.market import Stock
|
||
from app.infrastructure.persistence.sqlalchemy.repositories.market_impl import (
|
||
SqlAlchemyDailyBarRepository,
|
||
SqlAlchemyStockRepository,
|
||
)
|
||
|
||
from conftest_quant import bars_dataframe_to_daily_bars, synthetic_daily
|
||
|
||
df = synthetic_daily(
|
||
{"600000.SH": 0.008, "600001.SH": 0.006, "600002.SH": 0.004}, n=320
|
||
)
|
||
with Session() as session:
|
||
SqlAlchemyStockRepository(session).upsert_many(
|
||
[
|
||
Stock(symbol="600000.SH", name="A", list_date=date(1999, 1, 1)),
|
||
Stock(symbol="600001.SH", name="B", list_date=date(1999, 1, 1)),
|
||
Stock(symbol="600002.SH", name="C", list_date=date(1999, 1, 1)),
|
||
]
|
||
)
|
||
SqlAlchemyDailyBarRepository(session).upsert_many(bars_dataframe_to_daily_bars(df))
|
||
SqlAlchemyIndexConstituentRepository(session).upsert_many(_rows())
|
||
session.commit()
|
||
svc = SelectionService(
|
||
SqlAlchemyStockRepository(session),
|
||
SqlAlchemyDailyBarRepository(session),
|
||
index_repo=SqlAlchemyIndexConstituentRepository(session),
|
||
)
|
||
from app.domain.entities.research import UniverseSpec
|
||
from app.domain.entities.selection import SelectionQuery
|
||
|
||
# as_of=2024-10(成分 {A,C})→ 只从 {A,C} 选,B 绝不进入
|
||
q = SelectionQuery(
|
||
universe=UniverseSpec(exclude_st=False, min_listing_days=0, index_code=_CODE),
|
||
factors=[{"name": "momentum_60", "weight": 1.0}],
|
||
top_n=2, as_of=date(2024, 10, 15),
|
||
)
|
||
res = svc.select(q)
|
||
got = {c.symbol for c in res.candidates}
|
||
assert got == {"600000.SH", "600002.SH"}
|
||
# as_of=2025-01(成分 {A,B})→ C 不再可入选
|
||
q2 = SelectionQuery(
|
||
universe=UniverseSpec(exclude_st=False, min_listing_days=0, index_code=_CODE),
|
||
factors=[{"name": "momentum_60", "weight": 1.0}],
|
||
top_n=2, as_of=date(2025, 1, 10),
|
||
)
|
||
res2 = svc.select(q2)
|
||
assert {c.symbol for c in res2.candidates} == {"600000.SH", "600001.SH"}
|
||
|
||
def test_index_code_filter(self, tmp_path) -> None:
|
||
self._build(tmp_path)
|
||
|
||
|
||
class TestDelistedUniverse:
|
||
"""退市股的时点股票池语义(幸存者偏差修正的核心断言)。
|
||
|
||
退市股必须在**退市日之前**纳入池子、退市之后排除;否则回测只剩「活下来的
|
||
赢家」,收益被系统性高估(高股息策略尤其容易被股息陷阱的退市股反噬)。
|
||
"""
|
||
|
||
def _stock(self, symbol: str, list_date: date, delist_date: date | None):
|
||
from app.domain.entities.market import Stock
|
||
|
||
return Stock(
|
||
symbol=symbol, name=f"测试{symbol}", list_date=list_date, delist_date=delist_date
|
||
)
|
||
|
||
def test_delisted_included_before_excluded_after(self) -> None:
|
||
from app.domain.entities.research import UniverseSpec
|
||
from app.quant.universe import filter_stocks
|
||
|
||
stocks = [
|
||
self._stock("600000.SH", date(1999, 11, 10), None),
|
||
self._stock("000005.SZ", date(1990, 12, 10), date(2024, 4, 26)),
|
||
]
|
||
u = UniverseSpec(exclude_st=False, min_listing_days=0)
|
||
before = {s.symbol for s in filter_stocks(stocks, u, as_of=date(2024, 1, 2))}
|
||
after = {s.symbol for s in filter_stocks(stocks, u, as_of=date(2024, 6, 3))}
|
||
on_delist_day = {s.symbol for s in filter_stocks(stocks, u, as_of=date(2024, 4, 26))}
|
||
assert "000005.SZ" in before and "000005.SZ" not in after
|
||
assert "000005.SZ" in on_delist_day # 退市日当天仍在(数据截至当日)
|
||
assert "600000.SH" in before and "600000.SH" in after
|
||
|
||
def test_st_name_excludes_regardless_of_period(self) -> None:
|
||
"""已知局限:exclude_st 用**最新名称**判定,会把曾用名非 ST 的标的整段排除。"""
|
||
from app.domain.entities.research import UniverseSpec
|
||
from app.quant.universe import filter_stocks
|
||
|
||
stocks = [self._stock("000005.SZ", date(1990, 12, 10), date(2024, 4, 26))]
|
||
stocks[0].name = "ST星源(退)"
|
||
u = UniverseSpec(exclude_st=True, min_listing_days=0)
|
||
assert filter_stocks(stocks, u, as_of=date(2024, 1, 2)) == []
|
||
u2 = UniverseSpec(exclude_st=False, min_listing_days=0)
|
||
assert len(filter_stocks(stocks, u2, as_of=date(2024, 1, 2))) == 1
|