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>
52 lines
1.5 KiB
Python
52 lines
1.5 KiB
Python
"""
|
|
因子阈值交叉策略。
|
|
|
|
通用策略:任意因子上穿/下穿阈值 → 交易信号。
|
|
|
|
支持:
|
|
- 上穿买入 (cross_up: close < MA → cross above MA → buy)
|
|
- 下穿买入 (cross_down: RSI > 70 → cross below 30 → buy)
|
|
"""
|
|
|
|
import pandas as pd
|
|
|
|
from backtest.base import BaseStrategy
|
|
from backtest.signal import factor_to_threshold_signal
|
|
|
|
|
|
class FactorCrossStrategy(BaseStrategy):
|
|
"""
|
|
因子阈值交叉策略。
|
|
|
|
适用场景:
|
|
- 均线偏离度上穿 0 → 买入(趋势转多)
|
|
- 波动率下穿阈值 → 买入(波动收敛后突破)
|
|
"""
|
|
|
|
category = "trend"
|
|
|
|
def __init__(
|
|
self,
|
|
factor_column: str,
|
|
buy_threshold: float, # 因子大于此值买
|
|
sell_threshold: float | None = None,
|
|
cross_direction: str = "up",
|
|
):
|
|
self.factor_column = factor_column
|
|
self.buy_threshold = buy_threshold
|
|
self.sell_threshold = sell_threshold
|
|
self.cross_direction = cross_direction
|
|
self.name = f"factor_cross_{factor_column}"
|
|
|
|
def generate_signals(self, factor_df: pd.DataFrame) -> pd.Series:
|
|
if self.factor_column not in factor_df.columns:
|
|
raise ValueError(f"factor_df 缺少 '{self.factor_column}' 列")
|
|
|
|
factor = factor_df[self.factor_column]
|
|
return factor_to_threshold_signal(
|
|
factor,
|
|
buy_threshold=self.buy_threshold,
|
|
sell_threshold=self.sell_threshold,
|
|
cross_direction=self.cross_direction,
|
|
)
|