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

新建NewBarGenerator类,继承BarGenerator类,然后修改 update_bar_minute_window 函数。写cta策略时,使用NewBarGenerator可以定义一些奇奇怪怪的K线,例如7分钟、9分钟、11分钟等。
代码如下:
`
from typing import Callable
from vnpy.trader.constant import Interval
from vnpy_ctastrategy import BarData, BarGenerator

class NewBarGenerator(BarGenerator):
def init(
self,
on_bar: Callable,
window: int = 0,
on_window_bar: Callable = None,
interval: Interval = Interval.MINUTE
):
super().init(on_bar, window, on_window_bar, interval)

def update_bar_minute_window(self, bar: BarData) -> None:
    # If not inited, create window bar object
    if not self.window_bar:
        dt = bar.datetime.replace(second=0, 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
    self.interval_count += 1
    if not self.interval_count % self.window:
        self.interval_count = 0
        self.on_window_bar(self.window_bar)
        self.window_bar = None

`

Administrator
avatar
加入于:
帖子: 4502
声望: 322

感谢分享,精华送上

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

大佬,请教一个技术问题,我想把小时K线合成为上一小时的50分钟到这一小时的49分钟,该如何修改代码?

Member
avatar
加入于:
帖子: 4704
声望: 287

根据自己需求修改bg的update_bar_hour_window函数

Member
avatar
加入于:
帖子: 260
声望: 3

巴黎小矿工 wrote:

新建NewBarGenerator类,继承BarGenerator类,然后修改 update_bar_minute_window 函数。写cta策略时,使用NewBarGenerator可以定义一些奇奇怪怪的K线,例如7分钟、9分钟、11分钟等。
代码如下:
`
from typing import Callable
from vnpy.trader.constant import Interval
from vnpy_ctastrategy import BarData, BarGenerator

class NewBarGenerator(BarGenerator):
def init(
self,
on_bar: Callable,
window: int = 0,
on_window_bar: Callable = None,
interval: Interval = Interval.MINUTE
):
super().init(on_bar, window, on_window_bar, interval)

def update_bar_minute_window(self, bar: BarData) -> None:
    # If not inited, create window bar object
    if not self.window_bar:
        dt = bar.datetime.replace(second=0, 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
    self.interval_count += 1
    if not self.interval_count % self.window:
        self.interval_count = 0
        self.on_window_bar(self.window_bar)
        self.window_bar = None

`

description

区别如上图所示,看见有的人分享的K线自定义合成函数NewBarGenerator写的如下面代码所示

 if not (bar.datetime.minute + 1) % self.window:

在这一行代码与你写的有点点不同( if not self.interval_count % self.window:),请问这具体的区别是? 望指导一下,万分感激

Member
avatar
加入于:
帖子: 1471
声望: 105

左边是用累计收到的1分钟K线条数来判断切分。

右边是用1分钟线的时间戳来判断切分。

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

沪公网安备 31011502017034号

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