Ejemplo n.º 1
0
def add_indicators(df):
    high = df["HA_High"].values
    close = df["HA_Close"].values
    low = df["HA_Low"].values
    _open = df["HA_Open"].values
    volume = df["volume"].values.astype('uint32')

    df["APO"] = talib.APO(close, fastperiod=9, slowperiod=21, matype=0)
    df["APO"] = talib.APO(close, fastperiod=9, slowperiod=21, matype=0)
    df["aroondown"], df["aroonup"] = talib.AROON(high, low, timeperiod=14)
    df["BOP"] = talib.BOP(_open, high, low, close)
    df["CCI"] = talib.CCI(high, low, close, timeperiod=10)
    df["DX"] = talib.DX(high, low, close, timeperiod=10)
    df["MOM"] = talib.MOM(close, timeperiod=10)
    df["slowk"], df["slowd"] = talib.STOCH(high,
                                           low,
                                           close,
                                           fastk_period=5,
                                           slowk_period=3,
                                           slowk_matype=0,
                                           slowd_period=3,
                                           slowd_matype=0)
    df["OBV"] = talib.OBV(close, np.asarray(volume, dtype='float'))
    df["ADOSC"] = talib.ADOSC(high,
                              low,
                              close,
                              np.asarray(volume, dtype='float'),
                              fastperiod=3,
                              slowperiod=10)
    df["upperband"], df["middleband"], df["lowerband"] = talib.BBANDS(
        close, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0)

    return df
Ejemplo n.º 2
0
def trade(context, data):
    for stock in context.stocks1:
        if data.can_trade(stock) and not data.is_stale(stock):
            prices = data.history(stock, 'price', 50, '1d')
            try:
                apo = talib.APO(prices, fastperiod=12, slowperiod=26, matype=context.matype)[-1]
            except:
                context.stocks1.remove(stock)
                log.info(stock)
                continue

            position = context.portfolio.positions[stock].amount
            
            if apo > 0 and position == 0:
                order_target_percent(stock, percent)

            elif apo < 0 and position > 0:
                order_target_percent(stock, 0)

    for stock in context.stocks2:
        if data.can_trade(stock) and not data.is_stale(stock):
            prices = data.history(stock, 'price', 50, '1d')
            try:
                apo = talib.APO(prices, fastperiod=12, slowperiod=26, matype=context.matype)[-1]
            except:
                context.stocks2.remove(stock)
                log.info(stock)
                continue

            position = context.portfolio.positions[stock].amount
            
            if apo > 0 and position == 0:
                order_target_percent(stock, percent)

            elif apo < 0 and position > 0:
                order_target_percent(stock, 0)

    for stock in context.stocks3:
        if data.can_trade(stock) and not data.is_stale(stock):
            prices = data.history(stock, 'price', 50, '1d')
            try:
                apo = talib.APO(prices, fastperiod=12, slowperiod=26, matype=context.matype)[-1]
            except:
                context.stocks3.remove(stock)
                log.info(stock)
                continue

            position = context.portfolio.positions[stock].amount
            
            if apo > 0 and position == 0:
                order_target_percent(stock, percent)

            elif apo < 0 and position > 0:
                order_target_percent(stock, 0)
        
    record(leverage=context.account.leverage)
Ejemplo n.º 3
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)
Ejemplo n.º 4
0
 def test_apo(self):
     """
     Test Absolute Price Oscillator (APO).
     """
     q = qufilab.apo(self.close, 200, 100, "sma")
     t = talib.APO(self.close, 200, 100, 0)
     np.testing.assert_allclose(q, t, rtol=self.tolerance)
