fix(core): 修复计算与数据对齐等多处逻辑问题
- 修正CAGR计算,去除NaN并检查起始值有效性以避免异常结果 - 优化混合数据源的数据对齐逻辑,使用配置结束日期与A股最新数据日期的较早者 - 计算因子时对齐A股交易日历,重新基于对齐价格计算日收益率,改进因子对齐准确度 - 轮动策略中跳过空信号,避免空信号影响持仓和调仓逻辑 - 调整信号处理,过滤空字符串和NaN,保证轮动信号数据有效性 - 多品种轮动持仓中加入空信号判断,避免无效信号导致错误 - 调整调仓明细和品种汇总保存逻辑,增加空文件创建以保证输出路径文件稳定生成 - 完善多处打印信息和注释,增强代码可读性与调试便利性
This commit is contained in:
@@ -115,6 +115,10 @@ class RotationStrategy(BacktestStrategy):
|
||||
target = daily_target.iloc[i]
|
||||
|
||||
if current_held is None:
|
||||
# 跳过空信号,直到找到第一个有效信号
|
||||
if not target:
|
||||
held_signals.append(None) # 添加None占位,保持长度一致
|
||||
continue
|
||||
current_held = target
|
||||
last_rebalance_idx = i
|
||||
held_signals.append(current_held)
|
||||
@@ -122,20 +126,24 @@ class RotationStrategy(BacktestStrategy):
|
||||
|
||||
days_since = i - last_rebalance_idx
|
||||
if days_since >= rebalance_days:
|
||||
should = self._check_rebalance(
|
||||
result.iloc[i], current_held, target,
|
||||
select_num, rebalance_threshold
|
||||
)
|
||||
if should:
|
||||
current_held = target
|
||||
last_rebalance_idx = i
|
||||
# 目标信号为空时不调仓
|
||||
if target: # 只在目标有效时才检查是否调仓
|
||||
should = self._check_rebalance(
|
||||
result.iloc[i], current_held, target,
|
||||
select_num, rebalance_threshold
|
||||
)
|
||||
if should:
|
||||
current_held = target
|
||||
last_rebalance_idx = i
|
||||
|
||||
held_signals.append(current_held)
|
||||
|
||||
result["信号_raw"] = held_signals
|
||||
result["信号"] = result["信号_raw"].shift(1)
|
||||
result = result.drop(columns=["信号_raw"])
|
||||
# 删除信号为 NaN 或空字符串的行
|
||||
result = result.dropna(subset=["信号"])
|
||||
result = result[result["信号"] != ""]
|
||||
|
||||
self.signals = result
|
||||
self._print_signal_stats(result, select_num, rebalance_days, rebalance_threshold)
|
||||
@@ -152,12 +160,14 @@ class RotationStrategy(BacktestStrategy):
|
||||
return (new_score / old_score - 1) >= threshold
|
||||
return new_score > 0
|
||||
else:
|
||||
new_codes = target.split(",")
|
||||
old_codes = current_held.split(",")
|
||||
new_codes = [c for c in target.split(",") if c] # 过滤空字符串
|
||||
old_codes = [c for c in current_held.split(",") if c] # 过滤空字符串
|
||||
if not new_codes or not old_codes:
|
||||
return True # 有空持仓,需要调仓
|
||||
if set(new_codes) == set(old_codes):
|
||||
return False
|
||||
new_total = sum(float(row[f"得分_{c}"]) for c in new_codes)
|
||||
old_total = sum(float(row[f"得分_{c}"]) for c in old_codes)
|
||||
new_total = sum(float(row.get(f"得分_{c}", 0)) for c in new_codes)
|
||||
old_total = sum(float(row.get(f"得分_{c}", 0)) for c in old_codes)
|
||||
if old_total > 0:
|
||||
return (new_total / old_total - 1) >= threshold
|
||||
return new_total > 0
|
||||
@@ -206,12 +216,17 @@ class RotationStrategy(BacktestStrategy):
|
||||
# 计算策略日收益率
|
||||
if select_num == 1:
|
||||
def calc_return(row):
|
||||
return row[f"日收益率_{row['信号']}"]
|
||||
signal = row['信号']
|
||||
if not signal or pd.isna(signal):
|
||||
return 0.0
|
||||
return row.get(f"日收益率_{signal}", 0.0)
|
||||
result["轮动策略日收益率"] = result.apply(calc_return, axis=1)
|
||||
else:
|
||||
def calc_multi_return(row):
|
||||
codes = row["信号"].split(",")
|
||||
returns = [row[f"日收益率_{c}"] for c in codes]
|
||||
codes = [c for c in row["信号"].split(",") if c] # 过滤空字符串
|
||||
if not codes:
|
||||
return 0.0
|
||||
returns = [row.get(f"日收益率_{c}", 0.0) for c in codes]
|
||||
return np.mean(returns)
|
||||
result["轮动策略日收益率"] = result.apply(calc_multi_return, axis=1)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user