fix(strategy): 修复债券指数OHLCV数据处理逻辑
- 问题: 债券指数(931862.CSI)只有close数据,open/high/low全是None - 原代码: 检查列存在后整行dropna → 数据变成0条 - 修复: 检查列存在 + 检查数据是否有效(不全为None) - 如果OHLCV无效 → 使用close列单独dropna - 结果: 30年国债4330条数据正常参与回测 - 收益影响: 累计收益+258%, Sharpe+0.04
This commit is contained in:
@@ -302,20 +302,29 @@ class RotationStrategy(StrategyBase):
|
||||
for code in valid_codes:
|
||||
df = index_data[code].copy()
|
||||
|
||||
# 检查是否有OHLCV数据
|
||||
# 检查是否有有效的OHLCV数据(列存在且不全为None)
|
||||
ohlcv_cols = ['open', 'high', 'low', 'close', 'volume']
|
||||
has_ohlcv = all(col in df.columns for col in ['open', 'high', 'low', 'close'])
|
||||
required_cols = ['open', 'high', 'low', 'close']
|
||||
|
||||
if has_ohlcv:
|
||||
# 如果有完整OHLCV,整行dropna()后提取close
|
||||
# 检查列是否存在
|
||||
cols_exist = all(col in df.columns for col in required_cols)
|
||||
|
||||
# 检查数据是否有效(不全为None/NaN)
|
||||
if cols_exist:
|
||||
cols_have_data = all(df[col].notna().any() for col in required_cols)
|
||||
else:
|
||||
cols_have_data = False
|
||||
|
||||
if cols_exist and cols_have_data:
|
||||
# 有完整有效的OHLCV数据,整行dropna()后提取close
|
||||
df_clean = df[ohlcv_cols].dropna()
|
||||
close_series = df_clean['close'] if len(df_clean) > 0 else pd.Series(dtype=float)
|
||||
elif 'close' in df.columns and df['close'].notna().any():
|
||||
# 只有close列有效数据(如债券指数)
|
||||
close_series = df['close'].dropna()
|
||||
else:
|
||||
# 只有close列的情况
|
||||
if 'close' in df.columns:
|
||||
close_series = df['close'].dropna()
|
||||
else:
|
||||
close_series = df.dropna()
|
||||
# 无有效数据
|
||||
close_series = pd.Series(dtype=float)
|
||||
|
||||
# 检查数据长度并警告,但不剔除
|
||||
if len(close_series) < self.n_days + 1:
|
||||
|
||||
Reference in New Issue
Block a user