- 新增 finance/tests/ 6 个测试套件(agents/backtest/dao_upsert/factors/features/fundamental_lookahead) - 数据层: data_manager / dao 优化,新增 upsert 逻辑 - 因子层: 基本面因子抽象定位 _mapping、ROE/PE/PB 重构 - 回测层: vectorbt/engine 大改动(251 行),report 增强 - ML 层: features/backtest_integration 特征工程与回测优化 - CLI: agent_cli 重构 - config/settings 扩充配置项
89 lines
2.5 KiB
Python
89 lines
2.5 KiB
Python
"""
|
|
PE / PB 估值因子。
|
|
|
|
基于日线收盘价 + 财务数据(EPS/每股净资产)计算。
|
|
"""
|
|
|
|
import pandas as pd
|
|
|
|
from factors.base import BaseFactor
|
|
from factors.fundamental._mapping import map_fundamental_to_daily
|
|
|
|
|
|
class PEFactor(BaseFactor):
|
|
"""
|
|
市盈率因子 = close / eps。
|
|
|
|
eps 来自财务数据中的 'eps' 列或 TTM EPS。
|
|
因子值越大表示估值越贵。
|
|
"""
|
|
|
|
category = "fundamental"
|
|
|
|
def __init__(self, financial_df: pd.DataFrame | None = None):
|
|
"""
|
|
参数:
|
|
financial_df: 含 'end_date' 和 'eps' 的 DataFrame。
|
|
"""
|
|
self._financial_df = financial_df
|
|
self.name = "pe"
|
|
|
|
def calculate(self, df: pd.DataFrame) -> pd.Series:
|
|
if self._financial_df is None or self._financial_df.empty:
|
|
return pd.Series(float("nan"), index=df.index)
|
|
|
|
eps_series = map_fundamental_to_daily(df, self._financial_df, "eps")
|
|
close = df["close"]
|
|
return close / eps_series.replace(0, float("nan"))
|
|
|
|
def get_required_columns(self) -> list[str]:
|
|
return ["close"]
|
|
|
|
|
|
class PBFactor(BaseFactor):
|
|
"""
|
|
市净率因子 = close / bvps(每股净资产)。
|
|
|
|
因子值越大表示估值越贵。
|
|
"""
|
|
|
|
category = "fundamental"
|
|
|
|
def __init__(self, financial_df: pd.DataFrame | None = None):
|
|
self._financial_df = financial_df
|
|
self.name = "pb"
|
|
|
|
def calculate(self, df: pd.DataFrame) -> pd.Series:
|
|
if self._financial_df is None or self._financial_df.empty:
|
|
return pd.Series(float("nan"), index=df.index)
|
|
|
|
bvps_series = map_fundamental_to_daily(df, self._financial_df, "bvps")
|
|
return df["close"] / bvps_series.replace(0, float("nan"))
|
|
|
|
def get_required_columns(self) -> list[str]:
|
|
return ["close"]
|
|
|
|
|
|
class EPFactor(BaseFactor):
|
|
"""
|
|
盈利收益率因子 = eps / close = 1 / PE。
|
|
|
|
值越大表示估值越便宜,适合与动量等因子同向排序。
|
|
"""
|
|
|
|
category = "fundamental"
|
|
|
|
def __init__(self, financial_df: pd.DataFrame | None = None):
|
|
self._financial_df = financial_df
|
|
self.name = "ep"
|
|
|
|
def calculate(self, df: pd.DataFrame) -> pd.Series:
|
|
if self._financial_df is None or self._financial_df.empty:
|
|
return pd.Series(float("nan"), index=df.index)
|
|
|
|
eps_series = map_fundamental_to_daily(df, self._financial_df, "eps")
|
|
return eps_series / df["close"].replace(0, float("nan")) * 100
|
|
|
|
def get_required_columns(self) -> list[str]:
|
|
return ["close"]
|