fix(core): 修复计算与数据对齐等多处逻辑问题

- 修正CAGR计算,去除NaN并检查起始值有效性以避免异常结果
- 优化混合数据源的数据对齐逻辑,使用配置结束日期与A股最新数据日期的较早者
- 计算因子时对齐A股交易日历,重新基于对齐价格计算日收益率,改进因子对齐准确度
- 轮动策略中跳过空信号,避免空信号影响持仓和调仓逻辑
- 调整信号处理,过滤空字符串和NaN,保证轮动信号数据有效性
- 多品种轮动持仓中加入空信号判断,避免无效信号导致错误
- 调整调仓明细和品种汇总保存逻辑,增加空文件创建以保证输出路径文件稳定生成
- 完善多处打印信息和注释,增强代码可读性与调试便利性
This commit is contained in:
2026-03-26 22:21:38 +08:00
parent 2faea1517f
commit 70bb69fd98
5 changed files with 114 additions and 49 deletions

View File

@@ -639,18 +639,31 @@ class HybridDataSource:
aggfunc='first'
)
# 数据对齐策略:使用各标的能获取到的最新数据
# 以A股最新数据日期为基准,其他市场数据对齐到该日期
# 数据对齐策略:使用配置的日期范围,确保回测区间与配置一致
# 以A股最新数据日期或配置的end_date取较早者为基准
# 获取A股最新数据日期
china_codes = [c for c in valid_codes if self._is_china_index(c)]
if china_codes:
a_share_latest = index_data[china_codes].dropna().index.max()
a_share_data_latest = index_data[china_codes].dropna().index.max()
else:
# 如果没有A股使用所有数据的最早最新日期
a_share_latest = index_data.dropna().index.max()
a_share_data_latest = index_data.dropna().index.max()
print(f" A股最新数据日期: {a_share_latest.strftime('%Y-%m-%d')}")
# 使用配置的end_date但不超过数据的最新日期
config_end = pd.Timestamp(end_date)
a_share_latest = min(a_share_data_latest, config_end)
print(f" A股数据最新日期: {a_share_data_latest.strftime('%Y-%m-%d')}")
print(f" 配置结束日期: {config_end.strftime('%Y-%m-%d')}")
print(f" 实际使用日期: {a_share_latest.strftime('%Y-%m-%d')}")
# 打印各标的的数据时间范围(用于调试)
print(f"\n 各标的数据时间范围:")
for code in valid_codes:
code_data = index_data[code].dropna()
if len(code_data) > 0:
print(f" {code}: {code_data.index.min().strftime('%Y-%m-%d')} ~ {code_data.index.max().strftime('%Y-%m-%d')} ({len(code_data)}条)")
# 获取A股交易日历从start_date到a_share_latest
start_str = pd.Timestamp(start_date).strftime('%Y%m%d')