Exemple #1
0
    def process1(self, timestamp, last_timestamp, candles, prices, volumes):
        signal = None

        # volume sma, increase signal strength when volume increase over its SMA
        # volume_sma = utils.MM_n(self.depth-1, self.volume.volumes)

        #
        # signals analysis
        #

        rsi_30_70 = 0  # -1 30, 1 70
        rsi_40_60 = 0  # 1 if RSI in 40-60
        volume_signal = 0
        ema_sma_cross = 0
        ema_sma_height = 0

        if self.rsi:
            self.rsi.compute(last_timestamp, prices)

            if self.rsi.last < self.rsi_low:
                rsi_30_70 = 1.0
            elif self.rsi.last > self.rsi_high:
                rsi_30_70 = -1.0

            if self.rsi.last > 0.4 and self.rsi.last < 0.6:
                rsi_40_60 = 1

        # if self.volume.last > volume_sma[-1]:
        #     volume_signal = 1
        # elif self.volume.last < volume_sma[-1]:
        #     volume_signal = -1

        if self.sma and self.ema:
            self.sma.compute(last_timestamp, prices)
            self.ema.compute(last_timestamp, prices)

            # ema over sma crossing
            ema_sma_cross = utils.cross((self.ema.prev, self.sma.prev), (self.ema.last, self.sma.last))

            if self.ema.last > self.sma.last:
                ema_sma_height = 1
            elif self.ema.last < self.sma.last:
                ema_sma_height = -1

        if ema_sma_cross > 0 and rsi_30_70 > 0:
            signal = StrategySignal(self.tf, timestamp)
            signal.signal = StrategySignal.SIGNAL_ENTRY
            signal.dir = 1
            signal.p = candles[-1].close

        elif ema_sma_cross < 0 and rsi_30_70 < 0:
            signal = StrategySignal(self.tf, timestamp)
            signal.signal = StrategySignal.SIGNAL_EXIT
            signal.dir = 1
            signal.p = candles[-1].close

        if self.pivotpoint:
            self.pivotpoint.compute(last_timestamp, self.price.open, self.price.high, self.price.low, self.price.close)

        return signal
Exemple #2
0
    def process_lin(self, timestamp, last_timestamp, candles, prices, volumes):
        signal = None

        self.score.initialize()

        if self.rsi:
            self.rsi.compute(last_timestamp, prices)

        if self.sma:
            self.sma.compute(last_timestamp, prices)

        if self.ema:
            self.ema.compute(last_timestamp, prices)

        if self.vwma:
            self.vwma.compute(last_timestamp, prices, volumes)

        if self.atr:
            self.atr.compute(last_timestamp, self.price.high, self.price.low,
                             self.price.close)

        if self.ema.last < self.sma.last:
            if self.rsi.last > 0.5:
                self.score.add(-1, 1)
            elif self.rsi.last < 0.2:
                self.score.add(1, 1)
        else:
            if self.rsi.last > 0.8:
                self.score.add(-1, 1)
            elif self.rsi.last < 0.6:
                self.score.add(1, 1)

        self.score.finalize()

        #
        # signal from score
        #

        if self.score.data[-1] >= self.score_level:
            signal = StrategySignal(self.tf, timestamp)
            signal.signal = StrategySignal.SIGNAL_ENTRY
            signal.dir = 1
            signal.p = candles[-1].close

        if self.score.data[-1] <= -self.score_level:
            signal = StrategySignal(self.tf, timestamp)
            signal.signal = StrategySignal.SIGNAL_EXIT
            signal.dir = 1
            signal.p = candles[-1].close

        if candles:
            # last processed candle timestamp (from last candle is non consolidated else from the next one)
            self.next_timestamp = candles[-1].timestamp if not candles[
                -1].ended else candles[-1].timestamp + self.tf

        return signal
Exemple #3
0
    def compute(self, timestamp, last_timestamp, candles, prices, volumes):
        signal = None

        # volume sma, increase signal strength when volume increase over its SMA
        # volume_sma = utils.MM_n(self.depth-1, self.volume.volumes)

        if self.rsi:
            self.rsi.compute(timestamp, prices)

        if self.pivotpoint:
            if self.pivotpoint.compute_at_close and self.last_closed:
                self.pivotpoint.compute(timestamp, self.price.open,
                                        self.price.high, self.price.low,
                                        self.price.close)

        if self.atr:
            if self.last_closed:
                self.atr.compute(timestamp, self.price.high, self.price.low,
                                 self.price.close)

        if self.tomdemark:
            if self.tomdemark.compute_at_close and self.last_closed:
                # last_timestamp
                self.tomdemark.compute(timestamp, self.price.timestamp,
                                       self.price.high, self.price.low,
                                       self.price.prices)

                # sell-setup
                if self.tomdemark.c.c == 9 and self.tomdemark.c.d < 0:
                    signal = StrategySignal(self.tf, timestamp)
                    signal.signal = StrategySignal.SIGNAL_EXIT
                    signal.dir = 1
                    signal.p = self.price.close[-1]

                # buy-setup
                elif self.tomdemark.c.c == 9 and self.tomdemark.c.d > 0:
                    signal = StrategySignal(self.tf, timestamp)
                    signal.signal = StrategySignal.SIGNAL_EXIT
                    signal.dir = -1
                    signal.p = self.price.close[-1]

        if self.bsawe:
            if self.last_closed:
                # use OHLC4 as price in place of close
                bsawe = self.bsawe.compute(timestamp, self.price.high,
                                           self.price.low, self.price.prices)

                if bsawe > 0:
                    signal = StrategySignal(self.tf, timestamp)
                    signal.signal = StrategySignal.SIGNAL_ENTRY
                    signal.dir = 1
                    signal.p = self.price.close[-1]

                    if self.tomdemark.c.tdst:
                        signal.sl = self.tomdemark.c.tdst

                    if len(self.pivotpoint.resistances[2]):
                        signal.tp = np.max(self.pivotpoint.resistances[2])

                elif bsawe < 0:
                    signal = StrategySignal(self.tf, timestamp)
                    signal.signal = StrategySignal.SIGNAL_ENTRY
                    signal.dir = -1
                    signal.p = self.price.close[-1]

                    if self.tomdemark.c.tdst:
                        signal.sl = self.tomdemark.c.tdst

                    if len(self.pivotpoint.supports[2]):
                        signal.tp = np.min(self.pivotpoint.supports[2])

        return signal
Exemple #4
0
    def compute(self, timestamp, last_timestamp, candles, prices, volumes):
        signal = None

        # volume sma, increase signal strength when volume increase over its SMA
        # volume_sma = utils.MM_n(self.depth-1, self.volume.volumes)

        if self.rsi:
            self.rsi.compute(last_timestamp, prices)

        # if self.volume_ema:
        #     self.volume_ema.compute(last_timestamp, volumes)

        #     if self.volume.last > self.volume_ema.last:
        #         volume_signal = 1
        #     elif self.volume.last < self.volume_ema.last:
        #         volume_signal = -1

        if self.pivotpoint:
            self.pivotpoint.compute(last_timestamp, self.price.open,
                                    self.price.high, self.price.low,
                                    self.price.close)

        # if self.bollingerbands:
        #     self.bollingerbands.compute(last_timestamp, prices)

        #     bb_break = 0
        #     bb_ma = 0

        #     if prices[-1] > self.bollingerbands.last_top:
        #         bb_break = 1
        #     elif prices[-1] < self.bollingerbands.last_bottom:
        #         bb_break = -1

        #     if prices[-1] > self.bollingerbands.last_ma:
        #         bb_ma = -1
        #     elif prices[-1] > self.bollingerbands.last_ma:
        #         bb_ma = 1

        if self.atr:
            self.atr.compute(last_timestamp, self.price.high, self.price.low,
                             self.price.close)

        if self.bbawe:
            # use OHLC4 as price in place of close
            bbawe = self.bbawe.compute(last_timestamp, self.price.high,
                                       self.price.low, self.price.prices)

        if self.tomdemark:
            self.tomdemark.compute(last_timestamp, self.price.timestamp,
                                   self.price.high, self.price.low,
                                   self.price.prices)

        level1_signal = 0

        if bbawe > 0:
            signal = StrategySignal(self.tf, timestamp)
            signal.signal = StrategySignal.SIGNAL_ENTRY
            signal.dir = 1
            signal.p = self.price.close[-1]

            if self.tomdemark.c.tdst:
                signal.sl = self.tomdemark.c.tdst

            if len(self.pivotpoint.resistances[2]):
                signal.tp = np.max(self.pivotpoint.resistances[2])

        elif bbawe < 0:
            signal = StrategySignal(self.tf, timestamp)
            signal.signal = StrategySignal.SIGNAL_ENTRY
            signal.dir = -1
            signal.p = self.price.close[-1]

            if self.tomdemark.c.tdst:
                signal.sl = self.tomdemark.c.tdst

            if len(self.pivotpoint.supports[2]):
                signal.tp = np.min(self.pivotpoint.supports[2])

        #
        # setup completed
        #

        # sell-setup
        if self.tomdemark.c.c == 9 and self.tomdemark.c.d < 0:
            signal = StrategySignal(self.tf, timestamp)
            signal.signal = StrategySignal.SIGNAL_EXIT
            signal.dir = 1
            signal.p = self.price.close[-1]

        # buy-setup
        elif self.tomdemark.c.c == 9 and self.tomdemark.c.d > 0:
            signal = StrategySignal(self.tf, timestamp)
            signal.signal = StrategySignal.SIGNAL_EXIT
            signal.dir = -1
            signal.p = self.price.close[-1]

        return signal
