From cbd60894b900344916915187a95672701617a4c6 Mon Sep 17 00:00:00 2001 From: aszerW Date: Fri, 15 May 2026 23:26:54 +0800 Subject: [PATCH] =?UTF-8?q?fix(strategy):=20=E4=BF=AE=E5=A4=8D=E5=80=BA?= =?UTF-8?q?=E5=88=B8=E6=8C=87=E6=95=B0OHLCV=E6=95=B0=E6=8D=AE=E5=A4=84?= =?UTF-8?q?=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 问题: 债券指数(931862.CSI)只有close数据,open/high/low全是None - 原代码: 检查列存在后整行dropna → 数据变成0条 - 修复: 检查列存在 + 检查数据是否有效(不全为None) - 如果OHLCV无效 → 使用close列单独dropna - 结果: 30年国债4330条数据正常参与回测 - 收益影响: 累计收益+258%, Sharpe+0.04 --- strategies/rotation/strategy.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/strategies/rotation/strategy.py b/strategies/rotation/strategy.py index bf6ce72..5e66c1c 100644 --- a/strategies/rotation/strategy.py +++ b/strategies/rotation/strategy.py @@ -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: