示例#1
0
    def manage_open_order(self, order, position, bars, to_update, to_cancel,
                          open_positions):
        if position is not None and self.factor > 0:
            # trail
            newStop = order.stop_price
            refRange = 0
            if self.atrPeriod > 0:
                atrId = "ATR" + str(self.atrPeriod)
                refRange = Indicator.get_data_static(bars[1], atrId)
                if refRange is None:
                    refRange = clean_range(bars,
                                           offset=1,
                                           length=self.atrPeriod)
                    Indicator.write_data_static(bars[1], refRange, atrId)

            elif position.wanted_entry is not None and position.initial_stop is not None:
                refRange = (position.wanted_entry - position.initial_stop)

            if refRange != 0:
                ep = bars[0].high if position.amount > 0 else bars[0].low
                be = position.wanted_entry + refRange * self.buffer
                if (ep - (position.wanted_entry + refRange * self.factor)) * position.amount > 0 \
                        and (be - newStop) * position.amount > 0:
                    newStop = math.floor(
                        be) if position.amount < 0 else math.ceil(be)

            if newStop != order.stop_price:
                order.stop_price = newStop
                to_update.append(order)
示例#2
0
    def process_bar(self, bars: List[Bar]):
        atr = clean_range(bars, offset=0, length=self.max_look_back * 2)

        offset = 1
        move_length = 1
        if (bars[offset].high - bars[offset].low) < (bars[offset + 1].high -
                                                     bars[offset + 1].low):
            move_length = 2

        threshold = atr * self.threshold_factor

        maxDist = atr * self.max_dist_factor
        buffer = atr * self.buffer_factor

        [sinceLongReset,
         longTrail] = self.calc_trail(bars, offset, 1, move_length, threshold,
                                      maxDist)
        [sinceShortReset,
         shortTrail] = self.calc_trail(bars, offset, -1, move_length,
                                       threshold, maxDist)

        sinceReset = min(sinceLongReset, sinceShortReset)

        if sinceReset >= 3:
            last_data: Data = self.get_data(bars[1])
            lastLongSwing = self.calc_swing(bars, 1, last_data.longSwing,
                                            sinceReset, buffer)
            lastShortSwing = self.calc_swing(bars, -1, last_data.shortSwing,
                                             sinceReset, buffer)
            if last_data.longSwing is not None and last_data.longSwing < bars[
                    0].high:
                lastLongSwing = None
            if last_data.shortSwing is not None and last_data.shortSwing > bars[
                    0].low:
                lastShortSwing = None
        else:
            lastLongSwing = None
            lastShortSwing = None

        self.write_data(
            bars[0],
            Data(sinceLongReset=sinceLongReset,
                 sinceShortReset=sinceShortReset,
                 longTrail=longTrail,
                 shortTrail=shortTrail,
                 longSwing=lastLongSwing,
                 shortSwing=lastShortSwing,
                 buffer=buffer,
                 atr=atr))
示例#3
0
    def manage_open_order(self, order, position, bars, to_update, to_cancel,
                          open_positions):
        if position is not None and self.maxATRDiff > 0 and self.atrPeriod > 0:
            # trail
            newStop = order.stop_price
            atrId = "ATR" + str(self.atrPeriod)
            refRange = Indicator.get_data_static(bars[1], atrId)
            if refRange is None:
                refRange = clean_range(bars, offset=1, length=self.atrPeriod)
                Indicator.write_data_static(bars[1], refRange, atrId)

            if refRange != 0:
                ep = bars[0].high if position.amount > 0 else bars[0].low
                maxdistStop = ep - math.copysign(refRange * self.maxATRDiff,
                                                 position.amount)
                if (maxdistStop - newStop) * position.amount > 0:
                    newStop = math.floor(
                        maxdistStop) if position.amount < 0 else math.ceil(
                            maxdistStop)

            if newStop != order.stop_price:
                order.stop_price = newStop
                to_update.append(order)