Exemple #5
0
    def process1(self, timestamp, to_ts, candles, prices, volumes):
        signal = None

        # volume sma, increase signal strength when volume increase over its SMA
        volume_sma = utils.MM_n(self.depth-1, self.volume.volumes)

        rsi = 0
        rsi_30_70 = 0  # 1 <30, -1 >70
        rsi_40_60 = 0  # 1 if RSI in 40-60

        stochrsi = 0
        stochrsi_20_80 = 0  # 1 <20, -1 >80
        stochrsi_40_60 = 0  # 1 if stochRSI in 40-60
        
        volume_signal = 0
        
        ema_sma_cross = 0
        ema_sma_height = 0

        if self.rsi:
            self.rsi.compute(to_ts, prices)[-1]

            if self.rsi.last < self.rsi_low:
                rsi_30_70 = 1.0
            elif self.rsi.last > self.rsi_high:
                rsi_30_70 = -1.0

            if self.rsi.last > 0.4 and self.rsi.last < 0.6:
                rsi_40_60 = 1

        if self.stochrsi:
            stochrsi = self.stochrsi.compute(to_ts, prices)[0][-1]

            if stochrsi < 0.2:
                stochrsi_20_80 = 1.0
            elif stochrsi > 0.8:
                stochrsi_20_80 = -1.0

            if stochrsi > 0.4 and stochrsi < 0.6:
                stochrsi_40_60 = 1

        signal = StrategySignal(self.tf, timestamp)

        if self.volume.last > volume_sma[-1]:
            volume_signal = 1
        elif self.volume.last < volume_sma[-1]:
            volume_signal = -1

        if self.sma and self.ema:
            sma = self.sma.compute(to_ts, prices)[-2:]
            ema = self.ema.compute(to_ts, prices)[-2:]

            # ema over sma crossing
            ema_sma_cross = utils.cross((ema[-2], sma[-2]), (ema[-1], sma[-1]))

            if ema[-1] > sma[-1]:
                ema_sma_height = 1
            elif ema[-1] < sma[-1]:
                ema_sma_height = -1

        if self.tomdemark:
            td = self.tomdemark.compute(to_ts, self.price.timestamp, self.price.high, self.price.low, self.price.close)[-1]

            #
            # setup entry
            #

            # long entry on sell-setup + rsi oversell
            if (td.c == 2 or td.c == 3) and td.d < 0 and candles[-1].close > candles[-2].close:
                # if rsi < 0.6: # (ema_sma_height > 0 and rsi_40_60 > 0):  # or rsi_30_70 < 0:  # 1
                if stochrsi_20_80 > 0 or rsi_30_70 > 0:
                    signal.signal = StrategySignal.SIGNAL_ENTRY
                    signal.dir = 1
                    signal.p = candles[-1].close
                    signal.sl = td.tdst or candles[-1].low - candles[-1].height

                    # 1.618 fibo
                    # signal.tp = (self.price.max - self.price.min) * 1.618 + self.price.close
                    #logger.info("> entry long %s c2-c3, c:%s p:%s sl:%s tp:%s" % (self.tf, td.c, signal.p, signal.sl, signal.tp))

            # short entry on buy-setup + rsi overbuy
            elif (td.c > 1 and td.c < 4) and td.d > 0 and candles[-1].close < candles[-2].close:
                if stochrsi_20_80 < 0 or rsi_30_70 < 0:
                    signal.signal = StrategySignal.SIGNAL_ENTRY
                    signal.dir = -1
                    signal.p = candles[-1].close
                    signal.sl = td.tdst or candles[-1].high + candles[-1].height

                    # 1.618 fibo
                    # signal.tp = self.price.close - (self.price.max - self.price.min) * 1.618
                    # logger.info("> entry short c2-c3, p:%s sl:%s tp:%s" % (signal.p, signal.sl, signal.tp))

            #
            # invalidation (3-7)
            #

            # elif td.c >= 3 and td.c <= 7:
            #     # if td.d > 0:  # and candles[-1].close < candles[-2].low:            
            #     if td.d < 0:  # and candles[-1].close < candles[-2].low:
            #         # short cancelation (excess of volume and ema under sma)
            #         pass

            #     # elif td.d < 0 and candles[-1].close < candles[-2].close:
            #     elif td.d > 0: # and candles[-1].close < candles[-2].close:
            #         # long cancelation (excess of volume and ema under sma)
            #         # if ema_sma_height < 0 or rsi_40_60 > 0:
            #         # if stochrsi_20_80 < 0:  # and volume_signal > 0:
            #         # if ema_sma_height < 0 and volume_signal > 0:
            #             # logger.info("> rsi_30_70 %s / rsi_40_60 %s / ema_sma_height %s / volume_signal %s" % (rsi_30_70, rsi_40_60, ema_sma_height, volume_signal))
            #             signal.signal = StrategySignal.SIGNAL_EXIT  # CANCEL
            #             signal.dir = 1
            #             signal.p = candles[-1].close
            #             logger.info("> canceled long entry c2-c3, p:%s" % (signal.p,))

            #
            # setup completed
            #

            elif (td.c == 8 and td.p) or td.c == 9:
                if td.d < 0:  # and candles[-1].close > candles[-2].close:
                    #if stochrsi_20_80 > 1:
                        signal.signal = StrategySignal.SIGNAL_EXIT
                        signal.dir = 1
                        signal.p = candles[-1].close
                        # logger.info("> Exit long %s c8p-c9 (%s%s)" % (self.tf, td.c, 'p' if signal.p else ''))

                elif td.d > 0:  # and candles[-1].close < candles[-2].close:
                    # if stochrsi_20_80 < 0:
                        signal.signal = StrategySignal.SIGNAL_EXIT
                        signal.dir = -1
                        signal.p = candles[-1].close
                        # logger.info("> Exit short %s c8p-c9 (%s%s)" % (self.tf, td.c, 'p' if signal.p else ''))

            #
            # CD entry
            #

            if td.cd >= 1 and td.cd <= 3:
                # count-down sell-setup + rsi oversell
                if td.cdd < 0:  # and candles[-1].close > candles[-2].high:
                # if rsi_30_70 > 0:
                    signal.signal = StrategySignal.SIGNAL_ENTRY
                    signal.dir = 1
                    signal.p = candles[-1].close
                    signal.sl = td.tdst

                    self.score.add(1.0, 1.0, 'td9-cd')
                    logger.info("> Entry long %s cd13, sl:%s" % (self.tf, signal.sl,))

                # count-down buy-setup + rsi overbuy
                elif td.cdd > 0:  # and candles[-1].close < candles[-2].low:
                    signal.signal = StrategySignal.SIGNAL_ENTRY
                    signal.dir = -1
                    signal.p = candles[-1].close
                    logger.info("> cancel entry long %s (sell ct13), p:%s" % (self.tf, signal.p,))

                    # # if rsi_30_70 < 0:
                    #     # signal.signal = StrategySignal.SIGNAL_ENTRY
                    #     # signal.dir = -1
                    #     # signal.p = candles[-1].close
                    #     # signal.sl = td.tdst

                    #     # self.score.add(-1.0, 1.0, 'td9-cd')
                    #     logger.info("> entry short cd13")
                    #     pass

            #
            # CD13 setup
            #

            elif td.cd == 13:
                logger.info(td.cd)
                # count-down sell-setup completed
                if td.cdd < 0:
                    signal.signal = StrategySignal.SIGNAL_EXIT
                    signal.p = candles[-1].close
                    signal.dir = 1

                    logger.info("> Exit long cd13")

                # count-down buy-setup completed
                elif td.cdd > 0:
                    signal.signal = StrategySignal.SIGNAL_EXIT
                    signal.p = candles[-1].close
                    signal.dir = -1

                    logger.info("> Exit short cd13")
                    pass

        return signal
Exemple #6
0
    def process4(self, timestamp, last_timestamp, candles, prices, volumes):
        signal = None

        # volume sma, increase signal strength when volume increase over its SMA
        volume_sma = utils.MM_n(self.depth-1, self.volume.volumes)

        rsi_30_70 = 0  # 1 <30, -1 >70
        rsi_40_60 = 0  # 1 if RSI in 40-60
        rsi_trend = 0

        stochrsi_20_80 = 0  # 1 <20, -1 >80
        stochrsi_40_60 = 0  # 1 if stochRSI in 40-60
        
        volume_signal = 0
        
        ema_sma_cross = 0
        ema_sma_height = 0

        if self.tf == 4*60*60:
            self.sma200.compute(last_timestamp, prices)
            self.sma55.compute(last_timestamp, prices)

        if self.rsi:
            self.rsi.compute(last_timestamp, prices)

            if self.rsi.last < self.rsi_low:
                rsi_30_70 = 1.0
            elif self.rsi.last > self.rsi_high:
                rsi_30_70 = -1.0

            if self.rsi.last > 0.4 and self.rsi.last < 0.6:
                rsi_40_60 = 1

            rsi_trend = utils.trend_extremum(self.rsi.rsis)

        # if self.stochrsi:
        #     self.stochrsi.compute(last_timestamp, prices)

        #     if self.stochrsi.last_k < 0.2:
        #         stochrsi_20_80 = 1.0
        #     elif self.stochrsi.last_k > 0.8:
        #         stochrsi_20_80 = -1.0

        #     if self.stochrsi.last_k > 0.4 and self.stochrsi.last_k < 0.6:
        #         stochrsi_40_60 = 1

        # if self.volume_ema:
        #     self.volume_ema.compute(last_timestamp, volumes)

        #     if self.volume.last > self.volume_ema.last:
        #         volume_signal = 1
        #     elif self.volume.last < self.volume_ema.last:
        #         volume_signal = -1

        if self.sma and self.ema:
            self.sma.compute(last_timestamp, prices)
            self.ema.compute(last_timestamp, prices)

            # ema over sma crossing
            ema_sma_cross = utils.cross((self.ema.prev, self.sma.prev), (self.ema.last, self.sma.last))

            if self.ema.last > self.sma.last:
                ema_sma_height = 1
            elif self.ema.last < self.sma.last:
                ema_sma_height = -1

        if self.bollingerbands:
            self.bollingerbands.compute(last_timestamp, prices)

            bb_break = 0
            bb_ma = 0

            if prices[-1] > self.bollingerbands.last_top:
                bb_break = 1
            elif prices[-1] < self.bollingerbands.last_bottom:
                bb_break = -1

        #     if prices[-1] > self.bollingerbands.last_ma:
        #         bb_ma = -1
        #     elif prices[-1] > self.bollingerbands.last_ma:
        #         bb_ma = 1

        if self.bbawe:
            bbawe = self.bbawe.compute(last_timestamp, self.price.high, self.price.low, self.price.close)

        if self.atr:
            self.atr.compute(last_timestamp, self.price.high, self.price.low, self.price.close)

        level1_signal = 0

        if self.ema.last < self.sma.last:
            # bear trend
            if self.rsi.last > 0.5:  # initial: 0.5
                level1_signal = -1
            elif self.rsi.last < 0.2:  # initial: 0.2
                level1_signal = 1
        else:
            # bull trend
            if self.rsi.last > 0.8:  # initial: 0.8
                level1_signal = -1
            elif self.rsi.last < 0.6:  # initial: 0.6
                level1_signal = 1

        if bbawe > 0:# and level1_signal > 0:
            signal = StrategySignal(self.tf, timestamp)
            signal.signal = StrategySignal.SIGNAL_ENTRY
            signal.dir = 1
            signal.p = self.price.close[-1]

            # if self.tomdemark.c.tdst:
            #     signal.sl = self.tomdemark.c.tdst

        elif bbawe < 0:# and level1_signal < 0:
            signal = StrategySignal(self.tf, timestamp)
            signal.signal = StrategySignal.SIGNAL_ENTRY
            signal.dir = -1
            signal.p = self.price.close[-1]

            # if self.tomdemark.c.tdst:
            #     signal.sl = self.tomdemark.c.tdst

        if self.tomdemark:
            self.tomdemark.compute(last_timestamp, self.price.timestamp, self.price.high, self.price.low, self.price.close)

            #
            # setup completed
            #

            # sell-setup
            # if self.tomdemark.c.c == 9 and self.tomdemark.c.d < 0:
            #     signal = StrategySignal(self.tf, timestamp)
            #     signal.signal = StrategySignal.SIGNAL_EXIT
            #     signal.dir = 1
            #     signal.p = self.price.close[-1]

            # # buy-setup
            # elif self.tomdemark.c.c == 9 and self.tomdemark.c.d > 0:
            #     signal = StrategySignal(self.tf, timestamp)
            #     signal.signal = StrategySignal.SIGNAL_EXIT
            #     signal.dir = -1
            #     signal.p = self.price.close[-1]

            # if signal and signal.signal == StrategySignal.SIGNAL_ENTRY:
            #     # if level1_signal > 0 and len(self.pivotpoint.supports[1]):
            #     #     # cancel if not below a support (long direction)
            #     #     if self.price.last >= np.nanmax(self.pivotpoint.supports[1]):
            #     #         level1_signal = 0

            #     # if level1_signal < 0 and len(self.pivotpoint.resistances[1]):
            #     #     # cancel if not above a resistance (short direction)
            #     #     if self.price.last <= np.nanmin(self.pivotpoint.resistances[1]):
            #     #         level1_signal = 0

            #     if level1_signal > 0 and len(self.pivotpoint.supports[1]):
            #         # cancel if not below a support (long direction)
            #         if self.price.last >= np.nanmax(self.pivotpoint.supports[1]):
            #             level1_signal = 0
            #             signal = None

            #     if level1_signal < 0 and len(self.pivotpoint.resistances[1]):
            #         # cancel if not below a resistance (short direction)
            #         if self.price.last <= np.nanmin(self.pivotpoint.resistances[1]):
            #             level1_signal = 0
            #             signal = None

        return signal
