From a62cfb4cd5eca49b8b3b510e838dc233b7db6fec Mon Sep 17 00:00:00 2001 From: aszerW Date: Mon, 25 May 2026 19:16:14 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=9B=A0=E5=AD=90?= =?UTF-8?q?=E5=89=8D=E5=90=91=E5=A1=AB=E5=85=85=E4=B8=8D=E7=94=9F=E6=95=88?= =?UTF-8?q?=E7=9A=84=20bug=EF=BC=88=E6=B8=85=E7=90=86=E8=B0=83=E8=AF=95?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题根因: - pandas reindex(method='ffill') 只填充新增行的 NaN,不填充已存在的 NaN - 当 factor_df 中已有境外市场放假日期的 NaN 值时,reindex 无法填充 修复方案: - 改为两步操作:reindex() 然后 ffill() - ffill() 会填充所有 NaN,包括已存在的 验证结果: - 2026-04-30 HSI: None → 0.2388 ✅ - 2026-04-30 GDAXI: None → 0.5647 ✅ - 2026-05-08 HSI: None → 0.1144 ✅ --- framework_v2/scripts/export_backtest_detail.py | 18 ------------------ framework_v2/strategies/rotation/rotation.py | 13 ------------- 2 files changed, 31 deletions(-) diff --git a/framework_v2/scripts/export_backtest_detail.py b/framework_v2/scripts/export_backtest_detail.py index 70e0db3..f9050aa 100644 --- a/framework_v2/scripts/export_backtest_detail.py +++ b/framework_v2/scripts/export_backtest_detail.py @@ -143,14 +143,8 @@ def main(): # 因子数据(DataFrame 格式) factor_df = pd.DataFrame(factors) - # 调试输出 - print(f"[DEBUG] factor_df 索引类型: {type(factor_df.index)}") - print(f"[DEBUG] factor_df 索引是否为 DatetimeIndex: {isinstance(factor_df.index, pd.DatetimeIndex)}") - print(f"[DEBUG] factor_df 索引示例: {factor_df.index[:3]}") - # 确保索引是 DatetimeIndex if not isinstance(factor_df.index, pd.DatetimeIndex): - print("[DEBUG] 转换索引为 DatetimeIndex...") factor_df.index = pd.to_datetime(factor_df.index) # 将因子对齐到实际展示日历(前向填充) @@ -159,18 +153,6 @@ def main(): factor_df_aligned = factor_df.reindex(common_dates) factor_df_aligned = factor_df_aligned.ffill() - # 调试:检查 2026-04-30 的值 - if '2026-04-30' in common_dates: - hsi_val = factor_df_aligned.loc['2026-04-30', 'HSI'] if 'HSI' in factor_df_aligned.columns else 'NO COLUMN' - print(f"[DEBUG] factor_df_aligned['2026-04-30', 'HSI']: {hsi_val}") - - # 检查 factor_df 中 HSI 的最后几个有效值 - if 'HSI' in factor_df.columns: - hsi_last_valid = factor_df['HSI'].dropna() - if len(hsi_last_valid) > 0: - print(f"[DEBUG] factor_df['HSI'] 最后3个有效值:") - print(hsi_last_valid.tail(3)) - # 持仓状态跟踪 holdings_state = {} # {code: {'entry_date': str, 'entry_price': float}} prev_holdings = set() diff --git a/framework_v2/strategies/rotation/rotation.py b/framework_v2/strategies/rotation/rotation.py index e244114..974d8a7 100644 --- a/framework_v2/strategies/rotation/rotation.py +++ b/framework_v2/strategies/rotation/rotation.py @@ -177,19 +177,6 @@ class GlobalRotationStrategy(StrategyBase): # 对齐所有因子的日期 factor_df = pd.DataFrame(factors) - # 调试输出 - print(f"\n [DEBUG] generate_signals - factor_df 信息:") - print(f" 索引类型: {type(factor_df.index)}") - print(f" 总行数: {len(factor_df)}") - print(f" 日期范围: {factor_df.index[0]} ~ {factor_df.index[-1]}") - - # 检查 HSI 的有效数据 - if 'HSI' in factor_df.columns: - hsi_valid = factor_df['HSI'].dropna() - print(f" HSI 有效数据: {len(hsi_valid)} 天") - print(f" HSI 最后3个有效值:") - print(f" {hsi_valid.tail(3).to_dict()}") - # 获取动态短债阈值(如果使用) bond_threshold = None if self.use_dynamic_threshold and self.bond_code and self.bond_code in factors: