Exemple #1
0
    def test_ADXR(self):
        class MyADXR(OperatorADXR):
            def __init__(self, name, **kwargs):
                super(MyADXR, self).__init__(100, name, **kwargs)

        self.env.add_operator('adxr', {
            'operator': MyADXR,
        })
        string = 'adxr(14, high, low, close)'
        gene = self.env.parse_string(string)
        self.assertRaises(IndexError, gene.eval, self.env, self.dates[98],
                          self.dates[-1])
        df = gene.eval(self.env, self.dates[99], self.dates[100])
        ser0, ser1 = df.iloc[0], df.iloc[1]
        h = self.env.get_data_value('high').values
        l = self.env.get_data_value('low').values
        c = self.env.get_data_value('close').values
        res0, res1, res = [], [], []
        for i, val in ser0.iteritems():
            res0.append(
                talib.ADXR(h[:100, i], l[:100, i], c[:100, i], 14)[-1] == val)
        for i, val in ser1.iteritems():
            res1.append(
                talib.ADXR(h[1:100 + 1, i], l[1:100 + 1,
                                              i], c[1:100 + 1,
                                                    i], 14)[-1] == val)
            res.append(
                talib.ADXR(h[:100 + 1, i], l[:100 + 1,
                                             i], c[:100 + 1,
                                                   i], 14)[-1] != val)
        self.assertTrue(all(res0) and all(res1) and any(res))
Exemple #2
0
def generate_feature(data):
    high = data.High.values
    low = data.Low.values
    close = data.Close.values

    feature_df = pd.DataFrame(index=data.index)
    feature_df["ADX"] = ADX = talib.ADX(high, low, close, timeperiod=14)
    feature_df["ADXR"] = ADXR = talib.ADXR(high, low, close, timeperiod=14)
    feature_df["APO"] = APO = talib.APO(close, fastperiod=12, slowperiod=26, matype=0)
    feature_df["AROONOSC"] = AROONOSC = talib.AROONOSC(high, low, timeperiod=14)
    feature_df["CCI"] = CCI = talib.CCI(high, low, close, timeperiod=14)
    feature_df["CMO"] = CMO = talib.CMO(close, timeperiod=14)
    feature_df["DX"] = DX = talib.DX(high, low, close, timeperiod=14)
    feature_df["MINUS_DI"] = MINUS_DI = talib.MINUS_DI(high, low, close, timeperiod=14)
    feature_df["MINUS_DM"] = MINUS_DM = talib.MINUS_DM(high, low, timeperiod=14)
    feature_df["MOM"] = MOM = talib.MOM(close, timeperiod=10)
    feature_df["PLUS_DI"] = PLUS_DI = talib.PLUS_DI(high, low, close, timeperiod=14)
    feature_df["PLUS_DM"] = PLUS_DM = talib.PLUS_DM(high, low, timeperiod=14)
    feature_df["PPO"] = PPO = talib.PPO(close, fastperiod=12, slowperiod=26, matype=0)
    feature_df["ROC"] = ROC = talib.ROC(close, timeperiod=10)
    feature_df["ROCP"] = ROCP = talib.ROCP(close, timeperiod=10)
    feature_df["ROCR100"] = ROCR100 = talib.ROCR100(close, timeperiod=10)
    feature_df["RSI"] = RSI = talib.RSI(close, timeperiod=14)
    feature_df["ULTOSC"] = ULTOSC = talib.ULTOSC(high, low, close, timeperiod1=7, timeperiod2=14, timeperiod3=28)
    feature_df["WILLR"] = WILLR = talib.WILLR(high, low, close, timeperiod=14)
    feature_df = feature_df.fillna(0.0)

    matrix = np.stack((
        ADX, ADXR, APO, AROONOSC, CCI, CMO, DX, MINUS_DI, ROCR100, ROC,
        MINUS_DM, MOM, PLUS_DI, PLUS_DM, PPO, ROCP, WILLR, ULTOSC, RSI))
    matrix = np.nan_to_num(matrix)
    matrix = matrix.transpose()

    return feature_df, matrix
	def momentum(self):
		adx = talib.ADX(self.high,self.low,self.close,self.period)
		adxr = talib.ADXR(self.high,self.low,self.close,self.period)
		apo = talib.APO(self.high,self.low,self.close,self.period)
		aroondown, aroonup = talib.AROON(self.high, self.low, period)
		aroonosc = talib.AROONOSC(self.high,self.low,self.period)
		bop  = talib.BOP(self.opens,self.high,self.low,self.close)
		cci = talib.CCI(self.high,self.low,self.close,self.period)
		cmo = talib.CMO(self.close,self.period)
		dx = talib.DX(self.high,self.low,self.close,self.period)
		macd, macdsignal, macdhist = talib.MACD(self.close, fastperiod=period, slowperiod=period*5, signalperiod=period*2)
		macd1, macdsignal1, macdhist1 = talib.MACDEXT(self.close, fastperiod=12, fastmatype=0, slowperiod=26, slowmatype=0, signalperiod=9, signalmatype=0)
		macd2, macdsignal2, macdhist2 = talib.MACDFIX(self.close, signalperiod=9)
		mfi = talib.MFI(self.high, self.low, self.close, self.volume, timeperiod=14)
		minus_di = talib.MINUS_DI(self.high, self.low, self.close, timeperiod=14)
		minus_dm = talib.MINUS_DM(self.high, self.low, timeperiod=14)
		mom = talib.MOM(self.close, timeperiod=10)
		plus_di = talib.PLUS_DI(self.high, self.low, self.close, timeperiod=14)
		plus_dm = talib.PLUS_DM(self.high, self.low, timeperiod=14)
		ppo  = talib.PPO(self.close, fastperiod=12, slowperiod=26, matype=0)
		roc  = talib.ROC(self.close, timeperiod=10)
		rocp = talib.ROCP(self.close, timeperiod=10)
		rocr = talib.ROCR(self.close, timeperiod=10)
		rocr100 = talib.ROCR100(self.close, timeperiod=10)
		rsi =  talib.RSI(self.close, timeperiod=14)
		slowk, slowd = talib.STOCH(self.high, self.low, self.close, fastk_period=5, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0)
		fastk, fastd = talib.STOCHF(self.high, self.low, self.close, fastk_period=5, fastd_period=3, fastd_matype=0)
		fastk1, fastd1 = talib.STOCHRSI(self.close, timeperiod=14, fastk_period=5, fastd_period=3, fastd_matype=0)
		trix = talib.TRIX(self.close, timeperiod=30)
		ultosc = talib.ULTOSC(self.high, self.low, self.close, timeperiod1=7, timeperiod2=14, timeperiod3=28)
		willr = talib.WILLR(self.high, self.low, self.close, timeperiod=14)
