修复未来函数bug

This commit is contained in:
2025-07-15 22:45:51 +08:00
parent 5de1a43b02
commit 5a2d0fb984
19 changed files with 15019 additions and 9388 deletions

View File

@@ -105,14 +105,7 @@ class BacktestEngine:
current_bar = self.data_manager.get_next_bar()
if current_bar is None:
break # 没有更多数据,回测结束
self.all_bars.append(current_bar)
self.close_list.append(current_bar.close)
self.open_list.append(current_bar.open)
self.high_list.append(current_bar.high)
self.low_list.append(current_bar.low)
self.volume_list.append(current_bar.volume)
break # 没有更多数据,回测结束
if self.start_time and current_bar.datetime < self.start_time:
continue
@@ -163,14 +156,7 @@ class BacktestEngine:
# 3. 更新策略关注的当前合约 symbol
self.strategy.symbol = current_bar.symbol
# 5. 更新引擎内部的历史 Bar 缓存
self._history_bars.append(current_bar)
if len(self._history_bars) > self._max_history_bars:
self._history_bars.pop(0)
# 6. 处理待撮合订单 (在调用策略 on_bar 之前,确保订单在当前 K 线开盘价撮合)
# self.simulator.process_pending_orders(current_bar)
self.strategy.on_open_bar(current_bar)
self.strategy.on_open_bar(current_bar.open, current_bar.symbol)
current_indicator_dict = {}
close_array = np.array(self.close_list)
@@ -178,7 +164,7 @@ class BacktestEngine:
high_array = np.array(self.high_list)
low_array = np.array(self.low_list)
volume_array = np.array(self.volume_list)
for indicator in self.indicators:
current_indicator_dict[indicator.get_name()] = indicator.get_latest_value(
close_array,
@@ -187,10 +173,18 @@ class BacktestEngine:
low_array,
volume_array
)
self.simulator.process_pending_orders(current_bar, current_indicator_dict)
self.all_bars.append(current_bar)
self.close_list.append(current_bar.close)
self.open_list.append(current_bar.open)
self.high_list.append(current_bar.high)
self.low_list.append(current_bar.low)
self.volume_list.append(current_bar.volume)
# 7. 调用策略的 on_bar 方法
# self.strategy.on_bar(current_bar)
self.simulator.process_pending_orders(current_bar, current_indicator_dict)
self.strategy.on_close_bar(current_bar)
self.simulator.process_pending_orders(current_bar, current_indicator_dict)
@@ -277,4 +271,5 @@ class BacktestEngine:
return self.low_list
elif key == 'volume':
return self.volume_list
return None