Exemple #7
0
    def process1(self, timestamp, last_timestamp, candles, prices, volumes):
        signal = None

        # volume sma, increase signal strength when volume increase over its SMA
        # volume_sma = utils.MM_n(self.depth-1, self.volume.volumes)

        #
        # signals analysis
        #

        rsi_30_70 = 0  # -1 30, 1 70
        rsi_40_60 = 0  # 1 if RSI in 40-60
        volume_signal = 0
        ema_sma_cross = 0
        ema_sma_height = 0

        if self.rsi:
            self.rsi.compute(last_timestamp, prices)

            if self.rsi.last < self.rsi_low:
                rsi_30_70 = 1.0
            elif self.rsi.last > self.rsi_high:
                rsi_30_70 = -1.0

            if self.rsi.last > 0.4 and self.rsi.last < 0.6:
                rsi_40_60 = 1

        # if self.volume.last > volume_sma[-1]:
        #     volume_signal = 1
        # elif self.volume.last < volume_sma[-1]:
        #     volume_signal = -1

        if self.sma200:
            self.sma200.compute(last_timestamp, prices)

        if self.sma55:
            self.sma55.compute(last_timestamp, prices)

        if self.sma and self.ema:
            self.sma.compute(last_timestamp, prices)
            self.ema.compute(last_timestamp, prices)

            # ema over sma crossing
            ema_sma_cross = utils.cross((self.ema.prev, self.sma.prev),
                                        (self.ema.last, self.sma.last))

            if self.ema.last > self.sma.last:
                ema_sma_height = 1
            elif self.ema.last < self.sma.last:
                ema_sma_height = -1

        bb_way = 0

        if self.bollingerbands:
            self.bollingerbands.compute(last_timestamp, prices)

            if self.bollingerbands.last_ma < prices[
                    -1] < self.bollingerbands.last_top:
                bb_way = -1

        if self.mama:
            self.mama.compute(last_timestamp, self.price.close)

            cross = self.mama.cross()

            if cross > 0:
                self.mama_cross = (1, last_timestamp, self.price.close[-1])
            elif cross < 0:
                self.mama_cross = (-1, last_timestamp, self.price.close[-1])
            else:
                self.mama_cross = (0, last_timestamp, self.price.close[-1])

            # or we can try an entry on cross signal
            if cross > 0:
                signal = StrategySignal(self.tf, timestamp)
                signal.signal = StrategySignal.SIGNAL_ENTRY
                signal.dir = 1
                signal.p = candles[-1].close

        # if ema_sma_cross > 0 and rsi_30_70 > 0:
        #     self.trend = 1

        # elif ema_sma_cross < 0 and rsi_30_70 < 0:
        #     signal = StrategySignal(self.tf, timestamp)
        #     signal.signal = StrategySignal.SIGNAL_EXIT
        #     signal.dir = 1
        #     signal.p = candles[-1].close
        #     self.trend = -1
        # else:
        #     self.trend = 0

        ema_sma = 0

        if self.ema.last < self.sma.last:
            # bear trend
            if self.rsi.last > 0.5:  # initial: 0.5
                ema_sma = -1
            elif self.rsi.last < 0.2:  # initial: 0.2
                ema_sma = 1
        else:
            # bull trend
            if self.rsi.last > 0.8:  # initial: 0.8
                ema_sma = -1
            elif self.rsi.last < 0.6:  # initial: 0.6
                ema_sma = 1

        if ema_sma < 0 and self.mama and self.mama.trend() < 0:
            signal = StrategySignal(self.tf, timestamp)
            signal.signal = StrategySignal.SIGNAL_EXIT
            signal.dir = 1
            signal.p = candles[-1].close

        if self.mama:
            self.trend = self.mama.trend()
        else:
            self.trend = ema_sma

        # can long on upward trend, on dip or on lower of bollinger if range
        # @todo detect dip, bollinger range (flat mama + first bounces)

        self.can_long = self.trend >= 0  # or ema_sma > 0

        if self.pivotpoint:
            self.pivotpoint.compute(last_timestamp, self.price.open,
                                    self.price.high, self.price.low,
                                    self.price.close)

        if self.atr:
            self.atr.compute(last_timestamp, self.price.high, self.price.low,
                             self.price.close)

        if self.tomdemark:
            self.tomdemark.compute(last_timestamp, self.price.timestamp,
                                   self.price.high, self.price.low,
                                   self.price.close)

            if self.tomdemark.c.c == 9 and self.tomdemark.c.d < 0:
                # setup complete and trend change
                signal = StrategySignal(self.tf, timestamp)
                signal.signal = StrategySignal.SIGNAL_EXIT
                signal.dir = 1
                signal.p = self.price.close[-1]

            elif 3 <= self.tomdemark.c.c <= 5 and self.tomdemark.c.d > 0:  # and (ema_sma < 0):
                # cancelation
                signal = StrategySignal(self.tf, timestamp)
                signal.signal = StrategySignal.SIGNAL_EXIT
                signal.dir = 1
                signal.p = self.price.close[-1]

        return signal
Exemple #8
0
    def process_cb(self, timestamp, last_timestamp, candles, prices, volumes):
        """
        Entry : EMA crossing + TD9 entry confirmation
        Exit : TD9 or opposite signal + EMA trend + RSI level
        """
        signal = None

        # volume sma, increase signal strength when volume increase over its SMA
        volume_sma = utils.MM_n(self.depth-1, self.volume.volumes)

        rsi_30_70 = 0  # 1 <30, -1 >70
        rsi_40_60 = 0  # 1 if RSI in 40-60
        rsi_trend = 0

        stochrsi_20_80 = 0  # 1 <20, -1 >80
        stochrsi_40_60 = 0  # 1 if stochRSI in 40-60
        
        volume_signal = 0
        
        ema_sma_cross = 0
        ema_sma_height = 0

        if self.tf == Instrument.TF_4HOUR:
            self.sma200.compute(timestamp, prices)
            self.sma55.compute(timestamp, prices)

        if self.rsi:
            self.rsi.compute(timestamp, prices)

            if self.rsi.last < self.rsi_low:
                rsi_30_70 = 1.0
            elif self.rsi.last > self.rsi_high:
                rsi_30_70 = -1.0

            if self.rsi.last > 0.4 and self.rsi.last < 0.6:
                rsi_40_60 = 1

            rsi_trend = utils.trend_extremum(self.rsi.rsis)

        # if self.stochrsi:
        #     self.stochrsi.compute(timestamp, prices)

        #     if self.stochrsi.last_k < 0.2:
        #         stochrsi_20_80 = 1.0
        #     elif self.stochrsi.last_k > 0.8:
        #         stochrsi_20_80 = -1.0

        #     if self.stochrsi.last_k > 0.4 and self.stochrsi.last_k < 0.6:
        #         stochrsi_40_60 = 1

        # if self.volume_ema:
        #     self.volume_ema.compute(timestamp, volumes)

        #     if self.volume.last > self.volume_ema.last:
        #         volume_signal = 1
        #     elif self.volume.last < self.volume_ema.last:
        #         volume_signal = -1

        if self.sma and self.ema:
            self.sma.compute(timestamp, prices)
            self.ema.compute(timestamp, prices)

            # ema over sma crossing
            ema_sma_cross = utils.cross((self.ema.prev, self.sma.prev), (self.ema.last, self.sma.last))

            if self.ema.last > self.sma.last:
                ema_sma_height = 1
            elif self.ema.last < self.sma.last:
                ema_sma_height = -1

        # if self.bollingerbands:
        #     self.bollingerbands.compute(timestamp, prices)

        #     bb_break = 0
        #     bb_ma = 0

        #     if prices[-1] > self.bollingerbands.last_top:
        #         bb_break = 1
        #     elif prices[-1] < self.bollingerbands.last_bottom:
        #         bb_break = -1

        # #     if prices[-1] > self.bollingerbands.last_ma:
        # #         bb_ma = -1
        # #     elif prices[-1] > self.bollingerbands.last_ma:
        # #         bb_ma = 1

        if self.atr:
            if self.last_closed:
                self.atr.compute(timestamp, self.price.high, self.price.low, self.price.close)

        if self.pivotpoint:
            if self.pivotpoint.compute_at_close and self.last_closed:
                self.pivotpoint.compute(timestamp, self.price.open, self.price.high, self.price.low, self.price.close)

        if self.bsawe:
            if self.last_closed:
                # use OHLC4 as price in place of close
                bsawe = self.bsawe.compute(timestamp, self.price.high, self.price.low, self.price.prices)

                #
                # trend signal
                #

                ema_rsi_signal = 0

                if self.ema.last < self.sma.last:
                    # bear trend
                    if self.rsi.last > 0.5:  # initial: 0.5
                        ema_rsi_signal = -1
                    elif self.rsi.last < 0.2:  # initial: 0.2
                        ema_rsi_signal = 1
                else:
                    # bull trend
                    if self.rsi.last > 0.8:  # initial: 0.8
                        ema_rsi_signal = -1
                    elif self.rsi.last < 0.6:  # initial: 0.6
                        ema_rsi_signal = 1
                #
                # entry computation
                #

                if bsawe > 0 and ema_rsi_signal != -1:
                    # long entry
                    signal = StrategySignal(self.tf, timestamp)
                    signal.signal = StrategySignal.SIGNAL_ENTRY
                    signal.dir = 1
                    signal.p = self.price.close[-1]

                    if self.tomdemark.c.tdst and signal.sl < signal.p:
                        signal.sl = self.tomdemark.c.tdst

                    if len(self.pivotpoint.resistances[2]):
                        tp = np.max(self.pivotpoint.resistances[2])
                        if tp > self.atr.stop_loss(-1):
                            # try with twice (same for short)
                            signal.tp = (tp - signal.p) * 2.0 + signal.p
                            # signal.tp = tp

                elif bsawe < 0 and ema_rsi_signal != 1:
                    # short entry
                    signal = StrategySignal(self.tf, timestamp)
                    signal.signal = StrategySignal.SIGNAL_ENTRY
                    signal.dir = -1
                    signal.p = self.price.close[-1]

                    if self.tomdemark.c.tdst and signal.sl > signal.p:
                        signal.sl = self.tomdemark.c.tdst

                    if len(self.pivotpoint.supports[2]):
                        tp = np.min(self.pivotpoint.supports[2])
                        if tp < self.atr.stop_loss(1):
                            signal.tp = signal.p - (signal.p - tp) * 2.0
                            # signal.tp = tp

        #
        # exit computation
        #

        if self.tomdemark:
            if self.tomdemark.compute_at_close and self.last_closed:
                # last_timestamp
                self.tomdemark.compute(timestamp, self.price.timestamp, self.price.high, self.price.low, self.price.close)

                # only on 5min timeframe, or could manage at strategy only for parent timeframe

                # sell-setup
                if self.tomdemark.c.c >= 8 and self.tomdemark.c.d < 0 and ema_rsi_signal < 0 and self.tf == Instrument.TF_5MIN:
                    signal = StrategySignal(self.tf, timestamp)
                    signal.signal = StrategySignal.SIGNAL_EXIT
                    signal.dir = 1
                    signal.p = self.price.close[-1]

                # buy-setup
                elif self.tomdemark.c.c >= 8 and self.tomdemark.c.d > 0 and ema_rsi_signal > 0 and self.tf == Instrument.TF_5MIN:
                    signal = StrategySignal(self.tf, timestamp)
                    signal.signal = StrategySignal.SIGNAL_EXIT
                    signal.dir = -1
                    signal.p = self.price.close[-1]

                #
                # setup aborted (@todo how to in this long/short case)
                #

                elif ((self.tomdemark.c.c >= 4 and self.tomdemark.c.c <= 7) and self.tomdemark.c.d < 0) and ema_rsi_signal < 0 and self.tf == Instrument.TF_5MIN:
                    signal = StrategySignal(self.tf, timestamp)
                    signal.signal = StrategySignal.SIGNAL_EXIT
                    signal.dir = 1
                    signal.p = self.price.close[-1]

                elif ((self.tomdemark.c.c >= 4 and self.tomdemark.c.c <= 7) and self.tomdemark.c.d > 0) and ema_rsi_signal > 0 and self.tf == Instrument.TF_5MIN:
                    signal = StrategySignal(self.tf, timestamp)
                    signal.signal = StrategySignal.SIGNAL_EXIT
                    signal.dir = -1
                    signal.p = self.price.close[-1]

        return signal
