fix: 修复回测日期对齐问题,优化收益率计算
- 使用对齐后的index_close数据计算日收益率 - 添加日期对齐逻辑确保信号和收益率数据一致 - 修复pivot重复索引问题,使用pivot_table - 修复tushare期货接口调用(futures_daily -> fut_daily) 回测结果: - 最终净值: 0.9435 - 累计收益: -5.65% - 信号日期: 2302天
This commit is contained in:
@@ -197,14 +197,33 @@ class RotationStrategy(StrategyBase):
|
||||
|
||||
# 4. 执行回测
|
||||
print("\n执行回测...")
|
||||
returns_data = {}
|
||||
first_code = valid_codes[0]
|
||||
for code in valid_codes:
|
||||
df = index_data[code]
|
||||
returns_data[f'日收益率_{code}'] = df['close'].pct_change()
|
||||
|
||||
returns_df = pd.DataFrame(returns_data)
|
||||
returns_df.index = index_data[first_code].index
|
||||
# 使用对齐后的指数收盘价数据获取日期基准
|
||||
index_close = data.get('index_close')
|
||||
|
||||
# 计算日收益率(使用对齐后的收盘价数据)
|
||||
if index_close is not None and not index_close.empty:
|
||||
returns_df = index_close.pct_change()
|
||||
returns_df.columns = [f'日收益率_{col}' for col in returns_df.columns]
|
||||
else:
|
||||
# 回退到原始数据
|
||||
returns_data = {}
|
||||
for code in valid_codes:
|
||||
if code in index_data:
|
||||
df = index_data[code]
|
||||
returns_data[f'日收益率_{code}'] = df['close'].pct_change()
|
||||
returns_df = pd.DataFrame(returns_data)
|
||||
|
||||
if valid_codes:
|
||||
first_code = valid_codes[0]
|
||||
returns_df.index = index_data[first_code].index
|
||||
|
||||
# 确保信号和收益率数据日期对齐
|
||||
common_dates = signals.index.intersection(returns_df.index)
|
||||
signals = signals.loc[common_dates]
|
||||
returns_df = returns_df.loc[common_dates]
|
||||
|
||||
print(f" 对齐后日期: {len(common_dates)} 天")
|
||||
|
||||
executor = BacktestExecutor(
|
||||
initial_capital=100000,
|
||||
|
||||
Reference in New Issue
Block a user