Exemple #4
0
def checkOne(code_str):
    df = base.getOneStockData(code_str)
    df['MINUS_DM_' + str(adx_timeperiod)] = ta.MINUS_DM(
        np.array(df['high']), np.array(df['low']), timeperiod=adx_timeperiod)
    df['PLUS_DM_' + str(adx_timeperiod)] = ta.PLUS_DM(
        np.array(df['high']), np.array(df['low']), timeperiod=adx_timeperiod)
    df['TR_' + str(adx_timeperiod)] = ta.TRANGE(np.array(df['high']),
                                                np.array(df['low']),
                                                np.array(df['close']))
    df['MINUS_DI_' + str(adx_timeperiod)] = ta.MINUS_DI(
        np.array(df['high']),
        np.array(df['low']),
        np.array(df['close']),
        timeperiod=adx_timeperiod)
    df['PLUS_DI_' + str(adx_timeperiod)] = ta.PLUS_DI(
        np.array(df['high']),
        np.array(df['low']),
        np.array(df['close']),
        timeperiod=adx_timeperiod)
    df['ADX_' + str(adx_timeperiod)] = ta.ADX(np.array(df['high']),
                                              np.array(df['low']),
                                              np.array(df['close']),
                                              timeperiod=adx_timeperiod)
    df['ADXR_' + str(adx_timeperiod)] = ta.ADXR(np.array(df['high']),
                                                np.array(df['low']),
                                                np.array(df['close']),
                                                timeperiod=adx_timeperiod)

    df.to_csv(ADX_DIR + code_str + '.csv')
    def technical_indicators_df(self):
        o = self.daily_data['Open'].values
        c = self.daily_data['Close'].values
        h = self.daily_data['High'].values
        l = self.daily_data['Low'].values
        v = self.daily_data['Volume'].astype(float).values
        # define the technical analysis matrix

        ta = pd.DataFrame()
        ta['MA5'] = tb.MA(c, timeperiod=5)
        ta['MA10'] = tb.MA(c, timeperiod=10)
        ta['MA20'] = tb.MA(c, timeperiod=20)
        ta['MA60'] = tb.MA(c, timeperiod=60)
        ta['MA120'] = tb.MA(c, timeperiod=120)
        ta['MA5'] = tb.MA(v, timeperiod=5)
        ta['MA10'] = tb.MA(v, timeperiod=10)
        ta['MA20'] = tb.MA(v, timeperiod=20)
        ta['ADX'] = tb.ADX(h, l, c, timeperiod=14)
        ta['ADXR'] = tb.ADXR(h, l, c, timeperiod=14)
        ta['MACD'] = tb.MACD(c, fastperiod=12, slowperiod=26, signalperiod=9)[0]
        ta['RSI'] = tb.RSI(c, timeperiod=14)
        ta['BBANDS_U'] = tb.BBANDS(c, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0)[0]
        ta['BBANDS_M'] = tb.BBANDS(c, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0)[1]
        ta['BBANDS_L'] = tb.BBANDS(c, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0)[2]
        ta['AD'] = tb.AD(h, l, c, v)
        ta['ATR'] = tb.ATR(h, l, c, timeperiod=14)
        ta['HT_DC'] = tb.HT_DCPERIOD(c)

        self.ta = ta
