修复未来函数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

@@ -165,7 +165,11 @@ class TqsdkEngine:
order_to_send.limit_price
if order_to_send.price_type == "LIMIT"
# else self.quote.bid_price1 + (1 if tqsdk_direction == "BUY" else -1)
else self.quote.bid_price1 if tqsdk_direction == "SELL" else self.quote.ask_price1
else (
self.quote.bid_price1
if tqsdk_direction == "SELL"
else self.quote.ask_price1
)
),
)
# 更新原始 Order 对象与 Tqsdk 的订单ID和状态
@@ -358,7 +362,7 @@ class TqsdkEngine:
self.now = now_dt
if self._api.is_changing(self.klines):
if self._api.is_changing(self.klines.iloc[-1]):
kline_row = self.klines.iloc[-1]
kline_dt = pd.to_datetime(kline_row.datetime, unit="ns", utc=True)
kline_dt = kline_dt.tz_convert(BEIJING_TZ)
@@ -382,22 +386,22 @@ class TqsdkEngine:
kline_dt = pd.to_datetime(kline_row.datetime, unit="ns", utc=True)
kline_dt = kline_dt.tz_convert(BEIJING_TZ)
current_bar = Bar(
datetime=kline_dt,
symbol=self._last_underlying_symbol,
open=kline_row.open,
high=kline_row.high,
low=kline_row.low,
close=kline_row.close,
volume=kline_row.volume,
open_oi=kline_row.open_oi,
close_oi=kline_row.close_oi,
)
if (
self.last_processed_bar is None
or self.last_processed_bar.datetime != kline_dt
):
# 创建 core_data.Bar 对象
current_bar = Bar(
datetime=kline_dt,
symbol=self._last_underlying_symbol,
open=kline_row.open,
high=kline_row.high,
low=kline_row.low,
close=kline_row.close,
volume=kline_row.volume,
open_oi=kline_row.open_oi,
close_oi=kline_row.close_oi,
)
# 设置当前 Bar 到 Context
self._context.set_current_bar(current_bar)
@@ -423,19 +427,10 @@ class TqsdkEngine:
else:
self._is_rollover_bar = False
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)
self.last_processed_bar = current_bar
# 调用策略的 on_bar 方法
self._strategy.on_open_bar(current_bar)
self._strategy.on_open_bar(current_bar.open, current_bar.symbol)
# 处理订单和取消请求
self._process_queued_requests()
@@ -443,25 +438,13 @@ class TqsdkEngine:
# 记录投资组合快照
self._record_portfolio_snapshot(current_bar.datetime)
else:
# 创建 core_data.Bar 对象
current_bar = Bar(
datetime=kline_dt,
symbol=self._last_underlying_symbol,
open=kline_row.open,
high=kline_row.high,
low=kline_row.low,
close=kline_row.close,
volume=kline_row.volume,
open_oi=kline_row.open_oi,
close_oi=kline_row.close_oi,
)
self.all_bars[-1] = current_bar
self.all_bars.append(current_bar)
self.close_list[-1] = current_bar.close
self.open_list[-1] = current_bar.open
self.high_list[-1] = current_bar.high
self.low_list[-1] = current_bar.low
self.volume_list[-1] = current_bar.volume
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)
self.last_processed_bar = current_bar
@@ -511,15 +494,15 @@ class TqsdkEngine:
def get_bar_history(self):
return self.all_bars
def get_price_history(self, key: str):
if key == 'close':
if key == "close":
return self.close_list
elif key == 'open':
elif key == "open":
return self.open_list
elif key == 'high':
elif key == "high":
return self.high_list
elif key == 'low':
elif key == "low":
return self.low_list
elif key == 'volume':
elif key == "volume":
return self.volume_list