技术指标--TRIX

Python TRIX 底部反转 顶部反转 金叉 死叉

"""
   TRIX 底部反转 顶部反转 金叉 死叉
"""
from scipy import signal
import numpy as np
import talib


class TrixBreak:
    def __init__(self, close):
        self.close = close

    def __get_trix(self, timeperiod):
        tr = talib.TRIX(self.close, timeperiod=timeperiod)
        return tr[~np.isnan(tr)]

    def bottom_reverse(self, timeperiod=12):
        """
        底部V型反转
        :return: time trix[time]
        """
        tr = self.__get_trix(timeperiod=timeperiod)
        position = signal.argrelextrema(tr.values, np.less)
        if len(position[-1]) > 0:
            return [tr.index[position[-1][-1]], tr[position[-1][-1]]]
        else:
            return []

    def top_reverse(self, timeperiod=12):
        """
        顶部倒V型反转
        :return: time trix[time]
        """
        tr = self.__get_trix(timeperiod=timeperiod)
        position = signal.argrelextrema(tr.values, np.greater)
        if len(position[-1]) > 0:
            return [tr.index[position[-1][-1]], tr[position[-1][-1]]]
        else:
            return []

    def golden_cross(self, trix_period=12, trma_period=20):
        """
        金叉 TRIX up cross TRMA
        :return: [time trix[time]]
        """
        tr = self.__get_trix(timeperiod=trix_period)
        trma = tr.rolling(trma_period, min_periods=trma_period).mean()
        for i in range(len(tr) - 2, 0, -1):
            if tr[i] <= trma[i] and tr[i + 1] > trma[i + 1]:
                return [tr.index[i], tr[i]]
        return []

    def dead_cross(self, trix_period=12, trma_period=20):
        """
        死叉 TRIX down cross TRMA
        :return: [time trix[time]]
        """
        tr = self.__get_trix(timeperiod=trix_period)
        trma = tr.rolling(trma_period, min_periods=trma_period).mean()
        for i in range(len(tr) - 2, 0, -1):
            if tr[i] >= trma[i] and tr[i + 1] < trma[i + 1]:
                return [tr.index[i], tr[i]]
        return []

你可能感兴趣的:(技术指标--TRIX)