同步本地回测与tqsdk回测

This commit is contained in:
2025-06-29 12:03:43 +08:00
parent 4521939b95
commit 70c3b8186a
20 changed files with 29892 additions and 23140 deletions

View File

@@ -109,6 +109,7 @@ def collect_and_save_tqsdk_data_stream(
)
last_kline_datetime = None # 用于跟踪上一根已完成K线的时间
swap_month_dt = None
while api.wait_update():
if underlying_symbol is None:
@@ -117,7 +118,11 @@ def collect_and_save_tqsdk_data_stream(
# 检查是否有新的完整K线生成或者当前K线是最后一次更新 (在回测结束时)
# TqSdk会在K线完成时发送最后一次更新或者在回测结束时强制更新
if api.is_changing(quote, "underlying_symbol"):
underlying_symbol = quote.underlying_symbol
swap_month_dt = pd.to_datetime(
quote.datetime, unit="ns", utc=True
)
if api.is_changing(klines):
# 只有当K线序列发生变化时才处理
# 关注最新一根 K 线(即 klines.iloc[-1]
@@ -141,10 +146,15 @@ def collect_and_save_tqsdk_data_stream(
kline_dt = pd.to_datetime(
current_kline["datetime"], unit="ns", utc=True
)
kline_dt = kline_dt.tz_convert(BEIJING_TZ).strftime(
).tz_convert(BEIJING_TZ)
if swap_month_dt is not None and kline_dt.hour == swap_month_dt.hour:
underlying_symbol = quote.underlying_symbol
kline_dt = kline_dt.strftime(
"%Y-%m-%d %H:%M:%S"
)
kline_data_to_save = {
"datetime": kline_dt,
"open": current_kline["open"],