mirror of
https://ghfast.top/https://github.com/aeroxw/easy_tdx_max.git
synced 2026-09-12 18:04:20 +08:00
style: ruff format factor module
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
"""因子 CLI 命令。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
# 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
|
||||
|
||||
# 导入 builtin 触发自动注册
|
||||
from easy_tdx.factor.builtin import get_factor, list_factors # noqa: F401
|
||||
from easy_tdx.factor.engine import FactorEngine
|
||||
|
||||
__all__ = [
|
||||
"Factor",
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
# src/easy_tdx/factor/base.py
|
||||
"""因子基类与全局注册表。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
@@ -32,9 +33,7 @@ class Factor(ABC):
|
||||
def __init__(self) -> None:
|
||||
for attr in ("name", "category", "description", "inputs"):
|
||||
if not hasattr(self, attr):
|
||||
raise TypeError(
|
||||
f"Factor 子类 {type(self).__name__} 必须定义类属性 '{attr}'"
|
||||
)
|
||||
raise TypeError(f"Factor 子类 {type(self).__name__} 必须定义类属性 '{attr}'")
|
||||
|
||||
|
||||
FACTORY_REGISTRY: dict[str, type[Factor]] = {}
|
||||
@@ -47,8 +46,6 @@ def register_factor(cls: type[Factor]) -> type[Factor]:
|
||||
ValueError: 如果 name 已被注册。
|
||||
"""
|
||||
if cls.name in FACTORY_REGISTRY:
|
||||
raise ValueError(
|
||||
f"因子 '{cls.name}' 已注册(类: {FACTORY_REGISTRY[cls.name].__name__})"
|
||||
)
|
||||
raise ValueError(f"因子 '{cls.name}' 已注册(类: {FACTORY_REGISTRY[cls.name].__name__})")
|
||||
FACTORY_REGISTRY[cls.name] = cls
|
||||
return cls
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
"""内置因子库 — 导入子模块触发注册。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from easy_tdx.factor.base import FACTORY_REGISTRY, Factor
|
||||
@@ -35,7 +36,5 @@ def get_factor(name: str) -> type[Factor]:
|
||||
ValueError: 因子不存在。
|
||||
"""
|
||||
if name not in FACTORY_REGISTRY:
|
||||
raise ValueError(
|
||||
f"未知因子: {name!r}。可用因子: {sorted(FACTORY_REGISTRY.keys())}"
|
||||
)
|
||||
raise ValueError(f"未知因子: {name!r}。可用因子: {sorted(FACTORY_REGISTRY.keys())}")
|
||||
return FACTORY_REGISTRY[name]
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
"""缠论因子 — 桥接 ChanlunAnalyser。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import numpy as np
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
"""动量类因子。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pandas as pd
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
"""质量类因子。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import numpy as np
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
"""技术指标因子 — 桥接 MyTT 指标库。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import numpy as np
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
"""价值类因子(需要财务数据扩展,当前为占位实现)。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pandas as pd
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
"""波动率类因子。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import numpy as np
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
"""成交量类因子。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import numpy as np
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
# src/easy_tdx/factor/engine.py
|
||||
"""因子计算引擎 — 单股计算与截面批量计算。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import numpy as np
|
||||
@@ -14,9 +15,7 @@ def _resolve_factor(f: str | Factor) -> Factor:
|
||||
return f
|
||||
name = f.strip()
|
||||
if name not in FACTORY_REGISTRY:
|
||||
raise ValueError(
|
||||
f"未知因子: {name!r}。可用因子: {sorted(FACTORY_REGISTRY.keys())}"
|
||||
)
|
||||
raise ValueError(f"未知因子: {name!r}。可用因子: {sorted(FACTORY_REGISTRY.keys())}")
|
||||
return FACTORY_REGISTRY[name]()
|
||||
|
||||
|
||||
@@ -113,17 +112,17 @@ class FactorEngine:
|
||||
|
||||
close = df["close"].to_numpy()
|
||||
forward = np.full(len(close), np.nan)
|
||||
forward[: len(close) - period] = (
|
||||
close[period:] / close[: len(close) - period] - 1
|
||||
)
|
||||
forward[: len(close) - period] = close[period:] / close[: len(close) - period] - 1
|
||||
|
||||
dates = df["datetime"].apply(_datetime_to_int)
|
||||
|
||||
sub = pd.DataFrame({
|
||||
"date": dates,
|
||||
"code": code,
|
||||
col_name: forward,
|
||||
})
|
||||
sub = pd.DataFrame(
|
||||
{
|
||||
"date": dates,
|
||||
"code": code,
|
||||
col_name: forward,
|
||||
}
|
||||
)
|
||||
all_frames.append(sub)
|
||||
|
||||
if not all_frames:
|
||||
|
||||
Reference in New Issue
Block a user