Exemple #9
0
    def process_td9(self, timestamp, last_timestamp, candles, prices, volumes):
        signal = None

        # volume sma, increase signal strength when volume increase over its SMA
        # volume_sma = utils.MM_n(self.depth-1, self.volume.volumes)

        rsi = 0
        rsi_30_70 = 0  # 1 <30, -1 >70
        rsi_40_60 = 0  # 1 if RSI in 40-60
        # rsi_trend = 0

        stochrsi = 0
        stochrsi_20_80 = 0  # 1 <20, -1 >80
        stochrsi_40_60 = 0  # 1 if stochRSI in 40-60
        
        volume_signal = 0
        
        ema_sma_cross = 0
        ema_sma_height = 0

        if self.rsi:
            self.rsi.compute(timestamp, prices)

            if self.rsi.last < self.rsi_low:
                rsi_30_70 = 1.0
            elif rsi > self.rsi_high:
                rsi_30_70 = -1.0

            if self.rsi.last > 0.4 and self.rsi.last < 0.6:
                rsi_40_60 = 1

        if self.stochrsi:
            self.stochrsi.compute(timestamp, prices)
            stochrsi = self.stochrsi.last_k

            if stochrsi < 0.2:
                stochrsi_20_80 = 1.0
            elif stochrsi > 0.8:
                stochrsi_20_80 = -1.0

            if stochrsi > 0.4 and stochrsi < 0.6:
                stochrsi_40_60 = 1

        # if self.volume.last > volume_sma[-1]:
        #     volume_signal = 1
        # elif self.volume.last < volume_sma[-1]:
        #     volume_signal = -1

        if self.sma and self.ema:
            self.sma.compute(timestamp, prices)
            self.ema.compute(timestamp, prices)

            # ema over sma crossing
            ema_sma_cross = utils.cross((self.ema.prev, self.sma.prev), (self.ema.last, self.sma.last))

            if self.ema.last > self.sma.last:
                ema_sma_height = 1
            elif self.ema.last < self.sma.last:
                ema_sma_height = -1

        if self.bollingerbands:
            self.bollingerbands.compute(timestamp, prices)

            bb_break = 0
            bb_ma = 0

            if prices[-1] > self.bollingerbands.last_top:
                bb_break = 1
            elif prices[-1] < self.bollingerbands.last_bottom:
                bb_break = -1

            if prices[-1] > self.bollingerbands.last_ma:
                bb_ma = -1
            elif prices[-1] > self.bollingerbands.last_ma:
                bb_ma = 1

        if self.atr:
            if self.last_closed:
                self.atr.compute(timestamp, self.price.high, self.price.low, self.price.close)

        if self.tomdemark:
            if self.tomdemark.compute_at_close and self.last_closed:
                # last_timestamp
                self.tomdemark.compute(timestamp, self.price.timestamp, self.price.high, self.price.low, self.price.close)

                #
                # setup entry
                #

                # long entry on sell-setup + rsi oversell
                if (self.tomdemark.c.c == 2 and self.tomdemark.c.d < 0 and self.price.close[-1] > self.price.close[-2]) or (self.tomdemark.c.c == 3):  # avec td3            
                    # # if (td.c == 1 and td.d < 0 and candles[-1].close > candles[-2].close):
                    # if (td.c == 2 and td.d < 0 and candles[-1].close > candles[-2].close):
                    # if ((td.c == 2 or td.c == 3) and td.d < 0 and candles[-1].close > candles[-2].close):
                    # if ((td.c == 2 or td.c == 3) and td.d < 0) and candles[-1].close > candles[-2].close:
                    # if bb_break == 0:
                    # if (bb_break == 0 and bb_ma < 0):  # test with aggressive + bb
                    # if (stochrsi_20_80 > 0 or rsi_30_70 > 0):
                    # if (ema_sma_height > 0 or rsi_30_70 > 0) and bb_break >= 0:
                    # if (bb_break == 0 and bb_ma < 0) and (stochrsi_20_80 > 0 or rsi_30_70 > 0):  # used with average case but not with aggressive
                    # if (bb_break == 0 and bb_ma < 0) and (ema_sma_height > 0 or rsi_30_70 > 0):  #  and volume_signal > 0:
                    # if (stochrsi_20_80 > 0 or rsi_30_70 > 0):  # la classique mais prend des risk, a voir avec un bon SL peut-être
                    # if (rsi_trend > 0) and (ema_sma_height > 0 or rsi_30_70 > 0):  # protege de perte mais rate des gains
                    # if (ema_sma_height > 0 or rsi_30_70 > 0):  #  and volume_signal > 0:  # C4
                    # if ema_sma_height > 0:  # C5
                    # if ema_sma_height > 0 and bb_break >= 0 and rsi_trend > 0:  # C6
                    # if ema_sma_height > 0 and bb_break >= 0 and stochrsi_20_80 > 0:
                    # if 1:  # C1
                    # if rsi_trend > 0 and bb_break > 0:  # pas mal evite des entrees foireuses mais pas assez de profits
                    # if (stochrsi_20_80 > 0 or rsi_30_70 > 0):  # la classique mais prend des risk, il faut un bon stop-loss
                    if (ema_sma_height > 0 or rsi_30_70 > 0) and bb_break <= 0:
                        # if rsi_trend and (stochrsi_20_80 > 0 or rsi_30_70 > 0):
                        # if (stochrsi_20_80 > 0 and rsi_30_70 > 0):
                        signal = StrategySignal(self.tf, timestamp)
                        signal.signal = StrategySignal.SIGNAL_ENTRY
                        signal.dir = 1
                        signal.p = self.price.close[-1]

                        if self.tomdemark.c.tdst:
                            signal.sl = self.tomdemark.c.tdst

                        # Terminal.inst().info("Entry %s %s" % (self.tomdemark.c.tdst, signal.sl), view='default')

                # aggressive entry
                if ((self.tomdemark.c.c == 9 or (self.tomdemark.c.c == 8 and self.tomdemark.c.p)) and self.tomdemark.c.d > 0):
                    # if stochrsi_20_80 > 0 and rsi_30_70 > 0 and candles[-1].close > candles[-2].close:  # C1 -
                    # if 1:  # C4 +++
                    # if stochrsi_20_80 > 0 and candles[-1].close > candles[-2].close:  # C3  ++
                    # if rsi_30_70 > 0 and candles[-1].close > candles[-2].close:  # C5  ??
                    # if ema_sma_height > 0 and bb_break >= 0 and rsi_trend > 0:  # C6 ??
                    # if ema_sma_height > 0 and bb_break >= 0 and stochrsi_20_80 > 0:  # C7 ??
                    # if (ema_sma_height > 0 or rsi_30_70 > 0):  # C2 +
                    # if stochrsi_20_80 > 0 and candles[-1].close > candles[-2].close:  # C3  ++
                    if (stochrsi_20_80 > 0 or rsi_30_70 > 0):  # C8 ??
                        signal = StrategySignal(self.tf, timestamp)
                        signal.signal = StrategySignal.SIGNAL_ENTRY
                        signal.dir = 1
                        signal.p = self.price.close[-1]

                        # Terminal.inst().info("Aggressive entry %s %s" % (self.tomdemark.c.tdst, signal.sl), view='default')

                #
                # invalidation 2 of opposite setup
                #

                # @todo or if a >= 3 <= 7 close below the previous candle close
                # can make loss in bull run
                # second condition might be optimized
                elif (self.tomdemark.c.c == 2 and self.tomdemark.c.d > 0 and self.price.close[-1] < self.price.close[-2]) or (self.tomdemark.c.c == 3 and self.tomdemark.c.d > 0):
                    # elif td.c == 3 and td.d > 0:  # and candles[-1].close < candles[-2].close:
                    # long cancelation on buy-setup formation 
                    # if ema_sma_height < 0 or rsi_40_60 > 0:  # (excess of volume and ema under sma)
                    # if stochrsi_20_80 < 0:  # and volume_signal > 0:
                    # if bb_break < 0:
                    # if (rsi_trend < 0):  # C2
                    # if 1:  # C1 +
                    # if (bb_break == 0 and bb_ma < 0):  # C3 ++
                    if ema_sma_height < 0:  #  and volume_signal > 0:
                        # Terminal.inst().info("rsi_30_70 %s / rsi_40_60 %s / ema_sma_height %s / volume_signal %s" % (rsi_30_70, rsi_40_60, ema_sma_height, volume_signal), view='default')
                        signal = StrategySignal(self.tf, timestamp)
                        signal.signal = StrategySignal.SIGNAL_EXIT  # CANCEL
                        signal.dir = 1
                        signal.p = self.price.close[-1]
                        # Terminal.inst().info("Canceled long entry %s c2-c3, p:%s tf:%s" % (self.strategy_trader.instrument.symbol, signal.p, self.tf), view='default')

                #
                # setup completed
                #

                elif (((self.tomdemark.c.c >= 8 and self.tomdemark.c.p) or (self.tomdemark.c.c == 9)) and self.tomdemark.c.d < 0):  # and candles[-1].close > candles[-2].close:
                    if 1:  # stochrsi_20_80 > 1:
                        signal = StrategySignal(self.tf, timestamp)
                        signal.signal = StrategySignal.SIGNAL_EXIT
                        signal.dir = 1
                        signal.p = self.price.close[-1]
                        # Terminal.inst().info("Exit long %s %s c8p-c9 (%s%s)" % (self.strategy_trader.instrument.symbol, self.tf, self.tomdemark.c.c, 'p' if signal.p else ''), view='default')

                #
                # setup aborted
                #

                elif ((self.tomdemark.c.c >= 2 and self.tomdemark.c.c <= 7) and self.tomdemark.c.d < 0) and self.price.close[-1] < self.price.close[-2]:  # C1
                    # if stochrsi_20_80 < 0 or rsi_30_70 < 0:  # bearish stoch OR rsi  # C1
                    if stochrsi_20_80 < 0 and rsi_30_70 < 0:  # bearish stoch AND rsi (seems better)  # C2
                        signal = StrategySignal(self.tf, timestamp)
                        signal.signal = StrategySignal.SIGNAL_EXIT
                        signal.dir = 1
                        signal.p = self.price.close[-1]
                        #Terminal.inst().info("Abort long %s %s c3-c7 (%s%s)" % (self.strategy_trader.instrument.symbol, self.tf, td.c, 'p' if signal.p else ''), view='default')

                #
                # CD entry
                #

                if self.tomdemark.cd.c > 1:
                    Terminal.inst().info("CD%s" % self.tomdemark.cd.c)

                if self.tomdemark.cd.c == 1:
                    # count-down buy-setup + rsi oversell
                    Terminal.inst().info("CD1")
                    if self.tomdemark.c.d < 0:  # and candles[-1].close > candles[-2].high:
                        # if rsi_30_70 > 0:
                        signal = StrategySignal(self.tf, timestamp)
                        signal.signal = StrategySignal.SIGNAL_ENTRY
                        signal.dir = 1
                        signal.p = self.price.last
                        signal.sl = self.tomdemark.c.tdst
                        Terminal.inst().info("Entry long %s %s cd13, sl:%s" % (self.strategy_trader.instrument.symbol, self.tf, signal.sl,), view='default')

                #
                # CD13 setup
                #

                elif self.tomdemark.cd.c == 13:
                    # count-down sell-setup completed
                    if self.tomdemark.cd.d < 0:
                        signal = StrategySignal(self.tf, timestamp)
                        signal.signal = StrategySignal.SIGNAL_EXIT
                        signal.p = self.price.close[-1]
                        signal.dir = 1
                        Terminal.inst().info("Exit long cd13", view='default')

        return signal
