Initial commit: cc-cursor 全链路量化研究平台
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>
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
"""
|
||||
策略优化快捷函数。
|
||||
|
||||
为常用策略提供一键优化入口。
|
||||
"""
|
||||
|
||||
import pandas as pd
|
||||
|
||||
from backtest.vectorbt.engine import VectorBTEngine
|
||||
from optimizer.engine import OptunaEngine
|
||||
from optimizer.result import OptimizationResult, WalkForwardResult
|
||||
from optimizer.space import (
|
||||
sma_cross_space,
|
||||
rsi_revert_space,
|
||||
momentum_breakout_space,
|
||||
factor_cross_space,
|
||||
)
|
||||
|
||||
_DEFAULT_TRIALS = 100
|
||||
|
||||
|
||||
def optimize_sma_cross(
|
||||
price_df: pd.DataFrame,
|
||||
factor_df: pd.DataFrame | None = None,
|
||||
bt_engine: VectorBTEngine | None = None,
|
||||
n_trials: int = _DEFAULT_TRIALS,
|
||||
metric: str = "sharpe",
|
||||
) -> OptimizationResult:
|
||||
"""均线交叉策略参数寻优。"""
|
||||
from backtest.strategies.sma_cross import SMACrossStrategy
|
||||
return OptunaEngine(bt_engine).optimize(
|
||||
SMACrossStrategy, sma_cross_space, price_df, factor_df, metric, n_trials,
|
||||
)
|
||||
|
||||
|
||||
def optimize_rsi_revert(
|
||||
price_df: pd.DataFrame,
|
||||
factor_df: pd.DataFrame | None = None,
|
||||
bt_engine: VectorBTEngine | None = None,
|
||||
n_trials: int = _DEFAULT_TRIALS,
|
||||
metric: str = "sharpe",
|
||||
) -> OptimizationResult:
|
||||
"""RSI 反转策略参数寻优。"""
|
||||
from backtest.strategies.rsi_mean_revert import RSIMeanRevertStrategy
|
||||
return OptunaEngine(bt_engine).optimize(
|
||||
RSIMeanRevertStrategy, rsi_revert_space, price_df, factor_df, metric, n_trials,
|
||||
)
|
||||
|
||||
|
||||
def optimize_momentum_breakout(
|
||||
price_df: pd.DataFrame,
|
||||
factor_df: pd.DataFrame | None = None,
|
||||
bt_engine: VectorBTEngine | None = None,
|
||||
n_trials: int = _DEFAULT_TRIALS,
|
||||
metric: str = "sharpe",
|
||||
) -> OptimizationResult:
|
||||
"""动量突破策略参数寻优。"""
|
||||
from backtest.strategies.momentum_breakout import MomentumBreakoutStrategy
|
||||
return OptunaEngine(bt_engine).optimize(
|
||||
MomentumBreakoutStrategy, momentum_breakout_space, price_df, factor_df, metric, n_trials,
|
||||
)
|
||||
|
||||
|
||||
def optimize_factor_cross(
|
||||
price_df: pd.DataFrame,
|
||||
factor_column: str,
|
||||
factor_df: pd.DataFrame | None = None,
|
||||
bt_engine: VectorBTEngine | None = None,
|
||||
n_trials: int = _DEFAULT_TRIALS,
|
||||
metric: str = "sharpe",
|
||||
) -> OptimizationResult:
|
||||
"""因子阈值交叉策略参数寻优。
|
||||
|
||||
参数:
|
||||
factor_column: 因子列名(如 'momentum_20')
|
||||
其余同 optimize_* 系列。
|
||||
"""
|
||||
from backtest.strategies.factor_cross import FactorCrossStrategy
|
||||
|
||||
class _FCS(FactorCrossStrategy):
|
||||
def __init__(self, buy_threshold=0, sell_threshold=None):
|
||||
super().__init__(factor_column, buy_threshold, sell_threshold)
|
||||
|
||||
return OptunaEngine(bt_engine).optimize(
|
||||
_FCS, factor_cross_space, price_df, factor_df, metric, n_trials,
|
||||
)
|
||||
Reference in New Issue
Block a user