Ejemplo n.º 5
0
def extract_features(data):
    high = data['High']
    low = data['Low']
    close = data['Close']
    volume = data['Volume']
    open_ = data['Open']
    
    data['ADX'] = ta.ADX(high, low, close, timeperiod=19)
    data['CCI'] = ta.CCI(high, low, close, timeperiod=19)  
    data['CMO'] = ta.CMO(close, timeperiod=14)
    data['MACD'], X, Y = ta.MACD(close, fastperiod=10, slowperiod=30, signalperiod=9)
    data['MFI'] = ta.MFI(high, low, close, volume, timeperiod=19)
    data['MOM'] = ta.MOM(close, timeperiod=9)
    data['ROCR'] = ta.ROCR(close, timeperiod=12) 
    data['RSI'] = ta.RSI(close, timeperiod=19)  
    data['STOCHSLOWK'], data['STOCHSLOWD'] = ta.STOCH(high, low, close, fastk_period=5, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0)
    data['TRIX'] = ta.TRIX(close, timeperiod=30)
    data['WILLR'] = ta.WILLR(high, low, close, timeperiod=14)
    data['OBV'] = ta.OBV(close, volume)
    data['TSF'] = ta.TSF(close, timeperiod=14)
    data['NATR'] = ta.NATR(high, low, close)#, timeperiod=14)
    data['ULTOSC'] = ta.ULTOSC(high, low, close)
    data['AROONOSC'] = ta.AROONOSC(high, low, timeperiod=14)
    data['BOP'] = ta.BOP(open_, high, low, close)
    data['LINEARREG'] = ta.LINEARREG(close)
    data['AP0'] = ta.APO(close, fastperiod=9, slowperiod=23, matype=1)
    data['TEMA'] = ta.TRIMA(close, 29)
    
    return data
Ejemplo n.º 6
0
	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)
Ejemplo n.º 7
0
def apo(candles: np.ndarray,
        fastperiod=12,
        slowperiod=26,
        matype=0,
        sequential=False) -> Union[float, np.ndarray]:
    """
    APO - Absolute Price Oscillator

    :param candles: np.ndarray
    :param fastperiod: int - default: 12
    :param slowperiod: int - default: 26
    :param matype: int - default: 0
    :param sequential: bool - default=False

    :return: float | np.ndarray
    """
    if not sequential and len(candles) > 240:
        candles = candles[-240:]

    res = talib.APO(candles[:, 2],
                    fastperiod=fastperiod,
                    slowperiod=slowperiod,
                    matype=matype)

    return res if sequential else res[-1]
Ejemplo n.º 8
0
def apo(candles: np.ndarray,
        fast_period: int = 12,
        slow_period: int = 26,
        matype: int = 0,
        source_type: str = "close",
        sequential: bool = False) -> Union[float, np.ndarray]:
    """
    APO - Absolute Price Oscillator

    :param candles: np.ndarray
    :param fast_period: int - default: 12
    :param slow_period: int - default: 26
    :param matype: int - default: 0
    :param source_type: str - default: "close"
    :param sequential: bool - default=False

    :return: float | np.ndarray
    """
    candles = slice_candles(candles, sequential)

    source = get_candle_source(candles, source_type=source_type)

    res = talib.APO(source,
                    fastperiod=fast_period,
                    slowperiod=slow_period,
                    matype=matype)

    return res if sequential else res[-1]