Exemple #6
0
def get_features(data):
    tech_data = pd.DataFrame(index=data.index);

    for t in periods_list:
        tech_data[f'SMA_{t}'] = talib.SMA(data.close,timeperiod=t)
        tech_data[f'MOM_{t}'] = talib.MOM(data.close, timeperiod=t)
        tech_data[f'RSI_{t}'] = talib.RSI(data.close, timeperiod=t)
        tech_data[f'MA_{t}'] = talib.MA(data.close, timeperiod=t)
        tech_data[f'DX_{t}'] = talib.DX(data.high, data.low, data.close, timeperiod=t)
        tech_data[f'volume_change_{t}'] = data.volume.pct_change(periods=t)
        tech_data[f'volatility_{t}'] = data.close.pct_change(periods=t).std()
        tech_data[f'ADX_{t}'] = talib.ADX(data.high, data.low, data.close, timeperiod=t)
        tech_data[f'ADXR_{t}'] = talib.ADXR(data.high, data.low, data.close, timeperiod=t)
        tech_data[f'AROONOSC_{t}'] = talib.AROONOSC(data.high, data.low, timeperiod=t)
        tech_data[f'ROC_{t}'] = talib.ROC(data.close, timeperiod=t)
        tech_data[f'BIAS_{t}'] = (data['close'] - data['close'].rolling(t, min_periods=1).mean())/ data['close'].rolling(t, min_periods=1).mean()*100
        tech_data[f'BOLL_upper_{t}'], tech_data[f'BOLL_middle_{t}'], tech_data[f'BOLL_lower_{t}'] = talib.BBANDS(
                data.close,
                timeperiod=t,
                nbdevup=2,
                nbdevdn=2,
                matype=0)

    tech_data['SAR'] = talib.SAR(data.high, data.low)
    tech_data['AD'] = talib.AD(data.high, data.low, data.close, data.volume)
    tech_data['OBV'] = talib.OBV(data.close, data.volume)
    tech_data['target'] = data.close.pct_change().shift(-1).apply(lambda x: 1 if x > 0 else -1).fillna(0)
    tech_data['time'] = data.time
    tech_data = tech_data.set_index('time')

    reduce(lambda x, y: cross_over(x, y, tech_data), periods_list)
    
    features = list(set(tech_data.columns) - set(data.columns) - set(['target'])) 
    return tech_data.dropna(), features
Exemple #7
0
 def dmi(self):
     df = self.df
     MINUS_DI = talib.MINUS_DI(df.high, df.low, df.close, timeperiod=14)
     PLUS_DI = talib.PLUS_DI(df.high, df.low, df.close, timeperiod=14)
     ADX = talib.ADX(df.high, df.low, df.close, timeperiod=6)
     ADXR = talib.ADXR(df.high, df.low, df.close, timeperiod=6)
     return PLUS_DI.tolist(), MINUS_DI.tolist(), ADX.tolist(), ADXR.tolist()
Exemple #8
0
def getMomentumIndicators(df):

    high = df['High']
    low = df['Low']
    close = df['Close']
    open = df['Open']
    volume = df['Volume']
    df['ADX'] = ta.ADX(high, low, close, timeperiod=14)
    df['SMA'] = ta.ADXR(high, low, close, timeperiod=14)
    df['APO'] = ta.APO(close, fastperiod=12, slowperiod=26, matype=0)
    df['AROONDOWN'], df['AROOONUP'] = ta.AROON(high, low, timeperiod=14)
    df['AROONOSC'] = ta.AROONOSC(high, low, timeperiod=14)
    df['BOP'] = ta.BOP(open, high, low, close)
    df['CCI'] = ta.CCI(high, low, close, timeperiod=14)
    df['CMO'] = ta.CMO(close, timeperiod=14)
    df['DX'] = ta.DX(high, low, close, timeperiod=14)
    df['MACD'], df['MACDSIGNAL'], df['MACDHIST'] = ta.MACD(close, fastperiod=12, slowperiod=26, signalperiod=9)
    df['MFI'] = ta.MFI(high, low, close, volume, timeperiod=14)
    df['MINUS_DI'] = ta.MINUS_DI(high, low, close, timeperiod=14)
    df['MINUS_DM']= ta.MINUS_DM(high, low, timeperiod=14)
    df['MOM'] = ta.MOM(close, timeperiod=10)
    df['PLUS_DM'] =ta.PLUS_DM(high, low, timeperiod=14)
    df['PPO'] = ta.PPO(close, fastperiod=12, slowperiod=26, matype=0)
    df['ROC'] = ta.ROC(close, timeperiod=10)
    df['ROCP'] = ta.ROCP(close, timeperiod=10)
    df['ROCR'] = ta.ROCR(close, timeperiod=10)
    df['ROCR100'] = ta.ROCR100(close, timeperiod=10)
    df['RSI'] = ta.RSI(close, timeperiod=14)
    df['SLOWK'], df['SLOWD'] = ta.STOCH(high, low, close, fastk_period=5, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0)
    df['FASTK'], df['FASTD'] = ta.STOCHF(high, low, close, fastk_period=5, fastd_period=3, fastd_matype=0)
    df['FASTK2'], df['FASTD2'] = ta.STOCHRSI(close, timeperiod=14, fastk_period=5, fastd_period=3, fastd_matype=0)
    df['TRIX'] = ta.TRIX(close, timeperiod=30)
    df['ULTOSC'] = ta.ULTOSC(high, low, close, timeperiod1=7, timeperiod2=14, timeperiod3=28)
    df['WILLR'] = ta.WILLR(high, low, close, timeperiod=14)
