在2.2.0版本原有的逻辑上增加了日线合成,独立的函数合成日线,避免耦合,实测2.3.0版本有效
下图中增加了日线合成的入口
def update_bar_day_window(self, bar: BarData) -> None:
if not self.day_bar:
dt = bar.datetime.replace(hour=0, minute=0, second=0, microsecond=0) # 日计算,替换掉小时,分钟,秒,毫秒
self.day_bar = BarData(
symbol=bar.symbol,
exchange=bar.exchange,
datetime=dt,
gateway_name=bar.gateway_name,
open_price=bar.open_price,
high_price=bar.high_price,
low_price=bar.low_price,
volume=bar.volume
)
return
finished_bar = None
# 14:59收盘
if bar.datetime.hour == 14 and bar.datetime.minute == 59:
self.day_bar.high_price = max(
self.day_bar.high_price,
bar.high_price
)
self.day_bar.low_price = min(
self.day_bar.low_price,
bar.low_price
)
self.day_bar.close_price = bar.close_price
self.day_bar.volume += int(bar.volume)
self.day_bar.open_interest = bar.open_interest
finished_bar = self.day_bar
finished_bar.datetime = bar.datetime.replace(hour=0, minute=0, second=0, microsecond=0)
self.day_bar = None
# 未收盘则比价
else:
self.day_bar.high_price = max(
self.day_bar.high_price,
bar.high_price
)
self.day_bar.low_price = min(
self.day_bar.low_price,
bar.low_price
)
self.day_bar.close_price = bar.close_price
self.day_bar.volume += int(bar.volume)
self.day_bar.open_interest = bar.open_interest
# Push finished window bar
if finished_bar:
self.on_day_bar(finished_bar)
# print("finished_bar", finished_bar.datetime, finished_bar.close_price)
# Cache last bar object
self.last_bar = bar
def on_day_bar(self, bar: BarData) -> None:
""""""
if self.window == 1:
self.on_window_bar(bar)
else:
if not self.window_bar:
self.window_bar = BarData(
symbol=bar.symbol,
exchange=bar.exchange,
datetime=bar.datetime,
gateway_name=bar.gateway_name,
open_price=bar.open_price,
high_price=bar.high_price,
low_price=bar.low_price
)
else:
self.window_bar.high_price = max(
self.window_bar.high_price,
bar.high_price
)
self.window_bar.low_price = min(
self.window_bar.low_price,
bar.low_price
)
self.window_bar.close_price = bar.close_price
self.window_bar.volume += int(bar.volume)
self.window_bar.open_interest = bar.open_interest
self.interval_count += 1
if not self.interval_count % self.window:
self.interval_count = 0
self.on_window_bar(self.window_bar)
self.window_bar = None