feat(rotation): 支持混合数据源并优化因子计算和策略逻辑
- 删除旧的Tushare Token环境变量函数,简化配置 - 在配置文件中新增全市场指数及SSH隧道配置支持YFinance数据访问 - 更新compute_factors函数,支持长格式混合数据源,兼容旧宽格式数据 - 修改RotationStrategy使用HybridDataSource,支持Tushare与YFinance数据源混合 - 添加SSH隧道支持,实现安全访问非主市场数据 - 优化因子计算逻辑,提升缺失值处理和因子合并的鲁棒性 - 修正基准净值计算,兼容长宽格式基准数据处理 - 增强信号生成逻辑,处理因子得分中的NaN情况防止异常
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
ETF轮动策略引擎
|
||||
|
||||
整合信号生成和回测逻辑
|
||||
使用 YFinance 数据源(支持 SSH 隧道)
|
||||
"""
|
||||
|
||||
import pandas as pd
|
||||
@@ -9,7 +10,7 @@ import numpy as np
|
||||
from typing import Optional
|
||||
|
||||
from strategies.base import BacktestStrategy
|
||||
from core.data.tushare_source import TushareDataSource
|
||||
from core.data.hybrid_source import HybridDataSource
|
||||
from core.factors.momentum import compute_factors, calculate_daily_return
|
||||
|
||||
|
||||
@@ -18,7 +19,16 @@ class RotationStrategy(BacktestStrategy):
|
||||
|
||||
def __init__(self, config: dict):
|
||||
super().__init__("ETF轮动策略", config)
|
||||
self.data_source = TushareDataSource(use_cache=config.get("use_cache", True))
|
||||
|
||||
# 初始化混合数据源
|
||||
ssh_config = config.get("ssh_tunnel", {})
|
||||
self.data_source = HybridDataSource(
|
||||
ssh_config=ssh_config,
|
||||
use_cache=config.get("use_cache", True)
|
||||
)
|
||||
print(f"使用混合数据源: Tushare(中国A股) + YFinance(港股/美股/加密货币)")
|
||||
print(f"SSH隧道: {ssh_config.get('enabled', False)}")
|
||||
|
||||
self.data = None
|
||||
self.signals = None
|
||||
self.backtest_result = None
|
||||
@@ -30,12 +40,14 @@ class RotationStrategy(BacktestStrategy):
|
||||
# 从配置中读取基准代码,或使用默认值
|
||||
benchmark_code = self.config.get("benchmark", {}).get("code", DEFAULT_BENCHMARK_CODE)
|
||||
|
||||
etf_data, benchmark_data, valid_codes = self.data_source.fetch_all(
|
||||
self.config["code_list"],
|
||||
benchmark_code,
|
||||
self.config["start_date"],
|
||||
self.config["end_date"],
|
||||
)
|
||||
# 使用上下文管理器管理 SSH 隧道(如果是 YFinance 数据源)
|
||||
with self.data_source:
|
||||
etf_data, benchmark_data, valid_codes = self.data_source.fetch_all(
|
||||
self.config["code_list"],
|
||||
benchmark_code,
|
||||
self.config["start_date"],
|
||||
self.config["end_date"],
|
||||
)
|
||||
|
||||
self.etf_data = etf_data
|
||||
self.benchmark_data = benchmark_data
|
||||
@@ -65,6 +77,9 @@ class RotationStrategy(BacktestStrategy):
|
||||
rebalance_threshold = self.config["rebalance_threshold"]
|
||||
|
||||
# Step 1: 每日目标组合
|
||||
if not score_cols:
|
||||
raise ValueError("没有有效的指数代码,无法生成信号")
|
||||
|
||||
if select_num == 1:
|
||||
daily_target = (
|
||||
result[score_cols]
|
||||
@@ -74,7 +89,11 @@ class RotationStrategy(BacktestStrategy):
|
||||
else:
|
||||
def top_n_codes(row):
|
||||
scores = pd.to_numeric(row[score_cols], errors="coerce")
|
||||
top = scores.nlargest(select_num).index.tolist()
|
||||
# 过滤掉 NaN 值
|
||||
scores = scores.dropna()
|
||||
if len(scores) == 0:
|
||||
return ""
|
||||
top = scores.nlargest(min(select_num, len(scores))).index.tolist()
|
||||
return ",".join([c.replace("得分_", "") for c in top])
|
||||
daily_target = result.apply(top_n_codes, axis=1)
|
||||
|
||||
@@ -216,7 +235,17 @@ class RotationStrategy(BacktestStrategy):
|
||||
result[f"净值_{code}"] = result[code] / first_price
|
||||
|
||||
# 基准净值
|
||||
bench_ret = self.benchmark_data.pct_change().dropna()
|
||||
# benchmark_data 是 DataFrame,需要提取 close 列
|
||||
if isinstance(self.benchmark_data, pd.DataFrame):
|
||||
if 'close' in self.benchmark_data.columns:
|
||||
bench_close = self.benchmark_data['close']
|
||||
else:
|
||||
# 宽格式数据
|
||||
bench_close = self.benchmark_data.iloc[:, 0]
|
||||
else:
|
||||
bench_close = self.benchmark_data
|
||||
|
||||
bench_ret = bench_close.pct_change().dropna()
|
||||
common_dates = result.index.intersection(bench_ret.index)
|
||||
bench_ret = bench_ret.loc[common_dates]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user