Exemple #9
0
def ADXR(df, n):
    return pd.Series(talib.ADXR(df['high'].values,
                                df['low'].values,
                                df['close'].values,
                                timeperiod=n),
                     index=df.index,
                     name='ADXR_%s' % str(n))
Exemple #10
0
def adxr(
    client,
    symbol,
    timeframe="6m",
    highcol="high",
    lowcol="low",
    closecol="close",
    period=14,
):
    """This will return a dataframe of average directional movement index rating for the given symbol across
    the given timeframe

    Args:
        client (pyEX.Client): Client
        symbol (string): Ticker
        timeframe (string): timeframe to use, for pyEX.chart
        highcol (string): column to use to calculate
        lowcol (string): column to use to calculate
        closecol (string): column to use to calculate
        period (int): period to calculate across

    Returns:
        DataFrame: result
    """
    df = client.chartDF(symbol, timeframe)
    adx = t.ADXR(df[highcol].values, df[lowcol].values, df[closecol].values, period)
    return pd.DataFrame(
        {
            highcol: df[highcol].values,
            lowcol: df[lowcol].values,
            closecol: df[closecol].values,
            "adx": adx,
        }
    )
Exemple #11
0
 def dmi(self, df, index):
     MINUS_DI = talib.MINUS_DI(df.high, df.low, df.close, timeperiod=14)
     #DX = talib.DX(df.high,df.low,df.close,timeperiod=14)
     PLUS_DI = talib.PLUS_DI(df.high, df.low, df.close, timeperiod=14)
     #PLUS_DM = talib.PLUS_DM(df.high,df.low, timeperiod=14)
     ADX = talib.ADX(df.high, df.low, df.close, timeperiod=6)
     ADXR = talib.ADXR(df.high, df.low, df.close, timeperiod=6)
     return MINUS_DI, PLUS_DI, ADX, ADXR
Exemple #12
0
def TA_ADXR(high, low, close, timeperiod=14) -> np.ndarray:
    """
    名称:平均趋向指数的趋向指数
    简介:使用ADXR指标,指标判断ADX趋势。
    ADXR - Average Directional Movement Index Rating
    """
    real = talib.ADXR(high, low, close, timeperiod=timeperiod)
    return np.c_[real]
Exemple #13
0
 def adxr(self, n, array=False):
     """
     ADXR.
     """
     result = talib.ADXR(self.high, self.low, self.close, n)
     if array:
         return result
     return result[-1]
Exemple #14
0
 def adxr(self, n: int, array: bool = False) -> Union[float, np.ndarray]:
     """
     ADXR.
     """
     result = talib.ADXR(self.high, self.low, self.close, n)
     if array:
         return result
     return result[-1]
Exemple #15
0
    def onFifteenBar(self, bar):
        """收到15分钟K线"""
        # 撤销之前发出的尚未成交的委托(包括限价单和停止单)
        for orderID in self.orderList:
            self.cancelOrder(orderID)
        self.orderList = []

        # 保存K线数据
        am = self.am
        am.updateBar(bar)

        if not am.inited:
            return

        # 计算指标数值

        std0 = np.std(am.close[-self.aPeriod - 1:-1])
        std1 = np.std(am.close[-self.aPeriod:])

        if std1 == 0:
            return

        volatility = (std1 - std0) / std1

        if volatility < 0.1:
            volatility = 0.1

        period = int(self.aPeriod * (1 + volatility))

        fast_ma = talib.MA(am.close, period)
        middle_ma = talib.MA(am.close, 2 * period)
        slow_ma = talib.MA(am.close, 3 * period)

        adxr = talib.ADXR(am.high, am.low, am.close, self.aPeriod)
        pdi = talib.PLUS_DI(am.high, am.low, am.close, self.aPeriod)
        mdi = talib.MINUS_DI(am.high, am.low, am.close, self.aPeriod)

        self.fastMa = fast_ma[-1]
        self.middleMa = middle_ma[-1]
        self.slowMa = slow_ma[-1]
        self.adxr = adxr[-1]
        self.pdi = pdi[-1]
        self.mdi = mdi[-1]

        # 判断是否要进行交易
        # 多头
        if self.adxr > 30 and self.pdi > self.mdi and self.fastMa > self.middleMa > self.slowMa:
            self.targetPos = self.fixedSize

        # 空头
        if self.adxr > 30 and self.mdi > self.pdi and self.fastMa < self.middleMa < self.slowMa:
            self.targetPos = -self.fixedSize

        # 同步数据到数据库
        self.saveSyncData()

        # 发出状态更新事件
        self.putEvent()
