vn.py官网
Veighna量化社区 | 你的开源社区量化交易平台
Member
avatar
加入于:
帖子: 4
声望: 0

在bar level回测正常。 tick level 回测的时候 engine.set_parameters已经设置了 mode=BacktestingMode.TICK, 策略里面也改成了self.load_tick(),下了订单但是马上就被取消
策略如下(简化成了macd >0 马上做多,macd<0马上做空)

from vnpy.app.cta_strategy import (
    CtaTemplate,
    BarGenerator,
    ArrayManager,
    TickData,
    BarData,
    OrderData,
    TradeData,
    StopOrder
)

from datetime import datetime

class Vegas_5m_tick9(CtaTemplate):
    """"""
    author = ""

    rsi_window = 13
    k12 = 12
    k144 = 144
    k169 = 169
    k288 = 288
    k338 = 338
    k576 = 576
    macd_fast_period = 24
    macd_slow_period = 36
    macd_signal_period = 9
    rsi_allow_long = 50
    rsi_allow_short = 50
    short_stop_per = 1.5
    short_take_per = 3.0
    long_stop_per = 1.5
    long_take_per = 3.0
    fixed_size = 1

    ema12_dist = 0.008
    bar_range_left = 0.009
    bar_range_right = 0.01


    parameters = [
        "rsi_window", "rsi_allow_short", "rsi_allow_long",
        'k12', 'k144', 'k169', 'k288', 'k338', 'k576',
        "macd_fast_period", "macd_slow_period", "macd_signal_period",
        'short_stop_per', 'short_take_per', 'long_stop_per', 'long_take_per',
        "fixed_size", 'bar_range_left', 'bar_range_right', 'ema12_dist'
    ]

    variables = [

    ]
    bars_5m = []

    def __init__(
        self,
        cta_engine,
        strategy_name: str,
        vt_symbol: str,
        setting: dict,

    ):
        """"""
        super().__init__(cta_engine, strategy_name, vt_symbol, setting)

        self.bg = BarGenerator(self.on_bar)
        self.am = ArrayManager() 

        self.bg5m = BarGenerator(self.on_bar, 5, self.on_5m_bar)
        self.am5m = ArrayManager(600) # 保存的bar数量

    def on_init(self):
        """
        Callback when strategy is inited.
        """
        self.write_log("策略初始化")

        #回测时加载n个交易日数据用于初始化
        # self.load_bar(3) # 数字是天数
        self.load_tick(3) # 数字是天数

    def on_start(self):
        """
        Callback when strategy is started.
        """
        self.write_log("策略启动")

    def on_stop(self):
        """
        Callback when strategy is stopped.
        """
        self.write_log("策略停止")

    def on_tick(self, tick: TickData):
        """
        Callback of new tick data update.
        """
        self.bg.update_tick(tick)

    def on_bar(self, bar: BarData):
        """
        Callback of new bar data update.
        """
        if self.pos > 0 and bar.close_price > self.long_take :
            self.sell(self.long_take - 0.1, abs(self.pos))

        if self.pos < 0 and bar.close_price < self.short_take :
            self.cover(self.short_take + 0.1, abs(self.pos))

        self.bg5m.update_bar(bar)

    def on_5m_bar(self, bar: BarData):
        #cancel all order
        self.cancel_all()

        self.am5m.update_bar(bar)

        if not self.am5m.inited:
            return

        self.rsi_value = self.am5m.rsi(self.rsi_window)

        self.macd_macd, self.macd_signal, self.macd_hist = self.am5m.macd(
            self.macd_fast_period, self.macd_slow_period, self.macd_signal_period)


        # No position
        if self.pos == 0:


            if self.macd_macd > 0 :
                print('macd', self.macd_macd ,  file=open( 'D:\Downloads\print.txt', 'a'))

                self.buy(bar.close_price + 0.2, self.fixed_size)

                # 止盈止损
                self.long_stop = bar.close_price *  (1 - self.long_stop_per / 100)
                self.long_take = bar.close_price *  (1 + self.long_take_per / 100)


            if self.macd_macd < 0 :
                # return
                self.short(bar.close_price - 0.2, self.fixed_size)
                # 止盈止损
                self.short_stop = bar.close_price *  (1 + self.short_stop_per / 100)
                self.short_take = bar.close_price *  (1 - self.short_take_per / 100)

        self.put_event()


    def on_trade(self, trade: TradeData):
        """
        Callback of new trade data update.
        """
        if self.pos > 0:
            self.sell(self.long_stop, abs(self.pos), stop=True)
            # self.sell(self.long_take, abs(self.pos), stop=True)
        elif self.pos  < 0:
            self.cover(self.short_stop, abs(self.pos), stop=True)
            # self.cover(self.short_take, abs(self.pos), stop=True)

        self.put_event()

    def on_order(self, order: OrderData):
        """
        Callback of new order data update.
        """
        print('order', order.status, file=open( 'D:\Downloads\print.txt', 'a'))
        pass

    def on_stop_order(self, stop_order: StopOrder):
        """
        Callback of stop order update.
        """
        pass