Ejemplo n.º 9
0
 def calculations(self):
     '''calculations'''
     self.df['rsi'] = ta.RSI(self.df['close'], timeperiod=5)
     self.df['apo'] = ta.APO(self.df['close'],
                             fastperiod=10,
                             slowperiod=5,
                             matype=0)
     self.df['upperband'], self.df['middleband'], self.df[
         'lowerband'] = ta.BBANDS(self.df['close'],
                                  timeperiod=5,
                                  nbdevup=2,
                                  nbdevdn=2,
                                  matype=0)
     self.df['ema'] = ta.EMA(self.df['close'], timeperiod=5)
     self.df['ma'] = ta.MA(self.df['close'], timeperiod=5, matype=0)
     self.df['sma'] = ta.MA(self.df['close'], timeperiod=5)
     self.df['t3'] = ta.T3(self.df['close'], timeperiod=5, vfactor=0)
     self.df['wma'] = ta.WMA(self.df['close'], timeperiod=5)
     self.df['aroonosc'] = ta.AROONOSC(self.df['high'],
                                       self.df['low'],
                                       timeperiod=5)
     self.df['cci'] = ta.CCI(self.df['high'],
                             self.df['low'],
                             self.df['close'],
                             timeperiod=5)
     self.df['cmo'] = ta.CMO(self.df['close'], timeperiod=14)
     self.df['macd'], self.df['macdsignal'], self.df[
         'macdhist'] = ta.MACDEXT(self.df['close'],
                                  fastperiod=12,
                                  fastmatype=0,
                                  slowperiod=26,
                                  slowmatype=0,
                                  signalperiod=9,
                                  signalmatype=0)
     self.df['slowk'], self.df['slowd'] = ta.STOCH(self.df['high'],
                                                   self.df['low'],
                                                   self.df['close'],
                                                   fastk_period=5,
                                                   slowk_period=3,
                                                   slowk_matype=0,
                                                   slowd_period=3,
                                                   slowd_matype=0)
     self.df['fastk'], self.df['fastd'] = ta.STOCHRSI(self.df['close'],
                                                      timeperiod=5,
                                                      fastk_period=5,
                                                      fastd_period=3,
                                                      fastd_matype=0)
     self.df['ultosc'] = ta.ULTOSC(self.df['high'],
                                   self.df['low'],
                                   self.df['close'],
                                   timeperiod1=7,
                                   timeperiod2=14,
                                   timeperiod3=28)
     self.df['adosc'] = ta.ADOSC(self.df['high'],
                                 self.df['low'],
                                 self.df['close'],
                                 self.df['volume'],
                                 fastperiod=3,
                                 slowperiod=10)
     return self.df
Ejemplo n.º 10
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
Ejemplo n.º 11
0
def apo(close_ts, fastperiod=12, slowperiod=26, matype=0):
    import talib
    close_np = close_ts.cpu().detach().numpy()
    close_df = pd.DataFrame(close_np)
    apo = close_df.apply(lambda x: talib.APO(x, fastperiod=12, slowperiod=26, matype=0))
    
    apo_ts = torch.tensor(apo.values, dtype=close_ts.dtype, device=close_ts.device)
    return apo_ts
Ejemplo n.º 12
0
 def apo(self, n, array=False):
     """
     APO.
     """
     result = talib.APO(self.close, n)
     if array:
         return result
     return result[-1]
Ejemplo n.º 13
0
 def apo(self, n: int, array: bool = False) -> Union[float, np.ndarray]:
     """
     APO.
     """
     result = talib.APO(self.close, n)
     if array:
         return result
     return result[-1]
Ejemplo n.º 14
0
    def APO(close, fastperiod=12, slowperiod=26, matype=0):
        # Absolute Price Oscillator
        param = {
            'fastperiod': fastperiod,
            'slowperiod': slowperiod,
            'matype': matype,
        }

        return talib.APO(close, **param)
Ejemplo n.º 15
0
def apo(df, n=5, m=10, normalize=False, col='Close'):
    c = df[col].as_matrix()
    _apo = pd.Series(ta.APO(c, fastperiod=n, slowperiod=m),
                     index=df.index,
                     name="APO_" + str(n) + "_" + str(m))
    if normalize:
        _apo = z_score(_apo)

    return _apo
Ejemplo n.º 16
0
 def APO(self, fastPeriod=12, slowPeriod=26, matype=0):
     real_data = np.array([self.df.close], dtype='f8')
     apo = talib.APO(real_data[0], fastperiod=fastPeriod, slowperiod=slowPeriod, matype=matype)
     # return go.Scatter(
     #     x=self.df.index,
     #     y=apo,
     #     name='APO'
     # )
     return apo