Exemple #16
0
def applyADXR(nsedailyquoteDF):

    adxrdf = pd.Series(talib.ADXR(nsedailyquoteDF['HIGH'].values,
                                  nsedailyquoteDF['LOW'].values,
                                  nsedailyquoteDF['CLOSE'].values,
                                  timeperiod=14),
                       index=nsedailyquoteDF.TIMESTAMP,
                       name='ADXR')
    return adxrdf
 def ADXR(self, indicatorSpan=14):
     real_data = np.array([self.df.high, self.df.low, self.df.close], dtype='f8')
     adxr = talib.ADXR(real_data[0], real_data[1], real_data[2], timeperiod=indicatorSpan)
     # return go.Scatter(
     #     x=self.df.index,
     #     y=adxr,
     #     name='ADXR'
     # )
     return adxr
    def adxr(self, sym, frequency, period=14):
        if not self.kbars_ready(sym, frequency):
            return []

        highs = self.high(sym, frequency)
        lows = self.low(sym, frequency)
        closes = self.close(sym, frequency)

        return ta.ADXR(highs, lows, closes, timeperiod=period)
Exemple #19
0
 def calculateMOM(dataset):
     timeperiod = 14
     dataset.loc[:,'mom_adx']    = talib.ADX(dataset['high'].values, dataset['low'].values, dataset['close'].values, timeperiod=timeperiod)
     dataset.loc[:,'mom_adxr']   = talib.ADXR(dataset['high'].values, dataset['low'].values, dataset['close'].values, timeperiod=timeperiod)
     dataset.loc[:,'mom_mdi']    = talib.MINUS_DI(dataset['high'].values, dataset['low'].values, dataset['close'].values, timeperiod=timeperiod)
     dataset.loc[:,'mom_mdm']    = talib.MINUS_DM(dataset['high'].values, dataset['low'].values, timeperiod=timeperiod)
     dataset.loc[:,'mom_pdi']    = talib.MINUS_DI(dataset['high'].values, dataset['low'].values, dataset['close'].values, timeperiod=timeperiod)
     dataset.loc[:,'mom_pdm']    = talib.MINUS_DM(dataset['high'].values, dataset['low'].values, timeperiod=timeperiod)
     return dataset
Exemple #20
0
    def technical_indicators_df(self, daily_data):
        """
        Assemble a dataframe of technical indicator series for a single stock
        """
        o = daily_data['Open'].values
        c = daily_data['Close'].values
        h = daily_data['High'].values
        l = daily_data['Low'].values
        v = daily_data['Volume'].astype(float).values
        # define the technical analysis matrix

        # Most data series are normalized by their series' mean
        ta = pd.DataFrame()
        ta['MA5'] = tb.MA(c, timeperiod=5) / tb.MA(c, timeperiod=5).mean()
        ta['MA10'] = tb.MA(c, timeperiod=10) / tb.MA(c, timeperiod=10).mean()
        ta['MA20'] = tb.MA(c, timeperiod=20) / tb.MA(c, timeperiod=20).mean()
        ta['MA60'] = tb.MA(c, timeperiod=60) / tb.MA(c, timeperiod=60).mean()
        ta['MA120'] = tb.MA(c, timeperiod=120) / tb.MA(c,
                                                       timeperiod=120).mean()
        ta['MA5'] = tb.MA(v, timeperiod=5) / tb.MA(v, timeperiod=5).mean()
        ta['MA10'] = tb.MA(v, timeperiod=10) / tb.MA(v, timeperiod=10).mean()
        ta['MA20'] = tb.MA(v, timeperiod=20) / tb.MA(v, timeperiod=20).mean()
        ta['ADX'] = tb.ADX(h, l, c, timeperiod=14) / tb.ADX(
            h, l, c, timeperiod=14).mean()
        ta['ADXR'] = tb.ADXR(h, l, c, timeperiod=14) / tb.ADXR(
            h, l, c, timeperiod=14).mean()
        ta['MACD'] = tb.MACD(c, fastperiod=12, slowperiod=26, signalperiod=9)[0] / \
                     tb.MACD(c, fastperiod=12, slowperiod=26, signalperiod=9)[0].mean()
        ta['RSI'] = tb.RSI(c, timeperiod=14) / tb.RSI(c, timeperiod=14).mean()
        ta['BBANDS_U'] = tb.BBANDS(c, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0)[0] / \
                         tb.BBANDS(c, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0)[0].mean()
        ta['BBANDS_M'] = tb.BBANDS(c, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0)[1] / \
                         tb.BBANDS(c, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0)[1].mean()
        ta['BBANDS_L'] = tb.BBANDS(c, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0)[2] / \
                         tb.BBANDS(c, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0)[2].mean()
        ta['AD'] = tb.AD(h, l, c, v) / tb.AD(h, l, c, v).mean()
        ta['ATR'] = tb.ATR(h, l, c, timeperiod=14) / tb.ATR(
            h, l, c, timeperiod=14).mean()
        ta['HT_DC'] = tb.HT_DCPERIOD(c) / tb.HT_DCPERIOD(c).mean()
        ta["High/Open"] = h / o
        ta["Low/Open"] = l / o
        ta["Close/Open"] = c / o

        self.ta = ta