Exemple #10
0
    def process_ch(self, timestamp, last_timestamp, candles, prices, volumes):
        """
        Entry : EMA trend + RSI level + TD9 entry confirmation
        Exit : TD9 or opposite signal
        """
        signal = None

        # volume sma, increase signal strength when volume increase over its SMA
        volume_sma = utils.MM_n(self.depth-1, self.volume.volumes)

        rsi_30_70 = 0  # 1 <30, -1 >70
        rsi_40_60 = 0  # 1 if RSI in 40-60
        rsi_trend = 0

        stochrsi_20_80 = 0  # 1 <20, -1 >80
        stochrsi_40_60 = 0  # 1 if stochRSI in 40-60
        
        volume_signal = 0
        
        ema_sma_cross = 0
        ema_sma_height = 0

        if self.tf == 4*60*60:
            self.sma200.compute(timestamp, prices)
            self.sma55.compute(timestamp, prices)

        if self.rsi:
            self.rsi.compute(timestamp, prices)

            if self.rsi.last < self.rsi_low:
                rsi_30_70 = 1.0
            elif self.rsi.last > self.rsi_high:
                rsi_30_70 = -1.0

            if self.rsi.last > 0.4 and self.rsi.last < 0.6:
                rsi_40_60 = 1

            rsi_trend = utils.trend_extremum(self.rsi.rsis)

        # if self.stochrsi:
        #     self.stochrsi.compute(timestamp, prices)

        #     if self.stochrsi.last_k < 0.2:
        #         stochrsi_20_80 = 1.0
        #     elif self.stochrsi.last_k > 0.8:
        #         stochrsi_20_80 = -1.0

        #     if self.stochrsi.last_k > 0.4 and self.stochrsi.last_k < 0.6:
        #         stochrsi_40_60 = 1

        # if self.volume_ema:
        #     self.volume_ema.compute(timestamp, volumes)

        #     if self.volume.last > self.volume_ema.last:
        #         volume_signal = 1
        #     elif self.volume.last < self.volume_ema.last:
        #         volume_signal = -1

        if self.sma and self.ema:
            self.sma.compute(timestamp, prices)
            self.ema.compute(timestamp, prices)

            # ema over sma crossing
            ema_sma_cross = utils.cross((self.ema.prev, self.sma.prev), (self.ema.last, self.sma.last))

            if self.ema.last > self.sma.last:
                ema_sma_height = 1
            elif self.ema.last < self.sma.last:
                ema_sma_height = -1

        if self.bollingerbands:
            self.bollingerbands.compute(timestamp, prices)

            bb_break = 0
            bb_ma = 0

            if prices[-1] > self.bollingerbands.last_top:
                bb_break = 1
            elif prices[-1] < self.bollingerbands.last_bottom:
                bb_break = -1

        #     if prices[-1] > self.bollingerbands.last_ma:
        #         bb_ma = -1
        #     elif prices[-1] > self.bollingerbands.last_ma:
        #         bb_ma = 1

        if self.atr:
            if self.last_closed:
                self.atr.compute(timestamp, self.price.high, self.price.low, self.price.close)

        if self.pivotpoint:
            if self.pivotpoint.compute_at_close and self.last_closed:
                self.pivotpoint.compute(timestamp, self.price.open, self.price.high, self.price.low, self.price.close)

        level1_signal = 0

        if self.ema.last < self.sma.last:
            # bear trend
            if self.rsi.last > 0.5:  # initial: 0.5
                level1_signal = -1
            elif self.rsi.last < 0.2:  # initial: 0.2
                level1_signal = 1
        else:
            # bull trend
            if self.rsi.last > 0.8:  # initial: 0.8
                level1_signal = -1
            elif self.rsi.last < 0.6:  # initial: 0.6
                level1_signal = 1

        if self.tomdemark:
            if self.tomdemark.compute_at_close and self.last_closed:
                # last_timestamp
                self.tomdemark.compute(timestamp, self.price.timestamp, self.price.high, self.price.low, self.price.close)

                # long entry on sell-setup
                if self.tomdemark.c.c >= 1 and self.tomdemark.c.c <= 6 and self.tomdemark.c.d < 0 and level1_signal > 0:
                    signal = StrategySignal(self.tf, timestamp)
                    signal.signal = StrategySignal.SIGNAL_ENTRY
                    signal.dir = 1
                    signal.p = self.price.close[-1]

                    if self.tomdemark.c.tdst:
                        signal.sl = self.tomdemark.c.tdst

                    if len(self.pivotpoint.resistances[1]):
                        signal.tp = np.max(self.pivotpoint.resistances[1])

                    # Terminal.inst().info("Entry long %s %s" % (self.tomdemark.c.tdst, signal.sl), view='default')

                # short entry on buy-setup
                elif self.tomdemark.c.c >= 1 and self.tomdemark.c.c <= 6 and self.tomdemark.c.d > 0 and level1_signal < 0:
                    signal = StrategySignal(self.tf, timestamp)
                    signal.signal = StrategySignal.SIGNAL_ENTRY
                    signal.dir = -1
                    signal.p = self.price.close[-1]

                    if self.tomdemark.c.tdst:
                        signal.sl = self.tomdemark.c.tdst

                    if len(self.pivotpoint.supports[1]):
                        signal.tp = np.min(self.pivotpoint.supports[1])

                    # Terminal.inst().info("Entry short %s %s" % (self.tomdemark.c.tdst, signal.sl), view='default')

                # aggressive long entry
                elif self.tomdemark.c.c >= 8 and self.tomdemark.c.d > 0 and level1_signal > 0:
                    signal = StrategySignal(self.tf, timestamp)
                    signal.signal = StrategySignal.SIGNAL_ENTRY
                    signal.dir = 1
                    signal.p = self.price.close[-1]

                    if len(self.pivotpoint.resistances[1]):
                        signal.tp = np.max(self.pivotpoint.resistances[1])

                    # td9 cannot provide us a SL, take it from ATR
                    signal.sl = self.atr.stop_loss(signal.dir)

                    # Terminal.inst().info("Aggressive long entry %s %s" % (self.tomdemark.c.tdst, signal.sl), view='default')

                # aggressive long entry
                elif self.tomdemark.c.c >= 8 and self.tomdemark.c.d < 0 and level1_signal < 0:
                    signal = StrategySignal(self.tf, timestamp)
                    signal.signal = StrategySignal.SIGNAL_ENTRY
                    signal.dir = -1
                    signal.p = self.price.close[-1]

                    if len(self.pivotpoint.supports[1]):
                        signal.tp = np.min(self.pivotpoint.supports[1])

                    # td9 cannot provide us a SL, take it from ATR
                    signal.sl = self.atr.stop_loss(signal.dir)

                    # Terminal.inst().info("Aggressive short entry %s %s" % (self.tomdemark.c.tdst, signal.sl), view='default')

                #
                # setup completed
                #

                # sell-setup
                elif self.tomdemark.c.c >= 8 and self.tomdemark.c.d < 0 and level1_signal < 0:
                    signal = StrategySignal(self.tf, timestamp)
                    signal.signal = StrategySignal.SIGNAL_EXIT
                    signal.dir = 1
                    signal.p = self.price.close[-1]

                    # Terminal.inst().info("Exit long %s %s c8p-c9 (%s%s)" % (self.strategy_trader.instrument.symbol, self.tf, self.tomdemark.c.c, 'p' if signal.p else ''), view='default')

                # buy-setup
                elif self.tomdemark.c.c >= 8 and self.tomdemark.c.d > 0 and level1_signal > 0:
                    signal = StrategySignal(self.tf, timestamp)
                    signal.signal = StrategySignal.SIGNAL_EXIT
                    signal.dir = -1
                    signal.p = self.price.close[-1]
                    
                    # Terminal.inst().info("Exit short %s %s c8p-c9 (%s%s)" % (self.strategy_trader.instrument.symbol, self.tf, self.tomdemark.c.c, 'p' if signal.p else ''), view='default')

                #
                # setup aborted (@todo how to in this long/short case)
                #

                elif ((self.tomdemark.c.c >= 4 and self.tomdemark.c.c <= 7) and self.tomdemark.c.d < 0) and level1_signal < 0:
                    signal = StrategySignal(self.tf, timestamp)
                    signal.signal = StrategySignal.SIGNAL_EXIT
                    signal.dir = 1
                    signal.p = self.price.close[-1]

                    # Terminal.inst().info("Abort long %s %s c3-c7 (%s%s)" % (self.strategy_trader.instrument.symbol, self.tf, self.tomdemark.c.c, 'p' if signal.p else ''), view='default')

                elif ((self.tomdemark.c.c >= 4 and self.tomdemark.c.c <= 7) and self.tomdemark.c.d > 0) and level1_signal > 0:
                    signal = StrategySignal(self.tf, timestamp)
                    signal.signal = StrategySignal.SIGNAL_EXIT
                    signal.dir = -1
                    signal.p = self.price.close[-1]

                    # Terminal.inst().info("Abort long %s %s c3-c7 (%s%s)" % (self.strategy_trader.instrument.symbol, self.tf, self.tomdemark.c.c, 'p' if signal.p else ''), view='default')

                # #
                # # invalidation 2 of opposite setup
                # #

                # elif self.tomdemark.c.c > 2 and self.tomdemark.c.d > 0 and level1_signal < 0:
                #     signal = StrategySignal(self.tf, timestamp)
                #     signal.signal = StrategySignal.SIGNAL_EXIT
                #     signal.dir = 1
                #     signal.p = self.price.close[-1]

                #     Terminal.inst().info("Canceled long entry %s c2-c3, p:%s tf:%s" % (self.strategy_trader.instrument.symbol, signal.p, self.tf), view='default')

        if signal and signal.signal == StrategySignal.SIGNAL_ENTRY:
            # if level1_signal > 0 and len(self.pivotpoint.supports[1]):
            #     # cancel if not below a support (long direction)
            #     if self.price.last >= np.nanmax(self.pivotpoint.supports[1]):
            #         level1_signal = 0

            # if level1_signal < 0 and len(self.pivotpoint.resistances[1]):
            #     # cancel if not above a resistance (short direction)
            #     if self.price.last <= np.nanmin(self.pivotpoint.resistances[1]):
            #         level1_signal = 0

            if level1_signal > 0 and len(self.pivotpoint.supports[1]):
                # cancel if not below a support (long direction)
                if self.price.last >= np.nanmax(self.pivotpoint.supports[1]):
                    level1_signal = 0
                    signal = None

            if level1_signal < 0 and len(self.pivotpoint.resistances[1]):
                # cancel if not below a resistance (short direction)
                if self.price.last <= np.nanmin(self.pivotpoint.resistances[1]):
                    level1_signal = 0
                    signal = None

        return signal
