feat(factor): wire up builtin factor auto-registration and export

This commit is contained in:
GitHub
2026-06-12 19:53:18 +08:00
parent c9be1f85d9
commit d9bb37f750
3 changed files with 260 additions and 2 deletions
+12 -1
View File
@@ -1,7 +1,18 @@
# src/easy_tdx/factor/__init__.py
"""因子研究模块。"""
from __future__ import annotations
from easy_tdx.factor.base import FACTORY_REGISTRY, Factor, register_factor
from easy_tdx.factor.engine import FactorEngine
__all__ = ["Factor", "register_factor", "FACTORY_REGISTRY", "FactorEngine"]
# 导入 builtin 触发自动注册
from easy_tdx.factor.builtin import get_factor, list_factors # noqa: F401
__all__ = [
"Factor",
"register_factor",
"FACTORY_REGISTRY",
"FactorEngine",
"list_factors",
"get_factor",
]
+40 -1
View File
@@ -1,2 +1,41 @@
"""内置因子 — 导入注册。"""
"""内置因子 — 导入子模块触发注册。"""
from __future__ import annotations
from easy_tdx.factor.base import FACTORY_REGISTRY, Factor
# 导入所有子模块以触发 @register_factor 装饰器
from easy_tdx.factor.builtin import ( # noqa: F401
chanlun,
momentum,
quality,
technical,
value,
volatility,
volume,
)
def list_factors() -> list[dict[str, str | tuple[str, ...]]]:
"""返回所有已注册因子的元数据。"""
return [
{
"name": cls.name,
"category": cls.category,
"description": cls.description,
"inputs": cls.inputs,
}
for cls in FACTORY_REGISTRY.values()
]
def get_factor(name: str) -> type[Factor]:
"""按名称获取因子类。
Raises:
ValueError: 因子不存在。
"""
if name not in FACTORY_REGISTRY:
raise ValueError(
f"未知因子: {name!r}。可用因子: {sorted(FACTORY_REGISTRY.keys())}"
)
return FACTORY_REGISTRY[name]