Exemple #21
0
def ADXR(high, low, close, timeperiod=14):
    ''' Average Directional Movement Index Rating 平均趋向指数的趋向指数

    分组: Momentum Indicator 动量指标

    简介: 使用ADXR指标,指标判断ADX趋势。

    real = ADXR(high, low, close, timeperiod=14)
    '''
    return talib.ADXR(high, low, close, timeperiod)
def generate_feature(data):
    high = data.High.values
    low = data.Low.values
    close = data.Close.values

    # feature_df = pd.DataFrame(index=data.index)
    feature_df = data.copy()
    feature_df["ADX"] = ADX = talib.ADX(high, low, close, timeperiod=14)
    feature_df["ADXR"] = ADXR = talib.ADXR(high, low, close, timeperiod=14)
    feature_df["APO"] = APO = talib.APO(close,
                                        fastperiod=12,
                                        slowperiod=26,
                                        matype=0)
    feature_df["AROONOSC"] = AROONOSC = talib.AROONOSC(high,
                                                       low,
                                                       timeperiod=14)
    feature_df["CCI"] = CCI = talib.CCI(high, low, close, timeperiod=14)
    feature_df["CMO"] = CMO = talib.CMO(close, timeperiod=14)
    feature_df["DX"] = DX = talib.DX(high, low, close, timeperiod=14)
    feature_df["MINUS_DI"] = MINUS_DI = talib.MINUS_DI(high,
                                                       low,
                                                       close,
                                                       timeperiod=14)
    feature_df["MINUS_DM"] = MINUS_DM = talib.MINUS_DM(high,
                                                       low,
                                                       timeperiod=14)
    feature_df["MOM"] = MOM = talib.MOM(close, timeperiod=10)
    feature_df["PLUS_DI"] = PLUS_DI = talib.PLUS_DI(high,
                                                    low,
                                                    close,
                                                    timeperiod=14)
    feature_df["PLUS_DM"] = PLUS_DM = talib.PLUS_DM(high, low, timeperiod=14)
    feature_df["PPO"] = PPO = talib.PPO(close,
                                        fastperiod=12,
                                        slowperiod=26,
                                        matype=0)
    feature_df["ROC"] = ROC = talib.ROC(close, timeperiod=10)
    feature_df["ROCP"] = ROCP = talib.ROCP(close, timeperiod=10)
    feature_df["ROCR100"] = ROCR100 = talib.ROCR100(close, timeperiod=10)
    feature_df["RSI"] = RSI = talib.RSI(close, timeperiod=14)
    feature_df["ULTOSC"] = ULTOSC = talib.ULTOSC(high,
                                                 low,
                                                 close,
                                                 timeperiod1=7,
                                                 timeperiod2=14,
                                                 timeperiod3=28)
    feature_df["WILLR"] = WILLR = talib.WILLR(high, low, close, timeperiod=14)
    feature_df = feature_df.fillna(0.0)

    # Exclude columns you don't want
    feature_df = feature_df[feature_df.columns[
        ~feature_df.columns.isin(['Open', 'High', 'Low', 'Close'])]]
    matrix = feature_df.values

    return feature_df, matrix
Exemple #23
0
 def _get_indicators(security, open_name, close_name, high_name, low_name,
                     volume_name):
     """
     expand the features of the data through technical analysis across 26 different signals
     :param security: data which features are going to be expanded
     :param open_name: open price column name
     :param close_name: close price column name
     :param high_name: high price column name
     :param low_name: low price column name
     :param volume_name: traded volumn column name
     :return: expanded and extracted data
     """
     open_price = security[open_name].values
     close_price = security[close_name].values
     low_price = security[low_name].values
     high_price = security[high_name].values
     volume = security[volume_name].values if volume_name else None
     security['MOM'] = talib.MOM(close_price)
     security['HT_DCPERIOD'] = talib.HT_DCPERIOD(close_price)
     security['HT_DCPHASE'] = talib.HT_DCPHASE(close_price)
     security['SINE'], security['LEADSINE'] = talib.HT_SINE(close_price)
     security['INPHASE'], security['QUADRATURE'] = talib.HT_PHASOR(
         close_price)
     security['ADXR'] = talib.ADXR(high_price, low_price, close_price)
     security['APO'] = talib.APO(close_price)
     security['AROON_UP'], _ = talib.AROON(high_price, low_price)
     security['CCI'] = talib.CCI(high_price, low_price, close_price)
     security['PLUS_DI'] = talib.PLUS_DI(high_price, low_price, close_price)
     security['PPO'] = talib.PPO(close_price)
     security['MACD'], security['MACD_SIG'], security[
         'MACD_HIST'] = talib.MACD(close_price)
     security['CMO'] = talib.CMO(close_price)
     security['ROCP'] = talib.ROCP(close_price)
     security['FASTK'], security['FASTD'] = talib.STOCHF(
         high_price, low_price, close_price)
     security['TRIX'] = talib.TRIX(close_price)
     security['ULTOSC'] = talib.ULTOSC(high_price, low_price, close_price)
     security['WILLR'] = talib.WILLR(high_price, low_price, close_price)
     security['NATR'] = talib.NATR(high_price, low_price, close_price)
     security['RSI'] = talib.RSI(close_price)
     security['EMA'] = talib.EMA(close_price)
     security['SAREXT'] = talib.SAREXT(high_price, low_price)
     # security['TEMA'] = talib.EMA(close_price)
     security['RR'] = security[close_name] / security[close_name].shift(
         1).fillna(1)
     security['LOG_RR'] = np.log(security['RR'])
     if volume_name:
         security['MFI'] = talib.MFI(high_price, low_price, close_price,
                                     volume)
         # security['AD'] = talib.AD(high_price, low_price, close_price, volume)
         # security['OBV'] = talib.OBV(close_price, volume)
         security[volume_name] = np.log(security[volume_name])
     security.drop([open_name, close_name, high_name, low_name], axis=1)
     security = security.dropna().astype(np.float32)
     return security