Exemple #11
0
    def process(self, timeframe, timestamp):
        # process only when signals of base timeframe
        if timeframe != self.base_timeframe:
            return

        # update data at tick level
        if timeframe == self.base_timeframe:
            self.gen_candles_from_ticks(timestamp)

        accept, compute = self.filter_market(timestamp)
        if not accept:
            return

        # and compute
        entries = []
        exits = []

        if compute:
            # we might receive only LONG signals
            entries, exits = self.compute(timeframe, timestamp)

        #
        # global indicators
        #

        ref_price = self.timeframes[self.ref_timeframe].price.last
        # sma200 = self.timeframes[self.ref_timeframe].sma200.last
        ref_sma55 = self.timeframes[self.ref_timeframe].sma55.last
        ref_sma = self.timeframes[self.ref_timeframe].sma.last
        ref_ema = self.timeframes[self.ref_timeframe].ema.last

        if self._wait_bless == 1:
            if self._wait_timeout > 0 and timestamp >= self._wait_timeout:
                # wait for timeout
                self._wait_bless = -1
                self._wait_timeout = 0

            if ref_sma > ref_sma55:
                self._wait_bless = -1
                Terminal.inst().message("SMA > SMA55 %s (price=%s ema=%s sma=%s sma55=%s)!" % (self.instrument.market_id, ref_price, ref_ema, ref_sma, ref_sma55) , view='content')

        #
        # filters the entries
        #

        retained_entries = []

        for entry in entries:
            # filters entry signal, according to some correlation, parent timeframe signal or trend or trade regions
            parent_entry_tf = self.parent_timeframe(entry.timeframe)

            # only allowed range of signal for entry
            if not (self.min_traded_timeframe <= entry.timeframe <= self.max_traded_timeframe):
                continue

            # trade region
            if not self.check_regions(timestamp, self.instrument.market_bid, self.instrument.market_ofr, entry, self.region_allow):
                continue

            # ref timeframe is bear don't take the risk
            if not self.timeframes[self.sltp_timeframe].can_long:
                continue

            #
            # consecutives loosing trade, stop the curse for a moment
            #

            if self._wait_bless == 1:
                continue

            if self._wait_bless == 0 and self._stats['cont-loss'] > self.max_trades:
                self._wait_bless = 1
                self._wait_timeout = timestamp + 12*60*60  # 12h self-ban
                Terminal.inst().message("Curse !!! Wait a moment, or a favorable trend %s !" % self.instrument.market_id, view='content')
                continue

            if self._wait_bless == -1:
                self._wait_bless = 0

            # TDST does not give us a stop, ok lets find one using ATR
            if not entry.sl:
                sl = self.timeframes[self.sltp_timeframe].atr.stop_loss(entry.dir)
                if sl < self.instrument.open_exec_price(entry.dir):
                    entry.sl = sl

            # defines a target
            if not entry.tp:
                tp = self.timeframes[self.sltp_timeframe].pivotpoint.last_resistances[2]  #* 1.05

                gain = (tp - entry.p) / entry.p
                loss = (entry.p - entry.sl) / entry.p

                # if loss != 0 and (gain / loss < 1.0):
                #    continue

                # not enought potential profit
                if (tp - entry.p) / entry.p < 0.005:  # 0.5% min
                    continue

                entry.tp = tp
                entry.ptp = 1.0

            retained_entries.append(entry)

            # TP 50% entry
            # entry_50pc = StrategySignal(0, 0)
            # entry_50pc.dup(entry)
            # entry_50pc.tp = np.max(self.timeframes[self.sltp_timeframe].pivotpoint.resistances[0])#[-1]
            # entry_50pc.ptp = 0.25

            # retained_entries.append(entry_50pc)

        #
        # process exits signals
        #

        if self.trades:
            self.lock()

            for trade in self.trades:
                retained_exit = None

                # important, do not update user controlled trades if it have some operations
                if trade.is_user_trade() and trade.has_operations():
                    continue

                for signal in exits:
                    parent_signal_tf = self.parent_timeframe(signal.timeframe)

                    # receive an exit signal of the timeframe of the trade
                    if signal.timeframe == trade.timeframe:
                        retained_exit = signal
                        break

                    # exit from parent timeframe signal
                    # if parent_signal_tf == trade.timeframe: 
                    #     retained_exit = signal
                    #     break

                    # exit from any parent timeframe signal
                    # if signal.timeframe > trade.timeframe:
                    #     retained_exit = signal
                    #     break

                # can cancel a non filled trade if exit signal occurs before timeout (timeframe)
                # if trade.is_entry_timeout(timestamp, trade.timeframe):
                #     trader = self.strategy.trader()
                #     trade.cancel_open(trader)
                #     Terminal.inst().info("Canceled order (exit signal or entry timeout) %s" % (self.instrument.market_id,), view='default')
                #     continue

                if trade.is_opened() and not trade.is_valid(timestamp, trade.timeframe):
                    # @todo re-adjust entry
                    Terminal.inst().info("Update order %s trade %s TODO" % (trade.id, self.instrument.market_id,), view='default')
                    continue

                # only for active and currently not closing trades
                if not trade.is_active() or trade.is_closing() or trade.is_closed():
                    continue

                close_exec_price = self.instrument.close_exec_price(trade.dir)

                #
                # stop-loss update
                #

                stop_loss = trade.sl

                # ATR stop-loss
                sl = self.timeframes[self.sltp_timeframe].atr.stop_loss(trade.direction)
                # if (not trade.sl or close_exec_price > trade.entry_price) and sl > stop_loss:
                if sl > stop_loss:
                    stop_loss = sl

                if 0:#not trade.sl:
                    trade.sl = stop_loss

                # increase in-profit stop-loss in long direction
                # @todo howto ?
                # for resistances in self.timeframes[trade.timeframe].pivotpoint.resistances:
                #     if len(resistances):
                #         level = resistances[-1]
                #         if level < close_exec_price and level > stop_loss:
                #             stop_loss = level

                # pivots = self.timeframes[trade.timeframe].pivotpoint.pivots:
                # if len(pivots):
                #     level = pivots[-1]
                #     if level < close_exec_price and level > stop_loss:
                #         stop_loss = level

                # a try using bbands
                # level = self.timeframes[trade.timeframe].bollingerbands.last_ma
                # if level >= stop_loss:
                #     stop_loss = level               

                if self.timeframes[self.sltp_timeframe].pivotpoint.last_pivot > 0.0:
                    update_tp = False

                    if close_exec_price > self.timeframes[self.sltp_timeframe].pivotpoint.last_resistances[2]:
                        #Terminal.inst().info("Price above R2 %s %s" % (trade.id, self.instrument.market_id), view='content')
                        update_tp = True
                        if stop_loss < self.timeframes[self.sltp_timeframe].pivotpoint.last_resistances[1]:
                            trade.sl = self.timeframes[self.sltp_timeframe].pivotpoint.last_resistances[1]

                    elif close_exec_price > self.timeframes[self.sltp_timeframe].pivotpoint.last_resistances[1]:
                        #Terminal.inst().info("Price above R1 %s %s" % (trade.id, self.instrument.market_id), view='content')
                        update_tp = True
                        if stop_loss < self.timeframes[self.sltp_timeframe].pivotpoint.last_resistances[0]:
                           trade.sl = self.timeframes[self.sltp_timeframe].pivotpoint.last_resistances[0]

                    elif close_exec_price > self.timeframes[self.sltp_timeframe].pivotpoint.last_resistances[0]:
                        #Terminal.inst().info("Price above R0 %s %s" % (trade.id, self.instrument.market_id), view='content')
                        update_tp = True
                        #if stop_loss < self.timeframes[self.sltp_timeframe].pivotpoint.last_pivot:
                        #    trade.sl = self.timeframes[self.sltp_timeframe].pivotpoint.last_pivot

                    elif close_exec_price > self.timeframes[self.sltp_timeframe].pivotpoint.last_pivot:
                        #Terminal.inst().info("Price above pivot %s %s" % (trade.id, self.instrument.market_id), view='content')
                        update_tp = True
                        #if stop_loss < self.timeframes[self.sltp_timeframe].pivotpoint.last_supports[0]:
                        #    trade.sl = self.timeframes[self.sltp_timeframe].pivotpoint.last_supports[0]

                    elif close_exec_price > self.timeframes[self.sltp_timeframe].pivotpoint.last_supports[0]:
                        #Terminal.inst().info("Price above S0 %s %s" % (trade.id, self.instrument.market_id), view='content')
                        update_tp = True
                        #if trade.sl < self.timeframes[self.sltp_timeframe].pivotpoint.last_supports[1]:
                        #   trade.sl = self.timeframes[self.sltp_timeframe].pivotpoint.last_supports[1]

                    elif close_exec_price > self.timeframes[self.sltp_timeframe].pivotpoint.last_supports[1]:
                        Terminal.inst().info("Price above S1 %s %s" % (trade.id, self.instrument.market_id), view='content')
                        update_tp = True
                        #if trade.sl < self.timeframes[self.sltp_timeframe].pivotpoint.last_supports[2]:
                        #    trade.sl = self.timeframes[self.sltp_timeframe].pivotpoint.last_supports[2]

                    elif close_exec_price > self.timeframes[self.sltp_timeframe].pivotpoint.last_supports[2]:
                        #Terminal.inst().info("Price above S2 %s %s" % (trade.id, self.instrument.market_id), view='content')
                        update_tp = True
                        # if close_exec_price < self.timeframes[self.sltp_timeframe].pivotpoint.last_supports[2]:
                        #    trade.sl = sl  #self.timeframes[self.sltp_timeframe].pivotpoint.last_supports[2]

                    #
                    # target update
                    #

                    tp = self.timeframes[self.sltp_timeframe].pivotpoint.last_resistances[int(2*trade.partial_tp)] # * 1.05

                    # enought potential profit (0.5% min target)
                    if (tp - close_exec_price) / close_exec_price > 0.005 and update_tp: # and tp > trade.tp:
                       trade.tp = tp

                    #gain = (trade.tp - close_exec_price) / close_exec_price
                    #loss = (close_exec_price - trade.sl) / close_exec_price

                    gain = (trade.tp - trade.entry_price) / trade.entry_price
                    loss = (trade.entry_price - trade.sl) / trade.entry_price

                pl = (close_exec_price - trade.entry_price) / trade.entry_price

                # if loss != 0 and gain / loss < 1.0 or pl < -0.035: # and close_exec_price < trade.entry_price:
                # @todo or at max loss config and as initial sl (worst value)
                if pl < -0.035: # and close_exec_price < trade.entry_price:
                    # not enough chance to win on this trade, cut the loss
                    retained_exit = StrategySignal(self.sltp_timeframe, timestamp)
                    retained_exit.signal = StrategySignal.SIGNAL_EXIT
                    retained_exit.dir = 1
                    retained_exit.p = close_exec_price
                    Terminal.inst().info("No more chance to win cut the trade %s %s %s" % (trade.id, self.instrument.market_id, gain / loss), view='content')

                #
                # exit trade if an exit signal retained
                #

                if retained_exit:
                    self.process_exit(timestamp, trade, retained_exit.price)
                    Terminal.inst().info("Exit trade %s %s" % (self.instrument.symbol, trade.id), view='content')

            self.unlock()

        # update actives trades
        self.update_trades(timestamp)

        # retained long entry do the order entry signal
        for entry in retained_entries:
            self.process_entry(timestamp, entry.price, entry.tp, entry.sl, entry.timeframe, entry.partial_tp)

        # streaming
        self.stream()
