def add_indicators(data: pd.DataFrame) -> pd.DataFrame: """ This method creates technical indicators, based on the OHLC and volume bars :param data: pandas DataFrame, containing open, high, low and close and optional volume columns :return: DataFrame with added technical indicators """ assert 'open' in data.columns, "open column not present or with different name" assert 'high' in data.columns, "high column not present or with different name" assert 'low' in data.columns, "low column not present or with different name" assert 'close' in data.columns, "close column not present or with different name" try: data['RSI'] = ta.rsi(data["close"]) data['TSI'] = ta.tsi(data["close"]) data['UO'] = ta.uo(data["high"], data["low"], data["close"]) data['AO'] = ta.ao(data["high"], data["low"]) data['MACD_diff'] = ta.macd_diff(data["close"]) data['Vortex_pos'] = ta.vortex_indicator_pos(data["high"], data["low"], data["close"]) data['Vortex_neg'] = ta.vortex_indicator_neg(data["high"], data["low"], data["close"]) data['Vortex_diff'] = abs(data['Vortex_pos'] - data['Vortex_neg']) data['Trix'] = ta.trix(data["close"]) data['Mass_index'] = ta.mass_index(data["high"], data["low"]) data['CCI'] = ta.cci(data["high"], data["low"], data["close"]) data['DPO'] = ta.dpo(data["close"]) data['KST'] = ta.kst(data["close"]) data['KST_sig'] = ta.kst_sig(data["close"]) data['KST_diff'] = (data['KST'] - data['KST_sig']) data['Aroon_up'] = ta.aroon_up(data["close"]) data['Aroon_down'] = ta.aroon_down(data["close"]) data['Aroon_ind'] = (data['Aroon_up'] - data['Aroon_down']) data['BBH'] = ta.bollinger_hband(data["close"]) data['BBL'] = ta.bollinger_lband(data["close"]) data['BBM'] = ta.bollinger_mavg(data["close"]) data['BBHI'] = ta.bollinger_hband_indicator(data["close"]) data['BBLI'] = ta.bollinger_lband_indicator(data["close"]) data['KCHI'] = ta.keltner_channel_hband_indicator(data["high"], data["low"], data["close"]) data['KCLI'] = ta.keltner_channel_lband_indicator(data["high"], data["low"], data["close"]) data['DCHI'] = ta.donchian_channel_hband_indicator(data["close"]) data['DCLI'] = ta.donchian_channel_lband_indicator(data["close"]) data['DR'] = ta.daily_return(data["close"]) data['DLR'] = ta.daily_log_return(data["close"]) if 'volume' in data.columns: data['MFI'] = ta.money_flow_index(data["high"], data["low"], data["close"], data["volume"]) data['ADI'] = ta.acc_dist_index(data["high"], data["low"], data["close"], data["volume"]) data['OBV'] = ta.on_balance_volume(data["close"], data["volume"]) data['CMF'] = ta.chaikin_money_flow(data["high"], data["low"], data["close"], data["volume"]) data['FI'] = ta.force_index(data["close"], data["volume"]) data['EM'] = ta.ease_of_movement(data["high"], data["low"], data["close"], data["volume"]) data['VPT'] = ta.volume_price_trend(data["close"], data["volume"]) data['NVI'] = ta.negative_volume_index(data["close"], data["volume"]) data.fillna(method='bfill', inplace=True) return data except (AssertionError, Exception) as error: raise IndicatorsError(error) LOGGER.error(error)
def enrich_sampleset(df): df['OBV'] = ta.on_balance_volume(df['close'], df['volume']) df['ADX'] = ta.adx(df['high'], df['low'], df['close'], n=14) df['VPT'] = ta.volume_price_trend(df['close'], df['volume']) df['ATR'] = ta.average_true_range(df['high'], df['low'], df['close'], n=14) df['MFI'] = ta.money_flow_index(df['high'], df['low'], df['close'], df['volume']) df['KST'] = ta.kst(df['close']) return ta.utils.dropna(df)
def money_flow_index(candles, window): """Money Flow Index""" highs = util.filtership(candles, "max") lows = util.filtership(candles, "min") closes = util.filtership(candles, "close") volumes = util.filtership(candles, "volume") result = ta.money_flow_index(highs, lows, closes, volumes, window) return result
def add_indicators(df): df['RSI'] = ta.rsi(df["Close"]) df['MFI'] = ta.money_flow_index(df["High"], df["Low"], df["Close"], df["Volume"]) df['TSI'] = ta.tsi(df["Close"]) df['UO'] = ta.uo(df["High"], df["Low"], df["Close"]) df['AO'] = ta.ao(df["High"], df["Low"]) df['MACD_diff'] = ta.macd_diff(df["Close"]) df['Vortex_pos'] = ta.vortex_indicator_pos(df["High"], df["Low"], df["Close"]) df['Vortex_neg'] = ta.vortex_indicator_neg(df["High"], df["Low"], df["Close"]) df['Vortex_diff'] = abs(df['Vortex_pos'] - df['Vortex_neg']) df['Trix'] = ta.trix(df["Close"]) df['Mass_index'] = ta.mass_index(df["High"], df["Low"]) df['CCI'] = ta.cci(df["High"], df["Low"], df["Close"]) df['DPO'] = ta.dpo(df["Close"]) df['KST'] = ta.kst(df["Close"]) df['KST_sig'] = ta.kst_sig(df["Close"]) df['KST_diff'] = (df['KST'] - df['KST_sig']) df['Aroon_up'] = ta.aroon_up(df["Close"]) df['Aroon_down'] = ta.aroon_down(df["Close"]) df['Aroon_ind'] = (df['Aroon_up'] - df['Aroon_down']) df['BBH'] = ta.bollinger_hband(df["Close"]) df['BBL'] = ta.bollinger_lband(df["Close"]) df['BBM'] = ta.bollinger_mavg(df["Close"]) df['BBHI'] = ta.bollinger_hband_indicator(df["Close"]) df['BBLI'] = ta.bollinger_lband_indicator(df["Close"]) df['KCHI'] = ta.keltner_channel_hband_indicator(df["High"], df["Low"], df["Close"]) df['KCLI'] = ta.keltner_channel_lband_indicator(df["High"], df["Low"], df["Close"]) df['DCHI'] = ta.donchian_channel_hband_indicator(df["Close"]) df['DCLI'] = ta.donchian_channel_lband_indicator(df["Close"]) df['ADI'] = ta.acc_dist_index(df["High"], df["Low"], df["Close"], df["Volume"]) df['OBV'] = ta.on_balance_volume(df["Close"], df["Volume"]) df['CMF'] = ta.chaikin_money_flow(df["High"], df["Low"], df["Close"], df["Volume"]) df['FI'] = ta.force_index(df["Close"], df["Volume"]) df['EM'] = ta.ease_of_movement(df["High"], df["Low"], df["Close"], df["Volume"]) df['VPT'] = ta.volume_price_trend(df["Close"], df["Volume"]) df['NVI'] = ta.negative_volume_index(df["Close"], df["Volume"]) df['DR'] = ta.daily_return(df["Close"]) df['DLR'] = ta.daily_log_return(df["Close"]) df.fillna(method='bfill', inplace=True) return df
def mf(): mfi = ta.money_flow_index(high, low, close, volume, n=14, fillna=False) MFI = mean(mfi[-7:]) if MFI <= 20: status2 = "MFI signal is: Buy" elif MFI <= 30: status2 = "MFI signal is: Buy" elif MFI >= 80: status2 = "MFI signal is: Sell" elif MFI >= 70: status2 = "MFI signal is: Sell" else: status2 = "MFI signal is: Hold" return status2
def add_candle_indicators(df, l, ck, hk, lk, vk): df[l + 'rsi'] = ta.rsi(df[ck]) df[l + 'mfi'] = ta.money_flow_index(df[hk], df[lk], df[ck], df[vk]) df[l + 'tsi'] = ta.tsi(df[ck]) df[l + 'uo'] = ta.uo(df[hk], df[lk], df[ck]) df[l + 'ao'] = ta.ao(df[hk], df[lk]) df[l + 'macd_diff'] = ta.macd_diff(df[ck]) df[l + 'vortex_pos'] = ta.vortex_indicator_pos(df[hk], df[lk], df[ck]) df[l + 'vortex_neg'] = ta.vortex_indicator_neg(df[hk], df[lk], df[ck]) df[l + 'vortex_diff'] = abs(df[l + 'vortex_pos'] - df[l + 'vortex_neg']) df[l + 'trix'] = ta.trix(df[ck]) df[l + 'mass_index'] = ta.mass_index(df[hk], df[lk]) df[l + 'cci'] = ta.cci(df[hk], df[lk], df[ck]) df[l + 'dpo'] = ta.dpo(df[ck]) df[l + 'kst'] = ta.kst(df[ck]) df[l + 'kst_sig'] = ta.kst_sig(df[ck]) df[l + 'kst_diff'] = (df[l + 'kst'] - df[l + 'kst_sig']) df[l + 'aroon_up'] = ta.aroon_up(df[ck]) df[l + 'aroon_down'] = ta.aroon_down(df[ck]) df[l + 'aroon_ind'] = (df[l + 'aroon_up'] - df[l + 'aroon_down']) df[l + 'bbh'] = ta.bollinger_hband(df[ck]) df[l + 'bbl'] = ta.bollinger_lband(df[ck]) df[l + 'bbm'] = ta.bollinger_mavg(df[ck]) df[l + 'bbhi'] = ta.bollinger_hband_indicator(df[ck]) df[l + 'bbli'] = ta.bollinger_lband_indicator(df[ck]) df[l + 'kchi'] = ta.keltner_channel_hband_indicator(df[hk], df[lk], df[ck]) df[l + 'kcli'] = ta.keltner_channel_lband_indicator(df[hk], df[lk], df[ck]) df[l + 'dchi'] = ta.donchian_channel_hband_indicator(df[ck]) df[l + 'dcli'] = ta.donchian_channel_lband_indicator(df[ck]) df[l + 'adi'] = ta.acc_dist_index(df[hk], df[lk], df[ck], df[vk]) df[l + 'obv'] = ta.on_balance_volume(df[ck], df[vk]) df[l + 'cmf'] = ta.chaikin_money_flow(df[hk], df[lk], df[ck], df[vk]) df[l + 'fi'] = ta.force_index(df[ck], df[vk]) df[l + 'em'] = ta.ease_of_movement(df[hk], df[lk], df[ck], df[vk]) df[l + 'vpt'] = ta.volume_price_trend(df[ck], df[vk]) df[l + 'nvi'] = ta.negative_volume_index(df[ck], df[vk]) df[l + 'dr'] = ta.daily_return(df[ck]) df[l + 'dlr'] = ta.daily_log_return(df[ck]) df[l + 'ma50'] = df[ck].rolling(window=50).mean() df[l + 'ma100'] = df[ck].rolling(window=100).mean() df[l + '26ema'] = df[[ck]].ewm(span=26).mean() df[l + '12ema'] = df[[ck]].ewm(span=12).mean() df[l + 'macd'] = (df[l + '12ema'] - df[l + '26ema']) df[l + '100sd'] = df[[ck]].rolling(100).std() df[l + 'upper_band'] = df[l + 'ma100'] + (df[l + '100sd'] * 2) df[l + 'lower_band'] = df[l + 'ma100'] - (df[l + '100sd'] * 2) df[l + 'ema'] = df[ck].ewm(com=0.5).mean() df[l + 'momentum'] = df[ck] - 1 return df
def add_ta_features(self): obv = talib.OBV(self.close, self.volume) obv_mv_avg = talib.MA(obv, timeperiod=10) obv_mv_avg[np.isnan(obv_mv_avg)] = obv[np.isnan(obv_mv_avg)] difference = obv - obv_mv_avg self.df['obv'] = obv self.df['obv_signal'] = difference self.df['obv_cheat'] = np.gradient(difference) upper, middle, lower = talib.BBANDS(self.close, timeperiod=20, nbdevup=2, nbdevdn=2, matype=0) self.df['dn'] = lower self.df['mavg'] = middle self.df['up'] = upper self.df['pctB'] = (self.close - self.df.dn) / (self.df.up - self.df.dn) rsi14 = talib.RSI(self.close, 14) self.df['rsi14'] = rsi14 macd, macdsignal, macdhist = talib.MACD(self.close, 12, 26, 9) self.df['macd'] = macd self.df['signal'] = macdsignal ## addtional info self.df['adx'] = talib.ADX(self.high, self.low, self.close, timeperiod=14) self.df['cci'] = talib.CCI(self.high, self.low, self.close, timeperiod=14) ## maximum profit self.df['plus_di'] = talib.PLUS_DI(self.high, self.low, self.close, timeperiod=14) ## lower_bound self.df['lower_bound'] = self.df['open'] - self.df['low'] + 1 ## ATR self.df['atr'] = talib.ATR(self.high, self.low, self.close, timeperiod=14) ## STOCH momentum self.df = ta.stochastic_oscillator_k(self.df) self.df = ta.stochastic_oscillator_d(self.df, n=10) ## TRIX self.df['trix'] = talib.TRIX(self.close, timeperiod=5) self.df['trix_signal'] = ta.moving_average(self.df['trix'], n=3) self.df['trix_hist'] = self.df['trix'] - self.df['trix_signal'] ## MFI self.df['mfi14'] = money_flow_index(self.df, 14)
import pandas as pd import seaborn as sns import matplotlib.pyplot as plt import ta df = pd.read_csv('./data/coinbase_daily.csv') df = df.dropna().reset_index().sort_values('Date') ta_df = pd.DataFrame() ta_df['RSI'] = ta.rsi(df["Close"]) ta_df['MFI'] = ta.money_flow_index( df["High"], df["Low"], df["Close"], df["Volume BTC"]) ta_df['TSI'] = ta.tsi(df["Close"]) ta_df['UO'] = ta.uo(df["High"], df["Low"], df["Close"]) ta_df['Stoch'] = ta.stoch(df["High"], df["Low"], df["Close"]) ta_df['Stoch_Signal'] = ta.stoch_signal(df["High"], df["Low"], df["Close"]) ta_df['WR'] = ta.wr(df["High"], df["Low"], df["Close"]) ta_df['AO'] = ta.ao(df["High"], df["Low"]) ta_df['MACD'] = ta.macd(df["Close"]) ta_df['MACD_signal'] = ta.macd_signal(df["Close"]) ta_df['MACD_diff'] = ta.macd_diff(df["Close"]) ta_df['EMA_fast'] = ta.ema_indicator(df["Close"]) ta_df['EMA_slow'] = ta.ema_indicator(df["Close"]) ta_df['Vortex_pos'] = ta.vortex_indicator_pos( df["High"], df["Low"], df["Close"]) ta_df['Vortex_neg'] = ta.vortex_indicator_neg( df["High"], df["Low"], df["Close"]) ta_df['Vortex_diff'] = abs( ta_df['Vortex_pos'] -
n4=15, nsig=9, fillna=True) X['ichimoku_a'] = ta.ichimoku_a(price['High'], price['Low'], n1=9, n2=26, fillna=True) X['ichimoku_b'] = ta.ichimoku_b(price['High'], price['Low'], n2=26, n3=52, fillna=True) X['money_flow_index'] = ta.money_flow_index(price['High'], price['Low'], price['Adj. Close'], price['Volume'], n=14, fillna=True) X['rsi'] = ta.rsi(price['Adj. Close'], n=14, fillna=True) X['tsi'] = ta.tsi(price['Adj. Close'], r=25, s=13, fillna=True) X['uo'] = ta.uo(price['High'], price['Low'], price['Adj. Close'], s=7, m=14, l=28, ws=4, wm=2, wl=1, fillna=True) X['stoch_signal'] = ta.stoch_signal(price['High'],
def add_technical_indicators(df): """ Args: df (pd.DataFrame): The processed dataframe returned by `process_data`. Returns: pd.DataFrame: The updated dataframe with the technical indicators inside. Acknowledgements: - Thanks for Adam King for this compilation of technical indicators! The original file and code can be found here: https://github.com/notadamking/RLTrader/blob/e5b83b1571f9fcfa6a67a2a810222f1f1751996c/util/indicators.py """ # Add momentum indicators df["AO"] = ta.ao(df["High"], df["Low"]) df["MFI"] = ta.money_flow_index(df["High"], df["Low"], df["Close"], df["Volume"]) df["RSI"] = ta.rsi(df["Close"]) df["TSI"] = ta.tsi(df["Close"]) df["UO"] = ta.uo(df["High"], df["Low"], df["Close"]) # Add trend indicators df["Aroon_up"] = ta.aroon_up(df["Close"]) df["Aroon_down"] = ta.aroon_down(df["Close"]) df["Aroon_ind"] = (df["Aroon_up"] - df["Aroon_down"]) df["CCI"] = ta.cci(df["High"], df["Low"], df["Close"]) df["DPO"] = ta.dpo(df["Close"]) df["KST"] = ta.kst(df["Close"]) df["KST_sig"] = ta.kst_sig(df["Close"]) df["KST_diff"] = (df["KST"] - df["KST_sig"]) df["MACD_diff"] = ta.macd_diff(df["Close"]) df["Mass_index"] = ta.mass_index(df["High"], df["Low"]) df["Trix"] = ta.trix(df["Close"]) df["Vortex_pos"] = ta.vortex_indicator_pos(df["High"], df["Low"], df["Close"]) df["Vortex_neg"] = ta.vortex_indicator_neg(df["High"], df["Low"], df["Close"]) df["Vortex_diff"] = abs(df["Vortex_pos"] - df["Vortex_neg"]) # Add volatility indicators df["BBH"] = ta.bollinger_hband(df["Close"]) df["BBL"] = ta.bollinger_lband(df["Close"]) df["BBM"] = ta.bollinger_mavg(df["Close"]) df["BBHI"] = ta.bollinger_hband_indicator(df["Close"]) df["BBLI"] = ta.bollinger_lband_indicator(df["Close"]) df["KCHI"] = ta.keltner_channel_hband_indicator(df["High"], df["Low"], df["Close"]) df["KCLI"] = ta.keltner_channel_lband_indicator(df["High"], df["Low"], df["Close"]) df["DCHI"] = ta.donchian_channel_hband_indicator(df["Close"]) df["DCLI"] = ta.donchian_channel_lband_indicator(df["Close"]) # Volume indicators df["ADI"] = ta.acc_dist_index(df["High"], df["Low"], df["Close"], df["Volume"]) df["CMF"] = ta.chaikin_money_flow(df["High"], df["Low"], df["Close"], df["Volume"]) df["EM"] = ta.ease_of_movement(df["High"], df["Low"], df["Close"], df["Volume"]) df["FI"] = ta.force_index(df["Close"], df["Volume"]) df["NVI"] = ta.negative_volume_index(df["Close"], df["Volume"]) df["OBV"] = ta.on_balance_volume(df["Close"], df["Volume"]) df["VPT"] = ta.volume_price_trend(df["Close"], df["Volume"]) # Add miscellaneous indicators df["DR"] = ta.daily_return(df["Close"]) df["DLR"] = ta.daily_log_return(df["Close"]) # Fill in NaN values df.fillna(method="bfill", inplace=True) # First try `bfill` df.fillna(value=0, inplace=True) # Then replace the rest of the NANs with 0s return df