Exemple #24
0
 def add_adx(self, n: int):
     if n <= len(self.closes):
         adx = nan_to_zero(
             talib.ADX(np.array(self.highs), np.array(self.lows),
                       np.array(self.closes), n)).tolist()
         adxr = nan_to_zero(
             talib.ADXR(np.array(self.highs), np.array(self.lows),
                        np.array(self.closes), n)).tolist()
         self.adx = Adx(n, adx, adxr)
         return True
     return False
Exemple #25
0
 def get_momentum_studies(open, low, high, close, volume, df):
     # Momentum studies
     # https://mrjbq7.github.io/ta-lib/func_groups/momentum_indicators.html
     df['MACD'], df['MACD_SIGN'], df['MACD_HIST'] = talib.MACD(
         close, fastperiod=12, slowperiod=26, signalperiod=9)
     df['STOCH-SLOW-K'], df['STOCH-SLOW-D'] = talib.STOCH(high,
                                                          low,
                                                          close,
                                                          fastk_period=5,
                                                          slowk_period=3,
                                                          slowk_matype=0,
                                                          slowd_period=3,
                                                          slowd_matype=0)
     df['STOCH-FAST-K'], df['STOCH-FAST-D'] = talib.STOCHF(high,
                                                           low,
                                                           close,
                                                           fastk_period=5,
                                                           fastd_period=3,
                                                           fastd_matype=0)
     df['STOCH-RSI-K'], df['STOCH-RSI-D'] = talib.STOCHRSI(close,
                                                           timeperiod=14,
                                                           fastk_period=5,
                                                           fastd_period=3,
                                                           fastd_matype=0)
     df['AROON-DOWN'], df['AROON-UP'] = talib.AROON(high,
                                                    low,
                                                    timeperiod=14)
     df["MINUS_DI"] = talib.MINUS_DI(high, low, close, timeperiod=14)
     df["MINUS_DM"] = talib.MINUS_DM(high, low, timeperiod=14)
     df["PLUS_DI"] = talib.PLUS_DI(high, low, close, timeperiod=14)
     df["PLUS_DM"] = talib.PLUS_DM(high, low, timeperiod=14)
     df["MOM"] = talib.MOM(close, timeperiod=10)
     df["MFI"] = talib.MFI(high, low, close, volume, timeperiod=14)
     df["ADX"] = talib.ADX(high, low, close, timeperiod=14)
     df["ADXR"] = talib.ADXR(high, low, close, timeperiod=14)
     df["APO"] = talib.APO(close, fastperiod=12, slowperiod=26, matype=0)
     df["AROONOSC"] = talib.AROONOSC(high, low, timeperiod=14)
     df["BOP"] = talib.BOP(open, high, low, close)
     df["CCI"] = talib.CCI(high, low, close, timeperiod=14)
     df["CMO"] = talib.CMO(close, timeperiod=14)
     df["DX"] = talib.DX(high, low, close, timeperiod=14)
     df["PPO"] = talib.PPO(close, fastperiod=12, slowperiod=26, matype=0)
     df["ROC"] = talib.ROC(close, timeperiod=10)
     df["RSI"] = talib.RSI(close, timeperiod=14)
     df["TRIX"] = talib.TRIX(close, timeperiod=30)
     df["ULT"] = talib.ULTOSC(high,
                              low,
                              close,
                              timeperiod1=7,
                              timeperiod2=14,
                              timeperiod3=28)
     df["WILLR"] = talib.WILLR(high, low, close, timeperiod=14)