Exemple #12
0
    def process1(self, timestamp, last_timestamp, candles, prices, volumes):
        signal = None

        # volume sma, increase signal strength when volume increase over its SMA
        # volume_sma = utils.MM_n(self.depth-1, self.volume.volumes)

        rsi = 0
        rsi_30_70 = 0  # 1 <30, -1 >70
        rsi_40_60 = 0  # 1 if RSI in 40-60
        # rsi_trend = 0

        stochrsi = 0
        stochrsi_20_80 = 0  # 1 <20, -1 >80
        stochrsi_40_60 = 0  # 1 if stochRSI in 40-60

        volume_signal = 0

        ema_sma_cross = 0
        ema_sma_height = 0

        if self.rsi:
            self.rsi.compute(timestamp, prices)

            if self.rsi.last < self.rsi_low:
                rsi_30_70 = 1.0
            elif rsi > self.rsi_high:
                rsi_30_70 = -1.0

            if self.rsi.last > 0.4 and self.rsi.last < 0.6:
                rsi_40_60 = 1

        if self.stochrsi:
            self.stochrsi.compute(timestamp, prices)
            stochrsi = self.stochrsi.last_k

            if stochrsi < 0.2:
                stochrsi_20_80 = 1.0
            elif stochrsi > 0.8:
                stochrsi_20_80 = -1.0

            if stochrsi > 0.4 and stochrsi < 0.6:
                stochrsi_40_60 = 1

        # if self.volume.last > volume_sma[-1]:
        #     volume_signal = 1
        # elif self.volume.last < volume_sma[-1]:
        #     volume_signal = -1

        if self.sma and self.ema:
            self.sma.compute(timestamp, prices)
            self.ema.compute(timestamp, prices)

            # ema over sma crossing
            ema_sma_cross = utils.cross((self.ema.prev, self.sma.prev),
                                        (self.ema.last, self.sma.last))

            if self.ema.last > self.sma.last:
                ema_sma_height = 1
            elif self.ema.last < self.sma.last:
                ema_sma_height = -1

        if self.bollingerbands:

            self.bollingerbands.compute(timestamp, prices)

            bb_break = 0
            bb_ma = 0

            if prices[-1] > self.bollingerbands.last_top:
                bb_break = 1
            elif prices[-1] < self.bollingerbands.last_bottom:
                bb_break = -1

            if prices[-1] > self.bollingerbands.last_ma:
                bb_ma = -1
            elif prices[-1] > self.bollingerbands.last_ma:
                bb_ma = 1

        if self.tomdemark:
            # last_timestamp
            self.tomdemark.compute(timestamp, self.price.timestamp,
                                   self.price.high, self.price.low,
                                   self.price.close)

            #
            # setup entry
            #

            # long entry on sell-setup + rsi oversell
            # soit on rentre prudent sur une 2 ou 3 au dessus de la precedent, et en plus rsi ou stochrsi oversell
            # soit on rentre bourrin sur une 1 avec close plus haute
            # soit on rentre semi bourrin sur une 2 seulement rien, d'autre
            # soit on rentre semi prudent sur une 2 au dessus de la precente, et rien de plus
            # soit on rentrer sur une 2 après une 9 du meme setup, avec close au dessus de la 9
            # on peut aussi renter selon bollinger
            #   - bollinger dans le nuage
            #   - bollinger break down avec stochRSI or RSI basse
            if (self.tomdemark.c.c == 2 and self.tomdemark.c.d < 0
                    and self.price.close[-1] > self.price.close[-2]) or (
                        self.tomdemark.c.c == 3):  # avec td3
                # # if (td.c == 1 and td.d < 0 and candles[-1].close > candles[-2].close):
                # if (td.c == 2 and td.d < 0 and candles[-1].close > candles[-2].close):
                # if ((td.c == 2 or td.c == 3) and td.d < 0 and candles[-1].close > candles[-2].close):
                # if ((td.c == 2 or td.c == 3) and td.d < 0) and candles[-1].close > candles[-2].close:
                # if bb_break == 0:
                # if (bb_break == 0 and bb_ma < 0):  # test with aggressive + bb
                # if (stochrsi_20_80 > 0 or rsi_30_70 > 0):
                # if (ema_sma_height > 0 or rsi_30_70 > 0) and bb_break >= 0:
                # if (bb_break == 0 and bb_ma < 0) and (stochrsi_20_80 > 0 or rsi_30_70 > 0):  # used with average case but not with aggressive
                # if (bb_break == 0 and bb_ma < 0) and (ema_sma_height > 0 or rsi_30_70 > 0):  #  and volume_signal > 0:
                # if (stochrsi_20_80 > 0 or rsi_30_70 > 0):  # la classique mais prend des risk, a voir avec un bon SL peut-être
                # if (rsi_trend > 0) and (ema_sma_height > 0 or rsi_30_70 > 0):  # protege de perte mais rate des gains
                # if (ema_sma_height > 0 or rsi_30_70 > 0):  #  and volume_signal > 0:  # C4
                # if ema_sma_height > 0:  # C5
                # if ema_sma_height > 0 and bb_break >= 0 and rsi_trend > 0:  # C6
                # if ema_sma_height > 0 and bb_break >= 0 and stochrsi_20_80 > 0:
                # if 1:  # C1
                # if rsi_trend > 0 and bb_break > 0:  # pas mal evite des entrees foireuses mais pas assez de profits
                # if (stochrsi_20_80 > 0 or rsi_30_70 > 0):  # la classique mais prend des risk, il faut un bon stop-loss
                if (ema_sma_height > 0 or rsi_30_70 > 0) and bb_break <= 0:
                    # if rsi_trend and (stochrsi_20_80 > 0 or rsi_30_70 > 0):
                    # if (stochrsi_20_80 > 0 and rsi_30_70 > 0):
                    signal = StrategySignal(self.tf, timestamp)
                    signal.signal = StrategySignal.SIGNAL_ENTRY
                    signal.dir = 1
                    signal.p = self.price.close[-1]

                    if self.tomdemark.c.tdst:
                        signal.sl = self.tomdemark.c.tdst

            # aggressive entry
            if ((self.tomdemark.c.c == 9 or
                 (self.tomdemark.c.c == 8 and self.tomdemark.c.p))
                    and self.tomdemark.c.d > 0):
                # if stochrsi_20_80 > 0 and rsi_30_70 > 0 and candles[-1].close > candles[-2].close:  # C1 -
                # if 1:  # C4 +++
                # if stochrsi_20_80 > 0 and candles[-1].close > candles[-2].close:  # C3  ++
                # if rsi_30_70 > 0 and candles[-1].close > candles[-2].close:  # C5  ??
                # if ema_sma_height > 0 and bb_break >= 0 and rsi_trend > 0:  # C6 ??
                # if ema_sma_height > 0 and bb_break >= 0 and stochrsi_20_80 > 0:  # C7 ??
                # if (ema_sma_height > 0 or rsi_30_70 > 0):  # C2 +
                # if stochrsi_20_80 > 0 and candles[-1].close > candles[-2].close:  # C3  ++
                if (stochrsi_20_80 > 0 or rsi_30_70 > 0):  # C8 ??
                    signal = StrategySignal(self.tf, timestamp)
                    signal.signal = StrategySignal.SIGNAL_ENTRY
                    signal.dir = 1
                    signal.p = self.price.close[-1]

            #
            # invalidation 2 of opposite setup
            #

            # @todo or if a >= 3 <= 7 close below the previous candle close
            # can make loss in bull run
            # second condition might be optimized
            elif (self.tomdemark.c.c == 2 and self.tomdemark.c.d > 0
                  and self.price.close[-1] < self.price.close[-2]) or (
                      self.tomdemark.c.c == 3 and self.tomdemark.c.d > 0):
                # elif td.c == 3 and td.d > 0:  # and candles[-1].close < candles[-2].close:
                # long cancelation on buy-setup formation
                # if ema_sma_height < 0 or rsi_40_60 > 0:  # (excess of volume and ema under sma)
                # if stochrsi_20_80 < 0:  # and volume_signal > 0:
                # if bb_break < 0:
                # if (rsi_trend < 0):  # C2
                # if 1:  # C1 +
                # if (bb_break == 0 and bb_ma < 0):  # C3 ++
                if ema_sma_height < 0:  #  and volume_signal > 0:
                    signal = StrategySignal(self.tf, timestamp)
                    signal.signal = StrategySignal.SIGNAL_EXIT  # CANCEL
                    signal.dir = 1
                    signal.p = self.price.close[-1]

            #
            # setup completed
            #

            elif (((self.tomdemark.c.c >= 8 and self.tomdemark.c.p) or
                   (self.tomdemark.c.c == 9)) and self.tomdemark.c.d <
                  0):  # and candles[-1].close > candles[-2].close:
                if 1:  # stochrsi_20_80 > 1:
                    signal = StrategySignal(self.tf, timestamp)
                    signal.signal = StrategySignal.SIGNAL_EXIT
                    signal.dir = 1
                    signal.p = self.price.close[-1]

            #
            # setup aborted
            #

            elif ((self.tomdemark.c.c >= 2 and self.tomdemark.c.c <= 7)
                  and self.tomdemark.c.d < 0
                  ) and self.price.close[-1] < self.price.close[-2]:  # C1
                # if stochrsi_20_80 < 0 or rsi_30_70 < 0:  # bearish stoch OR rsi  # C1
                if stochrsi_20_80 < 0 and rsi_30_70 < 0:  # bearish stoch AND rsi (seems better)  # C2
                    signal = StrategySignal(self.tf, timestamp)
                    signal.signal = StrategySignal.SIGNAL_EXIT
                    signal.dir = 1
                    signal.p = self.price.close[-1]

            #
            # CD entry
            #

            if self.tomdemark.cd.c == 1:
                # count-down buy-setup + rsi oversell
                if self.tomdemark.c.d < 0:  # and candles[-1].close > candles[-2].high:
                    # if rsi_30_70 > 0:
                    signal = StrategySignal(self.tf, timestamp)
                    signal.signal = StrategySignal.SIGNAL_ENTRY
                    signal.dir = 1
                    signal.p = self.price.last
                    signal.sl = self.tomdemark.c.tdst

            #
            # CD13 setup
            #

            elif self.tomdemark.cd.c == 13:
                # count-down sell-setup completed
                if self.tomdemark.cd.d < 0:
                    signal = StrategySignal(self.tf, timestamp)
                    signal.signal = StrategySignal.SIGNAL_EXIT
                    signal.p = self.price.close[-1]
                    signal.dir = 1

        return signal
Exemple #13
0
    def process4(self, timestamp, last_timestamp, candles, prices, volumes):
        signal = None

        # volume sma, increase signal strength when volume increase over its SMA
        volume_sma = utils.MM_n(self.depth - 1, self.volume.volumes)

        rsi_30_70 = 0  # 1 <30, -1 >70
        rsi_40_60 = 0  # 1 if RSI in 40-60
        rsi_trend = 0

        stochrsi_20_80 = 0  # 1 <20, -1 >80
        stochrsi_40_60 = 0  # 1 if stochRSI in 40-60

        volume_signal = 0

        ema_sma_cross = 0
        ema_sma_height = 0

        if self.rsi:
            self.rsi.compute(timestamp, prices)

            if self.rsi.last < self.rsi_low:
                rsi_30_70 = 1.0
            elif self.rsi.last > self.rsi_high:
                rsi_30_70 = -1.0

            if self.rsi.last > 0.4 and self.rsi.last < 0.6:
                rsi_40_60 = 1

            rsi_trend = utils.trend_extremum(self.rsi.rsis)

        # if self.stochrsi:
        #     self.stochrsi.compute(timestamp, prices)

        #     if self.stochrsi.last_k < 0.2:
        #         stochrsi_20_80 = 1.0
        #     elif self.stochrsi.last_k > 0.8:
        #         stochrsi_20_80 = -1.0

        #     if self.stochrsi.last_k > 0.4 and self.stochrsi.last_k < 0.6:
        #         stochrsi_40_60 = 1

        # if self.volume_ema:
        #     self.volume_ema.compute(timestamp, volumes)

        #     if self.volume.last > self.volume_ema.last:
        #         volume_signal = 1
        #     elif self.volume.last < self.volume_ema.last:
        #         volume_signal = -1

        if self.sma and self.ema:
            self.sma.compute(timestamp, prices)
            self.ema.compute(timestamp, prices)

            # ema over sma crossing
            ema_sma_cross = utils.cross((self.ema.prev, self.sma.prev),
                                        (self.ema.last, self.sma.last))

            if self.ema.last > self.sma.last:
                ema_sma_height = 1
            elif self.ema.last < self.sma.last:
                ema_sma_height = -1

        if self.bollingerbands:
            self.bollingerbands.compute(timestamp, prices)

            bb_break = 0
            bb_ma = 0

            if prices[-1] > self.bollingerbands.last_top:
                bb_break = 1
            elif prices[-1] < self.bollingerbands.last_bottom:
                bb_break = -1

        #     if prices[-1] > self.bollingerbands.last_ma:
        #         bb_ma = -1
        #     elif prices[-1] > self.bollingerbands.last_ma:
        #         bb_ma = 1

        if self.bsawe:
            bsawe = self.bsawe.compute(timestamp, self.price.high,
                                       self.price.low, self.price.close)

        ema_sma = 0

        if self.ema.last < self.sma.last:
            # bear trend
            if self.rsi.last > 0.5:  # initial: 0.5
                ema_sma = -1
            elif self.rsi.last < 0.2:  # initial: 0.2
                ema_sma = 1
        else:
            # bull trend
            if self.rsi.last > 0.8:  # initial: 0.8
                ema_sma = -1
            elif self.rsi.last < 0.6:  # initial: 0.6
                ema_sma = 1

        # @todo dip, bounce or trend...

        if bsawe > 0 and ema_sma > 0:
            signal = StrategySignal(self.tf, timestamp)
            signal.signal = StrategySignal.SIGNAL_ENTRY
            signal.dir = 1
            signal.p = self.price.close[-1]

        elif bsawe < 0 and ema_sma < 0:
            # exit signal
            signal = StrategySignal(self.tf, timestamp)
            signal.signal = StrategySignal.SIGNAL_EXIT
            signal.dir = 1
            signal.p = self.price.close[-1]

        if self.tomdemark:
            # last_timestamp
            self.tomdemark.compute(timestamp, self.price.timestamp,
                                   self.price.high, self.price.low,
                                   self.price.close)

            if 0:  # self.tomdemark.c.c >= 8 and self.tomdemark.c.d < 0 and (ema_sma < 0 or bsawe < 0):
                # setup complete and trend change
                signal = StrategySignal(self.tf, timestamp)
                signal.signal = StrategySignal.SIGNAL_EXIT
                signal.dir = 1
                signal.p = self.price.close[-1]

            elif 2 <= self.tomdemark.c.c <= 5 and self.tomdemark.c.d > 0 and (
                    ema_sma < 0 and bsawe < 0):
                # cancelation
                signal = StrategySignal(self.tf, timestamp)
                signal.signal = StrategySignal.SIGNAL_EXIT
                signal.dir = 1
                signal.p = self.price.close[-1]

        return signal