Ejemplo n.º 17
0
def applyAPO(nsedailyquoteDF):

    apodf = pd.Series(talib.APO(nsedailyquoteDF['CLOSE'].values,
                                fastperiod=12,
                                slowperiod=26,
                                matype=0),
                      index=nsedailyquoteDF.TIMESTAMP,
                      name='APO')
    return apodf
Ejemplo n.º 18
0
 def NMA_factor(self, df, fastperiod=12, slowperiod=26, matype=0):
     """
     the custom function that compute normalized apo
     the equation is (fast ma - slow ma)/slowma
     """
     numerator = talib.APO(df.loc[:, self.map_dict['default']].values,
                           fastperiod, slowperiod, matype)
     denominator = talib.MA(df.loc[:, self.map_dict['default']].values,
                            slowperiod, matype)
     return numerator / denominator
Ejemplo n.º 19
0
def APO(close, fastperiod=12, slowperiod=26, matype=0):
    '''  Absolute Price Oscillator

    分组: Momentum Indicator 动量指标

    简介:

    real = APO(close, fastperiod=12, slowperiod=26, matype=0)
    '''
    return talib.APO(close, fastperiod, slowperiod, matype)
Ejemplo n.º 20
0
def apotrade(context, data):
    for stock in context.stocks:
        prices = data.history(stock, 'price', 50, '1d')
        apo = talib.APO(prices, fastperiod=12, slowperiod=26, matype=0)[-1]
        position = context.portfolio.positions[stock].amount
        if apo > 0 and position == 0:
            order_target_percent(stock, pct)

        elif apo < 0 and position > 0:
            order_target_percent(stock, 0)
Ejemplo n.º 21
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 = 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
Ejemplo n.º 22
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
Ejemplo n.º 23
0
 def apo(self,
         fast_period: int,
         slow_period: int,
         matype: int = 0,
         array: bool = False) -> Union[float, np.ndarray]:
     """
     APO.
     """
     result = talib.APO(self.close, fast_period, slow_period, matype)
     if array:
         return result
     return result[-1]
Ejemplo n.º 24
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)
Ejemplo n.º 25
0
    def test_apo(self):
        result = pandas_ta.apo(self.close)
        self.assertIsInstance(result, Series)
        self.assertEqual(result.name, 'APO_12_26')

        try:
            expected = tal.APO(self.close, 12, 26)
            pdt.assert_series_equal(result, expected, check_names=False)
        except AssertionError as ae:
            try:
                corr = pandas_ta.utils.df_error_analysis(result, expected, col=CORRELATION)
                self.assertGreater(corr, CORRELATION_THRESHOLD)
            except Exception as ex:
                error_analysis(result, CORRELATION, ex)
