Ejemplo n.º 1
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
Ejemplo n.º 2
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
Ejemplo n.º 3
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
Ejemplo n.º 4
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
Ejemplo n.º 5
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