Exemple #14
0
    def process3(self, timestamp, last_timestamp, candles, prices, volumes):
        signal = None

        # volume sma, increase signal strength when volume increase over its SMA
        # volume_sma = utils.MM_n(self.depth-1, self.volume.volumes)

        rsi_30_70 = 0  # 1 <30, -1 >70
        rsi_40_60 = 0  # 1 if RSI in 40-60
        rsi_trend = 0

        stochrsi_20_80 = 0  # 1 <20, -1 >80
        stochrsi_40_60 = 0  # 1 if stochRSI in 40-60

        volume_signal = 0

        ema_sma_cross = 0
        ema_sma_height = 0

        if self.rsi:
            self.rsi.compute(last_timestamp, prices)

            if self.rsi.last < self.rsi_low:
                rsi_30_70 = 1.0
            elif self.rsi.last > self.rsi_high:
                rsi_30_70 = -1.0

            if self.rsi.last > 0.4 and self.rsi.last < 0.6:
                rsi_40_60 = 1

            rsi_trend = utils.trend_extremum(self.rsi.rsis)

        if self.stochrsi:
            self.stochrsi.compute(last_timestamp, prices)

            if self.stochrsi.last_k < 0.2:
                stochrsi_20_80 = 1.0
            elif self.stochrsi.last_k > 0.8:
                stochrsi_20_80 = -1.0

            if self.stochrsi.last_k > 0.4 and self.stochrsi.last_k < 0.6:
                stochrsi_40_60 = 1

        # if self.volume.last > volume_sma[-1]:
        #     volume_signal = 1
        # elif self.volume.last < volume_sma[-1]:
        #     volume_signal = -1

        if self.sma and self.ema:
            self.sma.compute(last_timestamp, prices)
            self.ema.compute(last_timestamp, prices)

            # ema over sma crossing
            ema_sma_cross = utils.cross((self.ema.prev, self.sma.prev),
                                        (self.ema.last, self.sma.last))

            if self.ema.last > self.sma.last:
                ema_sma_height = 1
            elif self.ema.last < self.sma.last:
                ema_sma_height = -1

        if self.bollingerbands:
            self.bollingerbands.compute(last_timestamp, prices)

            bb_break = 0
            bb_ma = 0

            if prices[-1] > self.bollingerbands.last_top:
                bb_break = 1
            elif prices[-1] < self.bollingerbands.last_bottom:
                bb_break = -1

            if prices[-1] > self.bollingerbands.last_ma:
                bb_ma = -1
            elif prices[-1] > self.bollingerbands.last_ma:
                bb_ma = 1

        if self.atr:
            self.atr.compute(last_timestamp, self.price.high, self.price.low,
                             self.price.close)

        level1_signal = 0

        if self.ema.last < self.sma.last:
            # bear trend
            if self.rsi.last > 0.5:  # initial: 0.5
                level1_signal = -1
            elif self.rsi.last < 0.2:  # initial: 0.2
                level1_signal = 1
        else:
            # bull trend
            if self.rsi.last > 0.8:  # initial: 0.8
                level1_signal = -1
            elif self.rsi.last < 0.6:  # initial: 0.6
                level1_signal = 1

        if self.tomdemark:
            self.tomdemark.compute(last_timestamp, self.price.timestamp,
                                   self.price.high, self.price.low,
                                   self.price.close)

            #
            # setup entry
            #

            # long entry on sell-setup + rsi oversell
            if (self.tomdemark.c.c == 2 and self.tomdemark.c.d < 0
                    and self.price.close[-1] > self.price.close[-2]) or (
                        self.tomdemark.c.c == 3):  # avec td3
                # if (td.c == 1 and td.d < 0 and candles[-1].close > candles[-2].close):
                # if (td.c == 2 and td.d < 0 and candles[-1].close > candles[-2].close):
                # if ((td.c == 2 or td.c == 3) and td.d < 0 and candles[-1].close > candles[-2].close):
                # if ((td.c == 2 or td.c == 3) and td.d < 0) and candles[-1].close > candles[-2].close:
                # if bb_break == 0:
                # if (bb_break == 0 and bb_ma < 0):  # test with aggressive + bb
                # if (stochrsi_20_80 > 0 or rsi_30_70 > 0):
                # if (ema_sma_height > 0 or rsi_30_70 > 0) and bb_break >= 0:
                # if (bb_break == 0 and bb_ma < 0) and (stochrsi_20_80 > 0 or rsi_30_70 > 0):  # used with average case but not with aggressive
                # if (bb_break == 0 and bb_ma < 0) and (ema_sma_height > 0 or rsi_30_70 > 0):  #  and volume_signal > 0:
                # if (stochrsi_20_80 > 0 or rsi_30_70 > 0):  # la classique mais prend des risk, a voir avec un bon SL peut-être
                # if (rsi_trend > 0) and (ema_sma_height > 0 or rsi_30_70 > 0):  # protege de perte mais rate des gains
                # if (ema_sma_height > 0 or rsi_30_70 > 0):  #  and volume_signal > 0:  # C4
                # if ema_sma_height > 0:  # C5
                # if ema_sma_height > 0 and bb_break >= 0 and rsi_trend > 0:  # C6
                # if ema_sma_height > 0 and bb_break >= 0 and stochrsi_20_80 > 0:
                # if 1:  # C1
                # if rsi_trend > 0 and bb_break > 0:  # pas mal evite des entrees foireuses mais pas assez de profits
                if level1_signal > 0:
                    # if (stochrsi_20_80 > 0 or rsi_30_70 > 0):  # la classique mais prend des risk, il faut un bon stop-loss
                    # if rsi_trend and (stochrsi_20_80 > 0 or rsi_30_70 > 0):
                    # if (stochrsi_20_80 > 0 and rsi_30_70 > 0):
                    signal = StrategySignal(self.tf, timestamp)
                    signal.signal = StrategySignal.SIGNAL_ENTRY
                    signal.dir = 1
                    signal.p = self.price.close[-1]

                    if self.tomdemark.c.tdst:
                        signal.sl = self.tomdemark.c.tdst

            # aggressive entry
            if self.tomdemark.c.c == 9 and self.tomdemark.c.d > 0:
                # if ((self.tomdemark.c.c == 9 or (self.tomdemark.c.c == 8 and self.tomdemark.c.p)) and self.tomdemark.c.d > 0):
                # if stochrsi_20_80 > 0 and rsi_30_70 > 0 and candles[-1].close > candles[-2].close:  # C1 -
                # if 1:  # C4 +++
                # if stochrsi_20_80 > 0 and candles[-1].close > candles[-2].close:  # C3  ++
                # if rsi_30_70 > 0 and candles[-1].close > candles[-2].close:  # C5  ??
                # if ema_sma_height > 0 and bb_break >= 0 and rsi_trend > 0:  # C6 ??
                # if ema_sma_height > 0 and bb_break >= 0 and stochrsi_20_80 > 0:  # C7 ??
                # if (ema_sma_height > 0 or rsi_30_70 > 0):  # C2 +
                # if 1:
                # if stochrsi_20_80 > 0 and candles[-1].close > candles[-2].close:  # C3  ++
                # if (stochrsi_20_80 > 0 or rsi_30_70 > 0):  # C8 ??
                if level1_signal > 0:
                    signal = StrategySignal(self.tf, timestamp)
                    signal.signal = StrategySignal.SIGNAL_ENTRY
                    signal.dir = 1
                    signal.p = self.price.close[-1]

            #
            # invalidation 2 of opposite setup
            #

            # @todo or if a >= 3 <= 7 close below the previous candle close
            # can make loss in bull run
            # second condition might be optimized
            elif (self.tomdemark.c.c == 2 and self.tomdemark.c.d > 0
                  and self.price.close[-1] < self.price.close[-2]) or (
                      self.tomdemark.c.c == 3 and self.tomdemark.c.d > 0):
                # elif td.c == 3 and td.d > 0:  # and self.price.close < self.price.close:
                # long cancelation on buy-setup formation
                # if ema_sma_height < 0 or rsi_40_60 > 0:  # (excess of volume and ema under sma)
                # if stochrsi_20_80 < 0:  # and volume_signal > 0:
                # if bb_break < 0:
                # if (rsi_trend < 0):  # C2
                # if ema_sma_height < 0:  #  and volume_signal > 0:
                if 1:  # C1 +
                    # if level1_signal < 0:
                    # if (bb_break == 0 and bb_ma < 0):  # C3 ++
                    signal = StrategySignal(self.tf, timestamp)
                    signal.signal = StrategySignal.SIGNAL_EXIT  # CANCEL
                    signal.dir = 1
                    signal.p = self.price.close[-1]

            #
            # setup completed
            #

            elif (((self.tomdemark.c.c >= 8 and self.tomdemark.c.p) or
                   (self.tomdemark.c.c == 9)) and self.tomdemark.c.d <
                  0):  # and self.price.close[-1] > self.price.close[-2]:
                # if 1:  # stochrsi_20_80 > 1:
                if 1:  # if level1_signal < 0:
                    signal = StrategySignal(self.tf, timestamp)
                    signal.signal = StrategySignal.SIGNAL_EXIT
                    signal.dir = 1
                    signal.p = self.price.close[-1]

            #
            # setup aborted
            #

            elif ((self.tomdemark.c.c >= 2 and self.tomdemark.c.c <= 7)
                  and self.tomdemark.c.d < 0
                  ) and self.price.close[-1] < self.price.close[-2]:  # C1
                # if stochrsi_20_80 < 0 or rsi_30_70 < 0:  # bearish stoch OR rsi  # C1
                # if stochrsi_20_80 < 0 and rsi_30_70 < 0:  # bearish stoch AND rsi (seems better)  # C2
                if 1:  # level1_signal < 0:
                    signal = StrategySignal(self.tf, timestamp)
                    signal.signal = StrategySignal.SIGNAL_EXIT
                    signal.dir = 1
                    signal.p = self.price.close[-1]

            #
            # CD entry
            #

            if self.tomdemark.cd.c >= 1 and self.tomdemark.cd.c <= 3:
                # count-down buy-setup + rsi oversell
                if self.tomdemark.cd.d < 0:  # and self.price.close[-1] > self.price.high[-2]:
                    # if rsi_30_70 > 0:
                    signal = StrategySignal(self.tf, timestamp)
                    signal.signal = StrategySignal.SIGNAL_ENTRY
                    signal.dir = 1
                    signal.p = self.price.close[-1]
                    signal.sl = self.tomdemark.c.tdst

            #
            # CD13 setup
            #

            elif self.tomdemark.cd.c == 13:
                # count-down sell-setup completed
                if self.tomdemark.cd.d < 0:
                    signal = StrategySignal(self.tf, timestamp)
                    signal.signal = StrategySignal.SIGNAL_EXIT
                    signal.p = self.price.close[-1]
                    signal.dir = 1

        return signal
Exemple #15
0
    def process1(self, timestamp, last_timestamp, candles, prices, volumes):
        signal = None

        # volume sma, increase signal strength when volume increase over its SMA
        # volume_sma = utils.MM_n(self.depth-1, self.volume.volumes)

        #
        # signals analysis
        #

        rsi_30_70 = 0  # -1 30, 1 70
        rsi_40_60 = 0  # 1 if RSI in 40-60
        volume_signal = 0
        ema_sma_cross = 0
        ema_sma_height = 0

        if self.rsi:
            self.rsi.compute(last_timestamp, prices)

            if self.rsi.last < self.rsi_low:
                rsi_30_70 = 1.0
            elif self.rsi.last > self.rsi_high:
                rsi_30_70 = -1.0

            if self.rsi.last > 0.4 and self.rsi.last < 0.6:
                rsi_40_60 = 1

        # if self.volume.last > volume_sma[-1]:
        #     volume_signal = 1
        # elif self.volume.last < volume_sma[-1]:
        #     volume_signal = -1

        if self.sma200:
            self.sma200.compute(last_timestamp, prices)

        if self.sma55:
            self.sma55.compute(last_timestamp, prices)

        if self.sma and self.ema:
            self.sma.compute(last_timestamp, prices)
            self.ema.compute(last_timestamp, prices)

            # ema over sma crossing
            ema_sma_cross = utils.cross((self.ema.prev, self.sma.prev),
                                        (self.ema.last, self.sma.last))

            if self.ema.last > self.sma.last:
                ema_sma_height = 1
            elif self.ema.last < self.sma.last:
                ema_sma_height = -1

        bb_way = 0

        if self.bollingerbands:
            self.bollingerbands.compute(last_timestamp, prices)

            if self.bollingerbands.last_ma < prices[
                    -1] < self.bollingerbands.last_top:
                bb_way = -1

        # if ema_sma_cross > 0 and rsi_30_70 > 0:
        #     self.trend = 1

        # elif ema_sma_cross < 0 and rsi_30_70 < 0:
        #     signal = StrategySignal(self.tf, timestamp)
        #     signal.signal = StrategySignal.SIGNAL_EXIT
        #     signal.dir = 1
        #     signal.p = candles[-1].close
        #     self.trend = -1
        # else:
        #     self.trend = 0

        level1_signal = 0

        if self.ema.last < self.sma.last:
            # bear trend
            if self.rsi.last > 0.5:  # initial: 0.5
                level1_signal = -1
            elif self.rsi.last < 0.2:  # initial: 0.2
                level1_signal = 1
        else:
            # bull trend
            if self.rsi.last > 0.8:  # initial: 0.8
                level1_signal = -1
            elif self.rsi.last < 0.6:  # initial: 0.6
                level1_signal = 1

        if level1_signal < 0:
            signal = StrategySignal(self.tf, timestamp)
            signal.signal = StrategySignal.SIGNAL_EXIT
            signal.dir = 1
            signal.p = candles[-1].close
            self.trend = -1
        elif level1_signal > 0:
            self.trend = 1
        else:
            self.trend = 0

        self.can_long = self.trend >= 0

        if self.pivotpoint:
            self.pivotpoint.compute(last_timestamp, self.price.open,
                                    self.price.high, self.price.low,
                                    self.price.close)

        if self.atr:
            self.atr.compute(last_timestamp, self.price.high, self.price.low,
                             self.price.close)

        return signal