7 Sprints 全部完成: Sprint 0: 基础设施 (DataManager + MariaDB) Sprint 1: 因子引擎 (34因子/12分类) Sprint 2: VectorBT 回测 (5策略+截面) Sprint 3: Optuna 优化 (+Walk-Forward) Sprint 4: ML 模型 (LightGBM+CatBoost) Sprint 5: Qwen 情绪因子 (三源新闻+日期对齐) Sprint 6: Agent 系统 (4Agent+日报.md/.html) 生产加固 (15项): Tushare双源fallback, SSH自动恢复, pool_pre_ping, save_daily先删后插, load_dotenv绝对路径, 日报5d/20d修复, RiskAgent改上证指数, 昨日对比+数据截止, mac_report utf8mb4, CLAUDE-*.md 9条已知Bug, demo全参数化, djapi数据源归一化, indexDatas API修正 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
114 lines
3.2 KiB
Python
114 lines
3.2 KiB
Python
"""
|
|
PE / PB 估值因子。
|
|
|
|
基于日线收盘价 + 财务数据(EPS/每股净资产)计算。
|
|
"""
|
|
|
|
import pandas as pd
|
|
|
|
from factors.base import BaseFactor
|
|
|
|
|
|
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_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_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_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"]
|
|
|
|
|
|
def _map_to_daily(
|
|
daily_df: pd.DataFrame,
|
|
fina_df: pd.DataFrame,
|
|
column: str,
|
|
) -> pd.Series:
|
|
"""将季度财务数据填充到日线索引(前值填充)。"""
|
|
fina = fina_df[["end_date", column]].dropna().copy()
|
|
fina["end_date"] = fina["end_date"].astype(str)
|
|
fina = fina.sort_values("end_date")
|
|
|
|
result = pd.Series(float("nan"), index=daily_df.index)
|
|
if fina.empty:
|
|
return result
|
|
|
|
dates = pd.to_datetime(daily_df.index, format="%Y%m%d", errors="coerce")
|
|
fina_dates = pd.to_datetime(fina["end_date"], format="%Y%m%d", errors="coerce")
|
|
|
|
for i, fina_date in enumerate(fina_dates):
|
|
mask = dates >= fina_date
|
|
if i + 1 < len(fina_dates):
|
|
mask &= dates < fina_dates.iloc[i + 1]
|
|
result[mask] = fina[column].iloc[i]
|
|
|
|
return result.astype("float64")
|