tqsdk实盘

This commit is contained in:
2025-07-10 15:07:31 +08:00
parent 4c243a4b47
commit 5de1a43b02
26 changed files with 12462 additions and 17535 deletions

View File

@@ -96,6 +96,12 @@ class TqsdkEngine:
self.trade_history: List[Trade] = []
self.all_bars: List[Bar] = [] # 收集所有处理过的Bar
self.close_list: List[float] = []
self.open_list: List[float] = []
self.high_list: List[float] = []
self.low_list: List[float] = []
self.volume_list: List[float] = []
self.last_processed_bar: Optional[Bar] = None
self._is_rollover_bar: bool = False # 换月信号
self._last_underlying_symbol = self.symbol # 用于检测主力合约换月
@@ -316,6 +322,8 @@ class TqsdkEngine:
"""
print(f"TqsdkEngine: 开始运行回测,从 {self.start_time}{self.end_time}")
self._strategy.trading = True
# 初始化策略 (如果策略有 on_init 方法)
if hasattr(self._strategy, "on_init"):
self._strategy.on_init()
@@ -416,6 +424,13 @@ class TqsdkEngine:
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
@@ -441,6 +456,13 @@ class TqsdkEngine:
close_oi=kline_row.close_oi,
)
self.all_bars[-1] = 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.last_processed_bar = current_bar
# 设置当前 Bar 到 Context
@@ -489,3 +511,15 @@ class TqsdkEngine:
def get_bar_history(self):
return self.all_bars
def get_price_history(self, key: str):
if key == 'close':
return self.close_list
elif key == 'open':
return self.open_list
elif key == 'high':
return self.high_list
elif key == 'low':
return self.low_list
elif key == 'volume':
return self.volume_list