VeighNa量化社区
你的开源社区量化交易平台
kevinglz's Avatar
Member
离线
22 帖子
声望: 1

假设我的策略是15分钟频率的,因为vnpy是15分钟走完之后才去挂单,假设14:59时触发了一个买入条件,挂了限价单,这个限价单会保留到夜盘的21:14吗?

假设上轨是bar.close_price + self.atr_multiplier*self.atr_value,当价格向上突破上轨时则卖出。这个逻辑该怎么写呢?

因为每天的结束时点会不一样,会分成两类情况:
1。如果有夜盘,则从23:59切分
2.。如果没有夜盘(如长假前一天),则从14:59切分(国债期货为15:14)
那么如何制定规则,让程序自动判定呢?

请问如何将相同品种的多个策略信号进行合并呢?开仓信号套用MultiSignalStrategy的模板比较容易处理,但因为每个策略有单独的止损条件,止损取决于开仓时点的价格,所以使用MultiSignalStrategy似乎行不通。

看了下商品期货的平今手续费比平昨更低,那么直接平仓不是更好的选择吗,在什么情况下会需要锁仓呢?

现需要将VNPY的策略代码移植到其他平台,但新的平台并不支持停止单(Stop=True)。请问以boll_channel_strategy为例,如何将止损单改写成限价单呢,且用于实盘呢?

            self.long_stop = self.intra_trade_high - self.atr_value * self.sl_multiplier
            self.sell(self.long_stop, abs(self.pos), True)
    def update_bar(self, bar: BarData) -> None:
        """
        Update 1 minute bar into generator
        """
        # If not inited, creaate window bar object
        if not self.window_bar:
            # Generate timestamp for bar data
            if self.interval == Interval.MINUTE:
                dt = bar.datetime.replace(second=0, microsecond=0)
            else:
                dt = bar.datetime.replace(minute=0, 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 into window bar
        self.window_bar.close_price = bar.close_price
        self.window_bar.volume += int(bar.volume)
        self.window_bar.open_interest = bar.open_interest

        # Check if window bar completed
        finished = False

        if self.interval == Interval.MINUTE:
            # x-minute bar
            if not (bar.datetime.minute + 1) % self.window:
                finished = True

        elif self.interval == Interval.HOUR:
            if self.last_bar:
                new_hour = bar.datetime.hour != self.last_bar.datetime.hour
                last_minute = bar.datetime.minute == 59

                if new_hour or last_minute:
                    # 1-hour bar
                    if self.window == 1:
                        finished = True
                    # x-hour bar
                    else:
                        self.interval_count += 1

                        if not self.interval_count % self.window:
                            finished = True
                            self.interval_count = 0

        if finished:
            self.on_window_bar(self.window_bar)
            self.window_bar = None

        # Cache last bar object
        self.last_bar = bar
        print('bar:',bar)
        print('window_bar:',self.window_bar)
        print('')

尝试合成5分钟k线,我在每次update_bar时将bar和self.window_bar打印出来,结果如下:

description

米筐的分钟数据是从9:01开始打时间戳的,代表9:00到9:01这一分钟的数据。测试发现k线合成走到9:04分的时候就结束了(finished=True,self.window_bar=None),只用了9:00~9:04这四根1分钟k线就合成了第一根5分钟k线,而9:04-9:05这一分钟最新数据完全没有用到。依次类推,第二根5分钟k线用的是9:05~9:09的数据,而9:09~9:10这一分钟也被忽略了。回测模式下,合成k线时总是会漏掉最新的一分钟k线,这个影响还是挺大的吧。

米筐的1分钟k线从9:01开始,代表9:00:00到9:01:00(左闭右开)这段时间的数据描述。
我的理解是,从1分钟线合成15分钟线时,应该走完9:01~9:15这15根k线才进行合成。
我在米筐的研究环境中做了实验,如果取每天早上的第一条15分钟K线,其成交量为前15分钟K线之和(如下图所示),即用到了9:01~9:15这15条数据。
description

但是vnpy在合成xmin bar的逻辑有如下代码:

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

由于这个“+1”的存在,导致程序走完9:14这一根k线的时候就合成完毕了,实际上只用了前14根k线。不知道这算不算bug呢?

我理解实盘的时候是将tick数据转换成分钟数据,因此vnpy是以一分钟的开始作为标记,即用9:30标记9:30~9:31(左闭右开)的数据;然而米筐数据是采用分钟的结束作为标记,即用9:31来标记9:30~9:31(左闭右开)的数据。如果是做回测,是不是会导致分钟k线错位?举个例子:
15分钟K线应该对应以9:16~9:30标记的分钟数据,而vnpy在回测时用到的数据却是9:15~9:29,因为代码中的“+1“,恰好错位了1分钟。

请问如何在回测中查看K线图表(买卖时点)的同时显示策略变量的走势图呢,如ma,atr等等。具体需要修改那些文件的源代码呢?谢谢

比如下面的例子:

if self.pos>0:
    if bar.close_price <= self.boll_mid:
        self.sell(bar.close_price, abs(self.pos))
    self.long_s1 = self.long_entry - self.fixed_s1
    self.sell(long_s1, abs(self.pos), True)

 如果两个条件同时满足,按照条件去下单会不会重复交易?

如果是个人投资者,可以将策略的开发回测,行情的接入,委托撤单全部放在vnpy上做。
但机构投资者通常有自己的交易柜台和风控系统,自己用vnpy开发的策略如何与公司内部的系统交互呢?比如从交易系统中读取数据,计算信号,再将委托发给交易系统,交易系统将成交回报返回给vnpy这样,有可以借鉴的例子吗?

© 2015-2022 微信 18391752892
备案服务号:沪ICP备18006526号

沪公网安备 31011502017034号

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