Files
sanguo_vnpy_v2/sanguo_factor/library.py
T
claude_dev f93621e3fc feat(factor): 因子表达式注册 + Alpha158 子集内置库
- 实现因子表达式注册表(registry.py)
- 实现 Alpha158 子集内置因子库(library.py)
- 使用 vnpy.alpha ts_ 算子语法(ts_mean, ts_delta, ts_max, ts_min)
- 修正列名:close/open/high/low/volume(非 _price 后缀)
- TDD 实现:8 测试全部通过
- 支持因子分类过滤(custom/builtin)
- 防止重复注册

Co-Authored-By: Claude <noreply@anthropic.com>
2026-07-06 08:06:10 +08:00

59 lines
1.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""内置因子表达式库(Alpha158/101 子集,使用 vnpy.alpha ts_ 算子)."""
from .registry import register_factor
# 内置因子列表(使用 ts_ 算子 + 列名 close/open/high/low/volume
# 修正:使用 vnpy.alpha 表达式语法(ts_mean 等),而非 polars 语法
BUILTIN_FACTORS = [
{
"name": "ma5",
"expression": "ts_mean(close, 5)",
"category": "builtin"
},
{
"name": "ma10",
"expression": "ts_mean(close, 10)",
"category": "builtin"
},
{
"name": "ma20",
"expression": "ts_mean(close, 20)",
"category": "builtin"
},
{
"name": "vol_ma5",
"expression": "ts_mean(volume, 5)",
"category": "builtin"
},
{
"name": "return_1d",
"expression": "ts_delta(close, 1)",
"category": "builtin"
},
{
"name": "return_5d",
"expression": "ts_delta(close, 5)",
"category": "builtin"
},
{
"name": "high_low_5",
"expression": "(ts_max(high, 5) - ts_min(low, 5)) / close",
"category": "builtin"
},
]
def _register_all() -> None:
"""注册所有内置因子到注册表."""
from .registry import _REGISTRY
for factor in BUILTIN_FACTORS:
if factor["name"] not in _REGISTRY:
register_factor(
factor["name"],
factor["expression"],
factor["category"]
)
# 模块导入时自动注册内置因子
_register_all()