vn.py官网
开源量化社区
Member
avatar
加入于:
帖子: 154
声望: 0

老师好,我把vnpy自带策略AtrRsiStrategy中的百分比止损参照BollChannelStrategy策略
修改为ATR止损,修改了一下,但是回测总是交易非常多的笔数,异常,只是一个简单的修改还出错,十分困惑,望老师帮忙指点纠正一下,万分感激

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


class AtrRsiStrategy_百分比修改为atr(CtaTemplate):
    """"""

    author = "用Python的交易员"

    atr_length = 50
    atr_window = 40
    atr_ma_length = 60
    rsi_length = 70
    rsi_entry = 30
    sl_multiplier = 1.5
    fixed_size = 1

    atr_value = 0
    atr_value1 = 0
    atr_ma = 0
    rsi_value = 0
    rsi_buy = 0
    rsi_sell = 0
    intra_trade_high = 0
    intra_trade_low = 0
    long_stop = 0
    short_stop = 0

    parameters = [
        "atr_length",
        "atr_ma_length",
        "rsi_length",
        "rsi_entry",
        "sl_multiplier",
        "fixed_size",
        "atr_window"
    ]
    variables = [
        "atr_value",
        "atr_value1",
        "atr_ma",
        "rsi_value",
        "rsi_buy",
        "rsi_sell",
        "intra_trade_high",
        "intra_trade_low",
        "long_stop",
        "short_stop"
    ]

    def __init__(self, cta_engine, strategy_name, vt_symbol, setting):
        """"""
        super().__init__(cta_engine, strategy_name, vt_symbol, setting)
        self.bg = BarGenerator(self.on_bar)
        self.am = ArrayManager()

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

        self.rsi_buy = 50 + self.rsi_entry
        self.rsi_sell = 50 - self.rsi_entry

        self.load_bar(10)

    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.
        """
        self.cancel_all()

        am = self.am
        am.update_bar(bar)
        if not am.inited:
            return

        atr_array = am.atr(self.atr_length, array=True)
        self.atr_value = atr_array[-1]
        self.atr_ma = atr_array[-self.atr_ma_length:].mean()
        self.rsi_value = am.rsi(self.rsi_length)


        self.atr_value1 = am.atr(self.atr_window)


        if self.pos == 0:
            self.intra_trade_high = bar.high_price
            self.intra_trade_low = bar.low_price

            if self.atr_value > self.atr_ma:
                if self.rsi_value > self.rsi_buy:
                    self.buy(bar.close_price + 5, self.fixed_size)
                elif self.rsi_value < self.rsi_sell:
                    self.short(bar.close_price - 5, self.fixed_size)

        elif self.pos > 0:
            self.intra_trade_high = max(self.intra_trade_high, bar.high_price)
            self.intra_trade_low = bar.low_price

            self.long_stop = self.intra_trade_high - self.atr_value1 * self.sl_multiplier
            self.sell(self.long_stop, abs(self.pos), True)

        elif self.pos < 0:
            self.intra_trade_high = bar.high_price
            self.intra_trade_low = min(self.intra_trade_low, bar.low_price)

            self.short_stop = self.intra_trade_low + self.atr_value1 * self.sl_multiplier
            self.cover(self.short_stop, abs(self.pos), True)

        self.put_event()

    def on_order(self, order: OrderData):
        """
        Callback of new order data update.
        """
        pass

    def on_trade(self, trade: TradeData):
        """
        Callback of new trade data update.
        """
        self.put_event()

    def on_stop_order(self, stop_order: StopOrder):
        """
        Callback of stop order update.
        """
        pass
Administrator
avatar
加入于:
帖子: 4756
声望: 278

代码上看不出什么问题,但是要理解一个逻辑:

  1. 这个策略的开仓条件,是一种持续性状态
  2. 止损,是一种回撤临时状态
  3. 所以有可能一止损后,下一根K线依旧满足入场条件,导致立即再次开仓
Member
avatar
加入于:
帖子: 154
声望: 0

用Python的交易员 wrote:

代码上看不出什么问题,但是要理解一个逻辑:

  1. 这个策略的开仓条件,是一种持续性状态
  2. 止损,是一种回撤临时状态
  3. 所以有可能一止损后,下一根K线依旧满足入场条件,导致立即再次开仓

1:老师您好,咨询一下,为什么同一个策略逻辑(此策略仅修改了VNPY自带策略中的ATR止损),vnpy自带的AtrRsiStrategy中的百分比止损就不会发生这类情况呢?
2::针对这类情况,比较好的解决方案是? 是在开仓信号里添加一个开关吗? 望指导,万分感激

Member
avatar
加入于:
帖子: 2378
声望: 148

 

1.原因应该是你用的sl_multiplier太小了,以至于价格太接近close_price,所以总是触到停止单定的价格,继而发出委托。

自己不确定的时候其实可以print看看。
 
description
 
图上没有改你的参数,只是加上了trailing_percent(拿的原策略里的0.8)算出的long_stop进行对比,可以看到,你的atr1算出的long_stop一直比trailing_percent算出的更贴近close_price,所以交易笔数多是正常的。
 
此外,策略跑起来是没有问题的,但是有现成的atr建议不用再加个不一样的window来计算atr。这样增加了复杂度,但不一定能对策略起到实际的提升效果。而且如果之后走到优化,因为本来atrrsi就有很多参数了,再增加新的参数不仅会消耗更多时间也会带来更大的过拟合的风险。如果要改atr止损就像下面一样简单改动就可以了。
 

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


class AtrRsiStrategy_百分比修改为atr(CtaTemplate):
    """"""

    author = "用Python的交易员"

    atr_length = 22
    atr_ma_length = 10
    rsi_length = 5
    rsi_entry = 16
    atr_multiplier = 3
    fixed_size = 1

    atr_value = 0
    atr_ma = 0
    rsi_value = 0
    rsi_buy = 0
    rsi_sell = 0
    intra_trade_high = 0
    intra_trade_low = 0

    parameters = [
        "atr_length",
        "atr_ma_length",
        "rsi_length",
        "rsi_entry",
        "atr_multiplier",
        "fixed_size"
    ]
    variables = [
        "atr_value",
        "atr_ma",
        "rsi_value",
        "rsi_buy",
        "rsi_sell",
        "intra_trade_high",
        "intra_trade_low"
    ]

    def __init__(self, cta_engine, strategy_name, vt_symbol, setting):
        """"""
        super().__init__(cta_engine, strategy_name, vt_symbol, setting)
        self.bg = BarGenerator(self.on_bar)
        self.am = ArrayManager()

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

        self.rsi_buy = 50 + self.rsi_entry
        self.rsi_sell = 50 - self.rsi_entry

        self.load_bar(10)

    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.
        """
        self.cancel_all()

        am = self.am
        am.update_bar(bar)
        if not am.inited:
            return

        atr_array = am.atr(self.atr_length, array=True)
        self.atr_value = atr_array[-1]
        self.atr_ma = atr_array[-self.atr_ma_length:].mean()
        self.rsi_value = am.rsi(self.rsi_length)

        if self.pos == 0:
            self.intra_trade_high = bar.high_price
            self.intra_trade_low = bar.low_price

            if self.atr_value > self.atr_ma:
                if self.rsi_value > self.rsi_buy:
                    self.buy(bar.close_price + 5, self.fixed_size)
                elif self.rsi_value < self.rsi_sell:
                    self.short(bar.close_price - 5, self.fixed_size)

        elif self.pos > 0:
            self.intra_trade_high = max(self.intra_trade_high, bar.high_price)
            self.intra_trade_low = bar.low_price

            long_stop = self.intra_trade_high - self.atr_value * self.atr_multiplier
            self.sell(long_stop, abs(self.pos), stop=True)

        elif self.pos < 0:
            self.intra_trade_low = min(self.intra_trade_low, bar.low_price)
            self.intra_trade_high = bar.high_price

            short_stop = self.intra_trade_low + self.atr_value * self.atr_multiplier
            self.cover(short_stop, abs(self.pos), stop=True)

        self.put_event()

    def on_order(self, order: OrderData):
        """
        Callback of new order data update.
        """
        pass

    def on_trade(self, trade: TradeData):
        """
        Callback of new trade data update.
        """
        self.put_event()

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

 

2.可以相应调整参数,比如说atr_window和对应的乘数(e.g. 增大)。要是觉得交易次数过多,也可以考虑一下不同的品种进行尝试。
 

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

沪公网安备 31011502017034号