Ejemplo n.º 26
0
def generate_tech_data_default(stock,
                               open_name,
                               close_name,
                               high_name,
                               low_name,
                               volume_name='vol'):
    open_price = stock[open_name].values
    close_price = stock[close_name].values
    low_price = stock[low_name].values
    high_price = stock[high_name].values
    volume = stock[volume_name].values
    data = stock.copy()
    data['MOM'] = talib.MOM(close_price)
    data['HT_DCPERIOD'] = talib.HT_DCPERIOD(close_price)
    data['HT_DCPHASE'] = talib.HT_DCPHASE(close_price)
    data['sine'], data['leadsine'] = talib.HT_SINE(close_price)
    data['inphase'], data['quadrature'] = talib.HT_PHASOR(close_price)
    data['ADXR'] = talib.ADXR(high_price, low_price, close_price)
    data['APO'] = talib.APO(close_price)
    data['AROON_UP'], _ = talib.AROON(high_price, low_price)
    data['CCI'] = talib.CCI(high_price, low_price, close_price)
    data['PLUS_DI'] = talib.PLUS_DI(high_price, low_price, close_price)
    data['PPO'] = talib.PPO(close_price)
    data['macd'], data['macd_sig'], data['macd_hist'] = talib.MACD(close_price)
    data['CMO'] = talib.CMO(close_price)
    data['ROCP'] = talib.ROCP(close_price)
    data['fastk'], data['fastd'] = talib.STOCHF(high_price, low_price,
                                                close_price)
    data['TRIX'] = talib.TRIX(close_price)
    data['ULTOSC'] = talib.ULTOSC(high_price, low_price, close_price)
    data['WILLR'] = talib.WILLR(high_price, low_price, close_price)
    data['NATR'] = talib.NATR(high_price, low_price, close_price)
    data['MFI'] = talib.MFI(high_price, low_price, close_price, volume)
    data['RSI'] = talib.RSI(close_price)
    data['AD'] = talib.AD(high_price, low_price, close_price, volume)
    data['OBV'] = talib.OBV(close_price, volume)
    data['EMA'] = talib.EMA(close_price)
    data['SAREXT'] = talib.SAREXT(high_price, low_price)
    data['TEMA'] = talib.EMA(close_price)
    #data = data.drop([open_name, close_name, high_name, low_name, volume_name, 'amount', 'count'], axis=1)
    data = drop_columns(data, [
        open_name, close_name, high_name, low_name, volume_name, 'amount',
        'count'
    ])
    data = data.dropna().astype(np.float32)
    return data
Ejemplo n.º 27
0
def get_additional_factors(open, high, low, close, volume):

    # Overlap Studies Functions
    mat = get_all_factors(open, high, low, close, volume)

    mat = np.column_stack((mat, talib.HT_TRENDLINE(close)))  ## close
    mat = np.column_stack((mat, talib.KAMA(close, timeperiod=30)))  ##close

    #Momentum Indicator Functions
    mat = np.column_stack((mat, talib.ADX(high, low, close, timeperiod=14)))
    mat = np.column_stack((mat, talib.ADXR(high, low, close, timeperiod=14)))
    mat = np.column_stack(
        (mat, talib.APO(close, fastperiod=12, slowperiod=26, matype=0)))
    mat = np.column_stack((mat, talib.AROONOSC(high, low, timeperiod=14)))
    mat = np.column_stack((mat, talib.BOP(open, high, low, close)))
    mat = np.column_stack((mat, talib.MOM(close, timeperiod=10)))

    #Volume Indicator Functions
    mat = np.column_stack((mat, talib.AD(high, low, close, volume)))
    mat = np.column_stack(
        (mat, talib.ADOSC(high,
                          low,
                          close,
                          volume,
                          fastperiod=3,
                          slowperiod=10)))
    mat = np.column_stack((mat, talib.OBV(close, volume)))

    #Volatility Indicator Functions
    mat = np.column_stack((mat, talib.NATR(high, low, close, timeperiod=14)))
    mat = np.column_stack((mat, talib.TRANGE(high, low, close)))

    #Price Transform Functions
    mat = np.column_stack((mat, talib.AVGPRICE(open, high, low, close)))
    mat = np.column_stack((mat, talib.MEDPRICE(high, low)))
    mat = np.column_stack((mat, talib.TYPPRICE(high, low, close)))
    mat = np.column_stack((mat, talib.WCLPRICE(high, low, close)))

    #Cycle Indicator Functions
    mat = np.column_stack((mat, talib.HT_DCPERIOD(close)))
    mat = np.column_stack((mat, talib.HT_DCPHASE(close)))
    mat = np.column_stack((mat, talib.HT_TRENDMODE(close)))

    # 20

    return mat
