def setUpClass(cls): cls._df = pd.read_csv(cls._filename, sep=',') cls._params = dict(close=cls._df['Close'], n_slow=26, n_fast=12, n_sign=9, fillna=True) cls._indicator = PercentagePriceOscillator(**cls._params)
def setUpClass(cls): cls._df = pd.read_csv(cls._filename, sep=",") cls._params = dict( close=cls._df["Close"], window_slow=26, window_fast=12, window_sign=9, fillna=True, ) cls._indicator = PercentagePriceOscillator(**cls._params)
if len(sys.argv) > 1: batch_size = 31 symbol = sys.argv[1] end = datetime.today() start = datetime(2000, 9, 1) ETH = pdr.DataReader(symbol,'yahoo',start,end) df = pd.DataFrame(data=ETH) kama_indicator = KAMAIndicator(close = df["Close"], window = 10, pow1 = 2, pow2 = 30, fillna = False) df['kama'] = kama_indicator.kama() ppo_indicator = PercentagePriceOscillator(close = df["Close"], window_slow = 20, window_fast = 10, window_sign = 9, fillna = False) df['ppo'] = ppo_indicator.ppo() roc_indicator = ROCIndicator(close = df["Close"], window = 12, fillna = False) df['roc'] = roc_indicator.roc() macd_indicator = MACD(close = df["Close"], window_slow = 20, window_fast = 12, window_sign = 9, fillna = False) df['macd'] = macd_indicator.macd() rsi_indicator = RSIIndicator(close = df["Close"], window = 14, fillna = False) df['rsi'] = rsi_indicator.rsi() aroon_indicator = AroonIndicator(close = df["Close"], window = 20, fillna = False) df['aroon'] = aroon_indicator.aroon_indicator() boll_indicator = BollingerBands(close = df["Close"], window = 20, window_dev = 2, fillna = False)
def add_momentum_ta( df: pd.DataFrame, high: str, low: str, close: str, volume: str, fillna: bool = False, colprefix: str = "", vectorized: bool = False, ) -> pd.DataFrame: """Add trend technical analysis features to dataframe. Args: df (pandas.core.frame.DataFrame): Dataframe base. high (str): Name of 'high' column. low (str): Name of 'low' column. close (str): Name of 'close' column. volume (str): Name of 'volume' column. fillna(bool): if True, fill nan values. colprefix(str): Prefix column names inserted vectorized(bool): if True, use only vectorized functions indicators Returns: pandas.core.frame.DataFrame: Dataframe with new features. """ # Relative Strength Index (RSI) df[f"{colprefix}momentum_rsi"] = RSIIndicator(close=df[close], window=14, fillna=fillna).rsi() # Stoch RSI (StochRSI) indicator_srsi = StochRSIIndicator(close=df[close], window=14, smooth1=3, smooth2=3, fillna=fillna) df[f"{colprefix}momentum_stoch_rsi"] = indicator_srsi.stochrsi() df[f"{colprefix}momentum_stoch_rsi_k"] = indicator_srsi.stochrsi_k() df[f"{colprefix}momentum_stoch_rsi_d"] = indicator_srsi.stochrsi_d() # TSI Indicator df[f"{colprefix}momentum_tsi"] = TSIIndicator(close=df[close], window_slow=25, window_fast=13, fillna=fillna).tsi() # Ultimate Oscillator df[f"{colprefix}momentum_uo"] = UltimateOscillator( high=df[high], low=df[low], close=df[close], window1=7, window2=14, window3=28, weight1=4.0, weight2=2.0, weight3=1.0, fillna=fillna, ).ultimate_oscillator() # Stoch Indicator indicator_so = StochasticOscillator( high=df[high], low=df[low], close=df[close], window=14, smooth_window=3, fillna=fillna, ) df[f"{colprefix}momentum_stoch"] = indicator_so.stoch() df[f"{colprefix}momentum_stoch_signal"] = indicator_so.stoch_signal() # Williams R Indicator df[f"{colprefix}momentum_wr"] = WilliamsRIndicator( high=df[high], low=df[low], close=df[close], lbp=14, fillna=fillna).williams_r() # Awesome Oscillator df[f"{colprefix}momentum_ao"] = AwesomeOscillatorIndicator( high=df[high], low=df[low], window1=5, window2=34, fillna=fillna).awesome_oscillator() # Rate Of Change df[f"{colprefix}momentum_roc"] = ROCIndicator(close=df[close], window=12, fillna=fillna).roc() # Percentage Price Oscillator indicator_ppo = PercentagePriceOscillator(close=df[close], window_slow=26, window_fast=12, window_sign=9, fillna=fillna) df[f"{colprefix}momentum_ppo"] = indicator_ppo.ppo() df[f"{colprefix}momentum_ppo_signal"] = indicator_ppo.ppo_signal() df[f"{colprefix}momentum_ppo_hist"] = indicator_ppo.ppo_hist() # Percentage Volume Oscillator indicator_pvo = PercentageVolumeOscillator(volume=df[volume], window_slow=26, window_fast=12, window_sign=9, fillna=fillna) df[f"{colprefix}momentum_pvo"] = indicator_pvo.pvo() df[f"{colprefix}momentum_pvo_signal"] = indicator_pvo.pvo_signal() df[f"{colprefix}momentum_pvo_hist"] = indicator_pvo.pvo_hist() if not vectorized: # KAMA df[f"{colprefix}momentum_kama"] = KAMAIndicator(close=df[close], window=10, pow1=2, pow2=30, fillna=fillna).kama() return df
def add_momentum_ta(df: pd.DataFrame, high: str, low: str, close: str, volume: str, fillna: bool = False, colprefix: str = "") -> pd.DataFrame: """Add trend technical analysis features to dataframe. Args: df (pandas.core.frame.DataFrame): Dataframe base. high (str): Name of 'high' column. low (str): Name of 'low' column. close (str): Name of 'close' column. fillna(bool): if True, fill nan values. colprefix(str): Prefix column names inserted Returns: pandas.core.frame.DataFrame: Dataframe with new features. """ # Relative Strength Index (RSI) df[f'{colprefix}momentum_rsi'] = RSIIndicator(close=df[close], n=14, fillna=fillna).rsi() # Stoch RSI (StochRSI) indicator = StochRSIIndicator(close=df[close], n=14, d1=3, d2=3, fillna=fillna) df[f'{colprefix}momentum_stoch_rsi'] = indicator.stochrsi() df[f'{colprefix}momentum_stoch_rsi_k'] = indicator.stochrsi_k() df[f'{colprefix}momentum_stoch_rsi_d'] = indicator.stochrsi_d() # TSI Indicator df[f'{colprefix}momentum_tsi'] = TSIIndicator(close=df[close], r=25, s=13, fillna=fillna).tsi() # Ultimate Oscillator df[f'{colprefix}momentum_uo'] = UltimateOscillator( high=df[high], low=df[low], close=df[close], s=7, m=14, len=28, ws=4.0, wm=2.0, wl=1.0, fillna=fillna).uo() # Stoch Indicator indicator = StochasticOscillator(high=df[high], low=df[low], close=df[close], n=14, d_n=3, fillna=fillna) df[f'{colprefix}momentum_stoch'] = indicator.stoch() df[f'{colprefix}momentum_stoch_signal'] = indicator.stoch_signal() # Williams R Indicator df[f'{colprefix}momentum_wr'] = WilliamsRIndicator( high=df[high], low=df[low], close=df[close], lbp=14, fillna=fillna).wr() # Awesome Oscillator df[f'{colprefix}momentum_ao'] = AwesomeOscillatorIndicator( high=df[high], low=df[low], s=5, len=34, fillna=fillna).ao() # KAMA df[f'{colprefix}momentum_kama'] = KAMAIndicator( close=df[close], n=10, pow1=2, pow2=30, fillna=fillna).kama() # Rate Of Change df[f'{colprefix}momentum_roc'] = ROCIndicator(close=df[close], n=12, fillna=fillna).roc() # Percentage Price Oscillator indicator = PercentagePriceOscillator(close=df[close], n_slow=26, n_fast=12, n_sign=9, fillna=fillna) df[f'{colprefix}momentum_ppo'] = indicator.ppo() df[f'{colprefix}momentum_ppo_signal'] = indicator.ppo_signal() df[f'{colprefix}momentum_ppo_hist'] = indicator.ppo_hist() # Percentage Volume Oscillator indicator = PercentageVolumeOscillator(volume=df[volume], n_slow=26, n_fast=12, n_sign=9, fillna=fillna) df[f'{colprefix}momentum_ppo'] = indicator.pvo() df[f'{colprefix}momentum_ppo_signal'] = indicator.pvo_signal() df[f'{colprefix}momentum_ppo_hist'] = indicator.pvo_hist() return df
def features_df(stocks, rsi_vals=False, macd_vals=False, ppo=False, awesome_oscillator_val=False, daily_log_return=False, _return=False, change=False, volatility=False, min5=False, min10=False, min30=False, hband_indicator=False, lband_indicator=False, corr=False): """ Calculates the momentum, RSI, moving average convergence/divergence (MACD), Percentage Price Oscillator, awesome oscillator indicator, daily log return column, return, change, volatility, 5-10 and 30 mins moving average, high band indicator, lower band indicator, correlation of the given stock dataframe. :param stocks: dataframe of stocks :param rsi_vals: Default value False. If you want the RSI column, set True :param macd_vals: Default value False. If you want the MACD column, set True :param ppo: Default value False. If you want the Percentage Price Oscillator column, set True :param awesome_oscillator_val: Default value False. If you want the awesome oscillator indicator column, set True :param daily_log_return: Default value False. If you want the daily log return column, set True :param _return: Default value False. If you want the return column, set True :param change: Default value False. If you want the change column, set True :param volatility: Default value False. If you want the volatility column, set True :param min5: Default value False. If you want the min5 column, set True :param min10: Default value False. If you want the min10 column, set True :param min30: Default value False. If you want the min30 column, set True :param hband_indicator: Default value False. If you want the high band indicator column, set True :param lband_indicator: Default value False. If you want the lower band indicator column, set True :param corr: Default value False. If you want the correlation column, set True :return: a dataframe with the different features of said stock Default value False. If you want the RSI column, set True """ if rsi_vals is True: stocks['rsi_vals'] = RSIIndicator(close=stocks.Close, window=10).rsi() if macd_vals is True: stocks['macd_vals'] = macd(stocks.Close, window_slow=26, window_fast=12) if ppo is True: stocks['Ppo'] = np.array( PercentagePriceOscillator(stocks.Close, window_slow=26, window_fast=12).ppo()) if awesome_oscillator_val is True: stocks['awesome_oscillator'] = awesome_oscillator(stocks.High, stocks.Low, window1=5, window2=29) if daily_log_return is True: stocks['daily_log_return'] = daily_return(close=stocks.Close) if _return is True: stocks['Return'] = round(stocks['Close'] / stocks['Open'] - 1, 3) if change is True: stocks['Change'] = (stocks.Close - stocks.Close.shift(1)).fillna(0) if volatility is True: stocks['Volatility'] = stocks.Close.ewm(21).std() if min5 is True: stocks['min5'] = stocks.Close.rolling(5).mean() if min10 is True: stocks['min10'] = stocks.Close.rolling(10).mean() if min30 is True: stocks['min30'] = stocks.Close.rolling(30).mean() if hband_indicator is True: stocks['hband_indicator'] = BollingerBands( close=stocks.Close).bollinger_hband_indicator() if lband_indicator is True: stocks['lband_indicator'] = BollingerBands( close=stocks.Close).bollinger_lband_indicator() if corr is True: stocks['Corr'] = stocks.Close.rolling(window=10).corr(stocks['min10']) return stocks.iloc[-1, :]
for k in range(500): if k == 344: # This index gives an error when fitting the model pass else: print(k, Symbols[k]) data = yf.Ticker(Symbols[k]) reader = data.history(day, interval='1m') y = np.where(reader['Close'].shift(-1) > reader['Close'], 1, -1)[30:] reader['rsi_vals'] = RSIIndicator(close=reader.Close, window=10).rsi() reader['macd_vals'] = macd(reader.Close, window_slow=26, window_fast=12) reader['Ppo'] = PercentagePriceOscillator(reader.Close, window_slow=26, window_fast=12).ppo() reader['awesome_oscillator'] = awesome_oscillator(reader.High, reader.Low, window1=5, window2=29) reader['daily_log_return'] = daily_return(close=reader.Close) reader['Return'] = round(reader['Close'] / reader['Open'] - 1, 3) reader['Change'] = (reader.Close - reader.Close.shift(1)).fillna(0) reader['Volatility'] = reader.Close.ewm(21).std() reader['min5'] = reader.Close.rolling(5).mean() reader['min10'] = reader.Close.rolling(10).mean() reader['min30'] = reader.Close.rolling(30).mean() reader['hband_indicator'] = BollingerBands( close=reader.Close).bollinger_hband_indicator() reader['lband_indicator'] = BollingerBands(