问题1
demo_strategy里打开BarGenerator后看到的对generate函数定义的代码
def generate(self) -> Optional[BarData]:
"""
Generate the bar data and call callback immediately.
"""
bar = self.bar
if self.bar:
bar.datetime = bar.datetime.replace(second=0, microsecond=0)
self.on_bar(bar)
self.bar = None
return bar
08课时视频里对generate函数定义的代码如下所示
def generate(self) -> Optional[BarData]:
"""
Generate the bar data and call callback immediately.
"""
self.bar.datetime = self.bar.datetime.replace(second=0, microsecond=0)
self.on_bar(self.bar)
self.bar = None
请问if self.bar: 和 return bar这两行代码是否需要?
问题2
demo_strategy里打开BarGenerator后看到的对update_tick函数定义的代码里有如下内容,但08课时视频里是没有的,请问这两行代码的意思是什么?究竟是否需要写这两行代码?
# Filter tick data with older timestamp
if self.last_tick and tick.datetime < self.last_tick.datetime:
return
问题3
demo_strategy里打开BarGenerator后看到的对update_tick函数定义的代码里有如下内容(重点是else部分)
if new_minute:
self.bar = BarData(
symbol=tick.symbol,
exchange=tick.exchange,
interval=Interval.MINUTE,
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
而08课时视频里对else下面的代码如下所示
else:
self.bar.high_price = max(self.bar.high_price, tick.last_price)
self.bar.low_price = min(self.bar.low_price, tick.last_price)
self.bar.close_price = tick.last_price
self.bar.open_interest = tick.open_interest
self.bar.datetime = tick.datetime
可以看出,08课时视频里是没有以下内容的,请问下面这些代码作用是什么?究竟是否需要写?
if tick.high_price > self.last_tick.high_price:
self.bar.high_price = max(self.bar.high_price, tick.high_price)
if tick.low_price < self.last_tick.low_price:
self.bar.low_price = min(self.bar.low_price, tick.low_price)
问题4
在def update_bar(self, bar: BarData) -> None:下面有如下代码
if self.interval == Interval.MINUTE:
# x_minute bar
if not (bar.datetime.minute + 1) % self.window:
finished = True
请问为何还要加上1?
问题5
在demo_strategy里打开BarGenerator后看到的def update_bar_hour_window(self, bar: BarData):下面有如下代码:
# If minute is 59, update minute bar into window bar and push
if bar.datetime.minute == 59:
self.hour_bar.high_price = max(
self.hour_bar.high_price,
bar.high_price
)
self.hour_bar.low_price = min(
self.hour_bar.low_price,
bar.low_price
)
self.hour_bar.close_price = bar.close_price
self.hour_bar.volume += int(bar.volume)
self.hour_bar.open_interest = bar.open_interest
finished_bar = self.hour_bar
self.hour_bar = None
请问,为何当bar.datetime.minute == 59时这根1小时K线就合成完毕了呢?为何不是等于60时才合成结束?