Ejemplo n.º 28
0
def generateIndicatorAPO(dictDataSpec):
    """
    https://www.investopedia.com/articles/technical/02/041002.asp
    """
    df = dictDataSpec['df']
    sReal = pd.Series(talib.APO(df.Close.values, 12, 26), index=df.index)
    sIndicator = sReal.apply(lambda x: np.nan)
    print sReal.describe()

    ADXThresholdUpper = 50
    ADXThresholdLower = 50
    NDayHist = 60
    NDayHistSL = 20
    sIndicator[(sReal > ADXThresholdUpper)&(sReal.shift(1) < ADXThresholdUpper)&(df['Close'] / df['Close'].rolling(NDayHist).min() > 1.1)] = 1
    sIndicator[(sReal < ADXThresholdLower)&(sReal.shift(1) > ADXThresholdLower)] = 0
    sIndicator[(df['Close']<df['Close'].shift(1).rolling(NDayHistSL).quantile(0.1))] = -1
    return sIndicator
def init_state(indata, test=False):
    openn = indata['open'].values
    close = indata['close'].values
    high = indata['high'].values
    low = indata['low'].values
    volume = indata['volume'].values
    
    diff = np.diff(close)
    diff = np.insert(diff, 0, 0)
    
    sma30 = talib.SMA(close, 30)
    sma60 = talib.SMA(close, timeperiod=60)
    rsi = talib.RSI(close, timeperiod=14)
    atr = talib.ATR(high, low, close, timeperiod=14)
    trange = talib.TRANGE(high, low, close)
    macd, macdsignal, macdhist = talib.MACD(close, 12, 26, 9)
    upper, middle, lower = talib.BBANDS(close, 20, 2, 2)
    ema = talib.EMA(close, 30)
    ma = talib.MA(close, 30)
    wma = talib.WMA(close, timeperiod=30)
    tema = talib.TEMA(close, 30)
    obv = talib.OBV(close, np.asarray(volume, dtype='float'))
    adx = talib.ADX(high, low, close, 14)
    apo = talib.APO(close, 12, 2, 0)
    bop = talib.BOP(openn, high, low, close)
    mom = talib.MOM(close,10)
    ppo = talib.PPO(close, 12, 26, 0)
    slowk, slowd = talib.STOCH(high, low, close, fastk_period=5, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0)
    ad = talib.AD(high, low, close, np.asarray(volume, dtype='float'))
    wcl = talib.WCLPRICE(high, low, close)

    #--- Preprocess data
    xdata = np.column_stack((close, diff, sma30, sma60, rsi, atr, macd, macdsignal, macdhist, lower, middle, upper, ema, ma, wma, adx, apo, bop, mom, ppo, slowk, slowd, trange, wcl))
    
    xdata = np.nan_to_num(xdata)
    if test == False:
        scaler = preprocessing.StandardScaler()
        xdata = np.expand_dims(scaler.fit_transform(xdata), axis=1)
        joblib.dump(scaler, 'data/scaler.pkl')
    elif test == True:
        scaler = joblib.load('data/scaler.pkl')
        xdata = np.expand_dims(scaler.fit_transform(xdata), axis=1)
    state = xdata[0:1, 0:1, :]

    return state, xdata, close
Ejemplo n.º 30
0
 def get_technicals_of_series(self, indivations):
     result = indivations
     result = np.vstack((result, ta.SMA(indivations, timeperiod=5)))
     result = np.vstack((result, ta.SMA(indivations, timeperiod=14)))
     result = np.vstack((result, ta.BBANDS(indivations)))
     result = np.vstack((result, ta.MAMA(indivations)))
     result = np.vstack((result, ta.APO(indivations)))
     result = np.vstack((result, ta.CMO(indivations)))
     result = np.vstack((result, ta.MACD(indivations)))
     result = np.vstack((result, ta.MOM(indivations)))
     result = np.vstack((result, ta.ROC(indivations)))
     result = np.vstack((result, ta.RSI(indivations)))
     result = np.vstack((result, ta.HT_TRENDMODE(indivations)))
     result = np.vstack((result, ta.LINEARREG(indivations)))
     result = result[:, ~np.isnan(result).any(axis=0)]
     result = result.T
     print(np.shape(result))
     return result