VeighNa量化社区
你的开源社区量化交易平台
Member
avatar
加入于:
帖子: 5
声望: 1
def on_bar(self, bar: BarData):
    """
    Callback of new bar data update.
    """

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

    close = self.am.close_array

    fast_macd,slow_macd,macd = talib.MACD(close,self.fast_window,
                                       self.slow_window,
                                       self.siganl_window)

    self.fast_macd0 = fast_macd[-1]
    self.fast_macd1 = fast_macd[-2]

    self.slow_macd0 = slow_macd[-1]
    self.slow_macd1 = slow_macd[-2]

    cross_over = self.fast_macd0 > self.slow_macd0 and self.fast_macd1 < self.slow_macd1
    cross_below = self.fast_macd0 < self.slow_macd0 and self.fast_macd1 > self.slow_macd1

    if self.pos == 0:

        if cross_over:
            self.buy(bar.close_price, self.fixed_size, True)
            self.long_stop = bar.close_price - self.stoploss

        elif cross_below:
            self.short(bar.close_price, self.fixed_size, True)
            self.short_stop = bar.close_price + self.stoploss
            self.short_protfit = bar.close_price - self.startstop

    elif self.pos > 0:
        if bar.low_price < self.long_stop:
            self.sell(self.long_stop, abs(self.pos), True)
        elif cross_below:
            self.sell(bar.close_price, abs(self.pos), True)

    elif self.pos < 0:
        if bar.high_price > self.short_stop:
            self.cover(self.short_stop, abs(self.pos), True)

        elif bar.low_price < self.short_protfit:
            self.cover(self.short_protfit, abs(self.pos), True)

        elif cross_over:
            self.cover(bar.close_price, abs(self.pos), True)

    self.put_event()

description

description
数据端口Wind,MCAD金叉做多死叉做空,设置止损止盈,总是迸发成交,如何处理

Member
avatar
加入于:
帖子: 4680
声望: 285

可以自己打印一下变量值,看看策略是否按照你的逻辑执行的

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

沪公网安备 31011502017034号

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