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