Exemple #26
0
def ADXR(quote):
    '''
    Calculate the ADXR and its trend (trend calculated by use SMA)
    '''
    high = numpy.array(quote.High)
    low = numpy.array(quote.Low)
    close = numpy.array(quote.Close)
    #ADXR is (adx + adx_previous)/2, but not in talib, where ADXR is unknown.
    #https://www.linnsoft.com/techind/adxr-avg-directional-movement-rating
    adxr = helper.RemoveNaN(ta.ADXR(high, low, close))
    #adxr_trend = helper.RemoveNaN(ta.SMA(adxr))
    #print("ADXR is ", adxr)
    #print("ADXR Trend is ", adxr_trend)
    return adxr
Exemple #27
0
    def dmi_ana(self, df, index):

        MINUS_DI = talib.MINUS_DI(df.high, df.low, df.close, timeperiod=14)
        #DX = talib.DX(df.high,df.low,df.close,timeperiod=14)
        PLUS_DI = talib.PLUS_DI(df.high, df.low, df.close, timeperiod=14)
        #PLUS_DM = talib.PLUS_DM(df.high,df.low, timeperiod=14)
        ADX = talib.ADX(df.high, df.low, df.close, timeperiod=6)
        ADXR = talib.ADXR(df.high, df.low, df.close, timeperiod=6)
        adxlist = ADX.values.tolist()
        #plt.plot(ADX.values.tolist(),'g',ADXR.values.tolist(),'b',PLUS_DI.values.tolist(),'y',MINUS_DI.values.tolist(),'k'
        #plt.show()
        #if adxlist[index]<adxlist[index-2]:return 0
        if adxlist[index] < 25: return 0
        return 1
Exemple #28
0
    def default_idts(self):
        """
        Assemble a dataframe of technical indicator series for a single stock
        """
        o = self.data['open'].values
        c = self.data['close'].values
        h = self.data['high'].values
        l = self.data['low'].values
        v = self.data['volume'].astype(float).values
        # define the technical analysis matrix

        # Most data series are normalized by their series' mean
        ta = pd.DataFrame()
        ta['MA5'] = tb.MA(c, timeperiod=5)
        ta['MA10'] = tb.MA(c, timeperiod=10)
        ta['MA20'] = tb.MA(c, timeperiod=20)
        ta['MA60'] = tb.MA(c, timeperiod=60)
        ta['MA120'] = tb.MA(c, timeperiod=120)
        ta['MA5'] = tb.MA(v, timeperiod=5)
        ta['MA10'] = tb.MA(v, timeperiod=10)
        ta['MA20'] = tb.MA(v, timeperiod=20)
        ta['ADX'] = tb.ADX(h, l, c, timeperiod=14)
        ta['ADXR'] = tb.ADXR(h, l, c, timeperiod=14)
        ta['MACD'] = tb.MACD(c, fastperiod=12, slowperiod=26,
                             signalperiod=9)[0]
        ta['RSI'] = tb.RSI(c, timeperiod=14)
        ta['BBANDS_U'] = tb.BBANDS(c,
                                   timeperiod=5,
                                   nbdevup=2,
                                   nbdevdn=2,
                                   matype=0)[0]
        ta['BBANDS_M'] = tb.BBANDS(c,
                                   timeperiod=5,
                                   nbdevup=2,
                                   nbdevdn=2,
                                   matype=0)[1]
        ta['BBANDS_L'] = tb.BBANDS(c,
                                   timeperiod=5,
                                   nbdevup=2,
                                   nbdevdn=2,
                                   matype=0)[2]
        ta['AD'] = tb.AD(h, l, c, v)
        ta['ATR'] = tb.ATR(h, l, c, timeperiod=14)
        ta['HT_DC'] = tb.HT_DCPERIOD(c)
        ta["High/Open"] = h / o
        ta["Low/Open"] = l / o
        ta["Close/Open"] = c / o
        ta.index = self.data.index
        return ta
def add_ADXR(self, timeperiod=14, type='line', color='secondary', **kwargs):
    """Average Directional Movement Index Rating."""

    if not (self.has_high and self.has_low and self.has_close):
        raise Exception()

    utils.kwargs_check(kwargs, VALID_TA_KWARGS)
    if 'kind' in kwargs:
        type = kwargs['kind']

    name = 'ADXR({})'.format(str(timeperiod))
    self.sec[name] = dict(type=type, color=color)
    self.ind[name] = talib.ADXR(self.df[self.hi].values,
                                self.df[self.lo].values,
                                self.df[self.cl].values, timeperiod)
Exemple #30
0
def getDMI( data ):
   high = data["high"]
   low = data["low"]
   close = data["close"]

   DI1 = talib.PLUS_DI(high, low, close, timeperiod=14)
   DI2 = talib.MINUS_DI(high, low, close, timeperiod=14)
   ADX = talib.ADX(high, low, close, timeperiod=14)
   ADXR =talib.ADXR(high, low, close, timeperiod=14)

   data['DMI_DI+'] = DI1
   data['DMI_DI-'] = DI2
   data['ADX'] = ADX
   data['ADXR'] = ADXR
   return data