Interval中增加个SECOND
BarGenerator 增加两个函数,为什么秒周期出来的出来的数据跟1分钟的一样,还差什么地方呢?谢谢
def update_sec_tick(self, tick: TickData) -> None:
"""
Update new tick data into generator.
"""
new_sec = False
if not self.bar:
new_sec = True
elif not (self.bar.datetime.second % self.bar.window):
sec = (tick.datetime.second // self.bar.window) * self.bar.window
self.bar.datetime = self.bar.datetime.replace(
second=sec, microsecond=0
)
self.on_bar(self.bar)
new_sec = True
if new_sec:
self.bar = BarData(
symbol=tick.symbol,
exchange=tick.exchange,
interval=Interval.SECOND,
datetime=tick.datetime,
gateway_name=tick.gateway_name,
open_price=tick.last_price,
high_price=tick.last_price,
low_price=tick.last_price,
close_price=tick.last_price,
open_interest=tick.open_interest
)
else:
self.bar.high_price = max(self.bar.high_price, tick.last_price)
if tick.high_price > self.last_tick.high_price:
self.bar.high_price = max(self.bar.high_price, tick.high_price)
self.bar.low_price = min(self.bar.low_price, tick.last_price)
if tick.low_price < self.last_tick.low_price:
self.bar.low_price = min(self.bar.low_price, tick.low_price)
self.bar.close_price = tick.last_price
self.bar.open_interest = tick.open_interest
self.bar.datetime = tick.datetime
if self.last_tick:
volume_change = tick.volume - self.last_tick.volume
self.bar.volume += max(volume_change, 0)
turnover_change = tick.turnover - self.last_tick.turnover
self.bar.turnover += max(turnover_change, 0)
self.last_tick = tick
def update_bar_second_window(self, bar: BarData) -> None:
""""""
# If not inited, create window bar object
if not self.window_bar:
sec = (bar.datetime.second // self.window) * self.window
dt = bar.datetime.replace(second=sec, microsecond=0)
self.window_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
)
# Otherwise, update high/low price into window bar
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
)
# Update close price/volume/turnover into window bar
self.window_bar.close_price = bar.close_price
self.window_bar.volume += bar.volume
self.window_bar.turnover += bar.turnover
self.window_bar.open_interest = bar.open_interest
# Check if window bar completed
# if not (bar.datetime.second + 1) % self.window:
if not (bar.datetime.second % self.window):
self.on_window_bar(self.window_bar)
self.window_bar = None