- factor_composite 表(migration c3e9a0d1f4b5,MySQL 已应用;name 唯一) - CompositeDefinition/Component 实体 + CompositeRepository Protocol + SQLAlchemy 实现 - /api/composites:POST(注册表自动填充组件 direction;未注册因子 400)、GET 列表/详情、DELETE - tests/test_composites_api.py(repo CRUD/同名拒绝/删除、API 方向填充/404/400);全量 pytest 通过
34 lines
1.1 KiB
Python
34 lines
1.1 KiB
Python
"""因子组合(Composite Factor)领域实体(M7.2b,v2 §13)。
|
||
|
||
组合 = 一组 {因子, 权重} + method(MVP fixed:截面 zscore×方向×权重求和;
|
||
方向在计算时取因子注册表元数据,落库时冗余快照以便列表展示)。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from datetime import datetime
|
||
|
||
from pydantic import BaseModel, Field, model_validator
|
||
|
||
|
||
class CompositeComponent(BaseModel):
|
||
name: str
|
||
weight: float = Field(default=1.0, gt=0)
|
||
direction: str = Field(default="higher_is_better")
|
||
|
||
|
||
class CompositeDefinition(BaseModel):
|
||
id: str = ""
|
||
name: str = Field(min_length=1, max_length=64)
|
||
method: str = Field(default="fixed", pattern="^(fixed)$")
|
||
description: str = ""
|
||
components: list[CompositeComponent] = Field(min_length=1)
|
||
created_at: datetime | None = None
|
||
|
||
@model_validator(mode="after")
|
||
def _no_duplicate(self) -> CompositeDefinition:
|
||
names = [c.name for c in self.components]
|
||
if len(set(names)) != len(names):
|
||
raise ValueError("components 存在重复因子名")
|
||
return self
|