VeighNa量化社区
你的开源社区量化交易平台
Member
avatar
加入于:
帖子: 9
声望: 0

问题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时才合成结束?

Administrator
avatar
加入于:
帖子: 4500
声望: 320
  1. 不能,有时需要通过调用generate函数的方式直接获取合成的K线
  2. 过滤某些时候出现的TICK推送乱序的情况(即老的时间戳,在新的时间戳后推送),通常出现在一些网络环境不稳定的交易通道
  3. 这里同样是针对国内期货市场,每秒最多2次TICK推送,则两次TICK间的信息可能丢失导致的问题
  4. 因为vn.py的K线时间戳是开始时间戳,而不是结束点的
  5. 原理同上了
Member
avatar
加入于:
帖子: 9
声望: 0

用Python的交易员 wrote:

  1. 不能,有时需要通过调用generate函数的方式直接获取合成的K线
  2. 过滤某些时候出现的TICK推送乱序的情况(即老的时间戳,在新的时间戳后推送),通常出现在一些网络环境不稳定的交易通道
  3. 这里同样是针对国内期货市场,每秒最多2次TICK推送,则两次TICK间的信息可能丢失导致的问题
  4. 因为vn.py的K线时间戳是开始时间戳,而不是结束点的
  5. 原理同上了

第三个问题还是不懂,请您详细说说吧

Member
avatar
加入于:
帖子: 4618
声望: 284

两个if判断也是为了过滤tick推送乱序导致last_price不正确的问题
版本一直在更新,update_tick函数都还是以最新版为准

© 2015-2022 上海韦纳软件科技有限公司
备案服务号:沪ICP备18006526号

沪公网安备 31011502017034号

【用户协议】
【隐私政策】
【免责条款】