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

大家好,我想问下有没有合成tick数据的方法,比如说每30秒合成一个新的数据,类似于BarGenerator一样合成一个30分钟的bar数据

Member
avatar
加入于:
帖子: 716
声望: 55

可以参照BarGenerator写一个TickGenerator

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

郭易燔 wrote:

可以参照BarGenerator写一个TickGenerator
谢谢大佬的思路,我写了个简单的TickGenerator,因为我的需求是每X秒响应一次,不用去合成数据,代码如下:
```
class TickGenerator:
def init(
self,
on_bar: Callable,
window: int = 0,
on_window_bar: Callable = None,
interval: Interval = Interval.SECOND
):
self.bar: BarData = None
self.on_bar: Callable = on_bar

    self.interval: Interval = interval
    self.interval_count: int = 0

    self.window: int = window
    self.window_bar: BarData = None
    self.on_window_bar: Callable = on_window_bar

    self.last_tick: TickData = None

def update_tick(self, tick: TickData) -> None:
    """
    Update new tick data into generator.
    """
    new_minute = False

    if not tick.last_price:
        return

    if self.last_tick and tick.datetime < self.last_tick.datetime:
        return

    if not self.bar:
        new_minute = True
    elif self.bar.datetime.minute != tick.datetime.minute or self.bar.datetime.hour != tick.datetime.hour or self.bar.datetime + timedelta(seconds=self.window) <= tick.datetime :
        self.bar.datetime = tick.datetime
        self.on_bar(self.bar)
        new_minute = True

    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

    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

```

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

沪公网安备 31011502017034号

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