feat(backend): 字段库(condition_field)+ 因子参数化(模板/受控参数)+ 单位换算底座
字段库(本次新增的表与接口): - `condition_field` 表 + `/api/condition-fields`:中文名/说明可编辑、可停用; `kind`/单位阶梯/`base_unit` 由代码注册表收敛(改类型 422,伪字段 422, 越界单位 422),停用的字段不再进条件下拉,但既有策略仍按名字解析。 - 说明书里的数值条件按字段注册表补**基准单位**后缀(字段间比较不加,不猜单位)。 因子参数化(键即身份,冻结口径): - 模板 + 参数注册表(`quant/factors.py`):`ParamSpec`(类型/范围/枚举/默认值/说明)+ `FactorTemplate`(公式/依赖列/参数);规范键把**全部**参数写进名字,如 `momentum(window=90,direction=lower_is_better)`,所以改参数 = 新建一个身份, 旧因子/既有策略/已归档实验都不变义;`momentum(window=90)`(缺参数)明确拒绝 —— 缺项要靠模板默认值补齐,而默认值是可改的代码细节,一旦改动会追溯性改义。 - 参数只在受控范围内取值(窗口 2~500、方向二选一),越界/未知模板/多给参数一律 422 并列出允许范围,不静默截断、不悄悄取默认值;内置实例的启用开关由代码决定(422)。 - `/api/factors` 暴露 `template`/`params`/`param_specs`/`label`/`source`/`enabled`/ `resolvable`;新增 `/api/factors/templates`、`POST /api/factors`、`PATCH /api/factors`; `get_factor = resolve_factor` 兼容全部旧调用点,参数化键也是一等条件字段。 - 迁移链:c5d6(存量策略陈旧说明重算)→ d6e7(condition_field)→ a7c1 (factor_definition.enabled + name varchar(128))。 测试:新增 test_condition_fields.py / test_factor_params.py;全量 pytest 500 passed。
This commit is contained in:
@@ -253,6 +253,44 @@ class TestStepsAndWarnings:
|
||||
)
|
||||
assert any("替补" in s for s in doc.steps)
|
||||
|
||||
def test_selection_steps_carry_no_own_numbering(self) -> None:
|
||||
"""选股策略的执行步骤不带自带序号:前端渲染进 <ol>,后端再写「1.」会双重编号。"""
|
||||
st = SelectionStrategy(
|
||||
name="高股息",
|
||||
description="x",
|
||||
factors=[FactorSpec(name="dividend_yield", weight=1)],
|
||||
conditions=[],
|
||||
)
|
||||
doc = describe_strategy(st)
|
||||
assert doc.steps
|
||||
for s in doc.steps:
|
||||
assert not s.lstrip().startswith(("1.", "2.", "3.", "4.", "5.")), s
|
||||
# 步骤内容本身仍要在(去掉的只是序号)
|
||||
assert "股票池" in doc.steps[0]
|
||||
assert "因子" in doc.steps[-1]
|
||||
|
||||
def test_condition_literals_carry_base_unit(self) -> None:
|
||||
"""字面量条件必须带**基准单位**:库里存的就是基准单位值,裸数字会被读成别的量级。
|
||||
|
||||
单位只做界面换算(见 quant/condition_fields 的单位阶梯),所以文档里写「50000 万元」
|
||||
才是引擎真正比较的口径;字段间比较(ref)两侧同单位,不加后缀。
|
||||
"""
|
||||
st = SelectionStrategy(
|
||||
name="单位口径",
|
||||
description="x",
|
||||
factors=[FactorSpec(name="dividend_yield", weight=1)],
|
||||
conditions=[
|
||||
ConditionSpec(field="total_mv", op="gte", value=50000),
|
||||
ConditionSpec(field="close", op="gt", ref="ma60"),
|
||||
ConditionSpec(field="industry", op="in", value=["银行", "白酒"]),
|
||||
],
|
||||
)
|
||||
doc = describe_strategy(st)
|
||||
joined = "\n".join([doc.formula, *doc.steps])
|
||||
assert "total_mv >= 50000 万元" in joined
|
||||
assert "close > 字段 ma60" in joined and "ma60 元" not in joined
|
||||
assert "['银行', '白酒']" in joined
|
||||
|
||||
def test_universe_step_conditional_and_time_accurate(self) -> None:
|
||||
"""股票池步骤只写实际生效的过滤,且时点口径必须正确(起始日过滤一次)。
|
||||
|
||||
|
||||
Reference in New Issue
Block a user