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

想更改一下海龟交易法则,突破信号触发直接建仓4个单位,止损以加仓信号逐步移动+唐安琪退出window,即在turtle_signal_strategy的代码上,注解掉加仓语句,但是保留根据加仓信号而移动的2N止损价,代码如下,请老师指教,谢谢

    def on_trade(self, trade: TradeData):
        """
        Callback of new trade data update.
        """
        if trade.direction == Direction.LONG:
            self.long_entry = trade.price
            self.long_stop = self.long_entry - 2 * self.atr_value

        else:
            self.short_entry = trade.price
            self.short_stop = self.short_entry + 2 * self.atr_value

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

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

    def send_buy_orders(self, price):
        """"""
        t = self.pos / self.fixed_size

        if t < 1:
            self.buy(price, self.fixed_size*4, True)

        if t < 2:
        #   self.buy(price + self.atr_value * 0.5, self.fixed_size, True)
            self.price = price + self.atr_value * 0.5

        if t < 3:
        #   self.buy(price + self.atr_value, self.fixed_size, True)
            self.price = price + self.atr_value

        if t < 4:
        #   self.buy(price + self.atr_value * 1.5, self.fixed_size, True)
            self.price = price + self.atr_value * 1.5

    def send_short_orders(self, price):
        """"""
        t = self.pos / self.fixed_size

        if t > -1:
            self.short(price, self.fixed_size*4, True)

        if t > -2:
        #   self.short(price - self.atr_value * 0.5, self.fixed_size, True)
            self.price = price - self.atr_value * 0.5

        if t > -3:
        #   self.short(price - self.atr_value, self.fixed_size, True)
            self.price = price - self.atr_value

        if t > -4:
        #   self.short(price - self.atr_value * 1.5, self.fixed_size, True)
            self.price = price - self.atr_value * 1.5
Member
加入于:
帖子: 13
声望: 0

不知道在判断t语句里,没有触发buy/short,但希望返回self.price,这样是不是可以达到on_trade函数可以返回最新的trade.price,计算相应的stop,不知道可不可以这样写

Member
avatar
加入于:
帖子: 3164
声望: 185

自己打印排查一下应该清楚了。
price应该能传进来,但是后面t<3,t<4的逻辑应该走不到,因为t取决于self.pos,没有下单自然策略持仓就不会变了。

Member
加入于:
帖子: 13
声望: 0

xiaohe wrote:

自己打印排查一下应该清楚了。
price应该能传进来,但是后面t<3,t<4的逻辑应该走不到,因为t取决于self.pos,没有下单自然策略持仓就不会变了。

谢谢,那self.pos 也给定义,是不是可以

    def on_trade(self, trade: TradeData):
        """
        Callback of new trade data update.
        """
        if trade.direction == Direction.LONG:
            self.long_entry = trade.price
            self.long_stop = self.long_entry - 2 * self.atr_value

        else:
            self.short_entry = trade.price
            self.short_stop = self.short_entry + 2 * self.atr_value

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

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

    def send_buy_orders(self, price):
        """"""
        t = self.pos / self.fixed_size

        if t < 1:
            self.buy(price, self.fixed_size*4, True)

        if t < 2:
        #   self.buy(price + self.atr_value * 0.5, self.fixed_size, True)
            self.price = price + self.atr_value * 0.5
            self.pos = 2

        if t < 3:
        #   self.buy(price + self.atr_value, self.fixed_size, True)
            self.price = price + self.atr_value
            self.pos = 3

        if t < 4:
        #   self.buy(price + self.atr_value * 1.5, self.fixed_size, True)
            self.price = price + self.atr_value * 1.5
            self.pos = 4

    def send_short_orders(self, price):
        """"""
        t = self.pos / self.fixed_size

        if t > -1:
            self.short(price, self.fixed_size*4, True)

        if t > -2:
        #   self.short(price - self.atr_value * 0.5, self.fixed_size, True)
            self.price = price - self.atr_value * 0.5
            self.pos = -2 

        if t > -3:
        #   self.short(price - self.atr_value, self.fixed_size, True)
            self.price = price - self.atr_value
            self.pos = -3

        if t > -4:
        #   self.short(price - self.atr_value * 1.5, self.fixed_size, True)
            self.price = price - self.atr_value * 1.5
            self.pos = -4
Member
avatar
加入于:
帖子: 3164
声望: 185

self.pos是策略持仓,请不要随意修改self.pos

Member
加入于:
帖子: 1
声望: 0

turtle_signal_strategy 这个策略逻辑有问题,进、出场逻辑都错了,代码应该没测试。管理员可以再确认下,看下他on_bar函数就明白了。

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

沪公网安备 31011502017034号