打印出来的结果类似于

macd 0.13436859257532774
order Status.NOTTRADED
order Status.CANCELLED
macd 0.1224525081596255
order Status.NOTTRADED
order Status.CANCELLED
macd 0.11129331033552603
order Status.NOTTRADED

所有订单不成交,马上就被取消。

2021-12-24 00:48:44.131052  开始加载历史数据
2021-12-24 00:48:44.203047  加载进度:# [0%]
2021-12-24 00:48:44.209051  加载进度:# [7%]
2021-12-24 00:48:44.216102  加载进度:## [14%]
2021-12-24 00:48:44.222151  加载进度:### [21%]
2021-12-24 00:48:44.232069  加载进度:### [28%]
2021-12-24 00:48:44.239969  加载进度:#### [34%]
2021-12-24 00:48:44.248656  加载进度:##### [41%]
2021-12-24 00:48:44.251652  加载进度:##### [48%]
2021-12-24 00:48:44.260670  加载进度:###### [55%]
2021-12-24 00:48:44.264656  加载进度:####### [62%]
2021-12-24 00:48:44.274669  加载进度:####### [69%]
2021-12-24 00:48:44.289635  加载进度:######## [76%]
2021-12-24 00:48:44.292663  加载进度:######### [83%]
2021-12-24 00:48:44.306645  加载进度:######### [90%]
2021-12-24 00:48:44.309669  加载进度:########## [97%]
2021-12-24 00:48:44.312653  历史数据加载完成,数据量:5721043
2021-12-24 00:48:45.169673  策略初始化完成
2021-12-24 00:48:45.169673  开始回放历史数据
2021-12-24 00:48:48.682612  回放进度:= [0%]
2021-12-24 00:48:51.775750  回放进度:== [10%]
2021-12-24 00:48:55.186807  回放进度:=== [20%]
2021-12-24 00:48:58.752515  回放进度:==== [30%]
2021-12-24 00:49:02.110492  回放进度:===== [40%]
2021-12-24 00:49:05.654370  回放进度:====== [50%]
2021-12-24 00:49:09.225055  回放进度:======= [60%]
2021-12-24 00:49:12.393397  回放进度:======== [70%]
2021-12-24 00:49:15.723135  回放进度:========= [80%]
2021-12-24 00:49:19.111351  回放进度:========== [90%]
2021-12-24 00:49:19.117359  回放进度:=========== [100%]
2021-12-24 00:49:19.117359  历史数据回放结束
2021-12-24 00:49:19.171435  开始计算逐日盯市盈亏
2021-12-24 00:49:19.171435  成交记录为空,无法计算
2021-12-24 00:49:19.172404  开始计算策略统计指标
2021-12-24 00:49:19.172404  ------------------------------
2021-12-24 00:49:19.172404  首个交易日:  
2021-12-24 00:49:19.172404  最后交易日:  
2021-12-24 00:49:19.172404  总交易日:   0
Member
avatar
加入于:
帖子: 2846
声望: 197

可以根据策略逻辑和历史数据在vnpy_ctastrategy.backtesting中进行打印排查了

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

下载的tick数据只有成交价格和量,没有买盘卖盘的挂单数据,所以成交不了。
https://zhuanlan.zhihu.com/p/100532844、

                bid_price_1 = float(item[1]) -0.03,
                bid_volume_1 = float(5),
                ask_price_1 = float(item[1]) + 0.03,
                ask_volume_1 = float(5),

自己生成了模拟挂单数据,可以了。

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

沪公网安备 31011502017034号

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