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>
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
"""内置因子库测试."""
|
||||
from sanguo_factor.registry import list_factors, get_factor, _REGISTRY
|
||||
import importlib
|
||||
import sanguo_factor.library
|
||||
|
||||
|
||||
def test_library_registers_on_import():
|
||||
"""测试导入时自动注册内置因子."""
|
||||
_REGISTRY.clear()
|
||||
importlib.reload(sanguo_factor.library)
|
||||
assert len(list_factors(category="builtin")) > 0
|
||||
|
||||
|
||||
def test_builtin_factors_count():
|
||||
"""测试内置因子数量正确."""
|
||||
_REGISTRY.clear()
|
||||
importlib.reload(sanguo_factor.library)
|
||||
builtin_factors = list_factors(category="builtin")
|
||||
assert len(builtin_factors) == 7
|
||||
|
||||
|
||||
def test_builtin_factor_expressions():
|
||||
"""测试内置因子表达式格式正确."""
|
||||
_REGISTRY.clear()
|
||||
importlib.reload(sanguo_factor.library)
|
||||
ma5 = get_factor("ma5")
|
||||
assert ma5 is not None
|
||||
assert ma5["category"] == "builtin"
|
||||
assert "ts_mean(close, 5)" in ma5["expression"]
|
||||
@@ -0,0 +1,43 @@
|
||||
"""因子表达式注册表测试."""
|
||||
import pytest
|
||||
from sanguo_factor.registry import register_factor, get_factor, list_factors, _REGISTRY
|
||||
|
||||
|
||||
def test_register_and_get_factor():
|
||||
"""测试注册和获取因子."""
|
||||
_REGISTRY.clear()
|
||||
register_factor("ma5", "ts_mean(close, 5)", category="trend")
|
||||
f = get_factor("ma5")
|
||||
assert f["name"] == "ma5"
|
||||
assert f["category"] == "trend"
|
||||
assert f["expression"] == "ts_mean(close, 5)"
|
||||
|
||||
|
||||
def test_list_factors_by_category():
|
||||
"""测试按分类列出因子."""
|
||||
_REGISTRY.clear()
|
||||
register_factor("ma5", "ts_mean(close, 5)", category="trend")
|
||||
register_factor("rsi", "ts_rank(close, 14)", category="momentum")
|
||||
assert len(list_factors(category="trend")) == 1
|
||||
assert len(list_factors()) == 2
|
||||
|
||||
|
||||
def test_register_duplicate_raises():
|
||||
"""测试重复注册抛出异常."""
|
||||
_REGISTRY.clear()
|
||||
register_factor("ma5", "ts_mean(close, 5)", category="trend")
|
||||
with pytest.raises(ValueError, match="因子已存在"):
|
||||
register_factor("ma5", "ts_mean(close, 10)", category="trend")
|
||||
|
||||
|
||||
def test_get_nonexistent_factor_returns_none():
|
||||
"""测试获取不存在的因子返回 None."""
|
||||
_REGISTRY.clear()
|
||||
assert get_factor("nonexistent") is None
|
||||
|
||||
|
||||
def test_list_factors_empty_registry():
|
||||
"""测试空注册表返回空列表."""
|
||||
_REGISTRY.clear()
|
||||
assert list_factors() == []
|
||||
assert list_factors(category="trend") == []
|
||||
Reference in New Issue
Block a user