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

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
Member
avatar
加入于:
帖子: 716
声望: 62

self.bar是不是需要改个变量名,可能和update_tick里冲突了

Member
avatar
加入于:
帖子: 13
声望: 0

应该不是,不执行update_tick,只执行update_sec_tick观察的结果是没触发update_bar这个函数
self.on_bar(self.bar),,这行代码执行了没有任何响应,on_bar中的打印日志也没有

def update_bar(self, bar: BarData) -> None:
    """
    Update 1 minute bar into generator
    """
    if self.interval == Interval.SECOND:
        self.update_bar_second_window(bar)
    elif self.interval == Interval.MINUTE:
        self.update_bar_minute_window(bar)
    else:
        self.update_bar_hour_window(bar)
Member
avatar
加入于:
帖子: 13
声望: 0

晕倒,不知什么时候碰到键盘,策略中的self.bg15sec = BarGenerator(self.on_bar...变成self.bg15sec = BarGenerator(self.onnnnnnn

可以跑起来了,还有个加载历史数据部分需要改

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

沪公网安备 31011502017034号

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