def get_ml_feature(self, symbol, prices=None, cutoff=None): if prices: price = prices.get(symbol, 1E10) vix = prices['^VIX'] else: price = self.closes[symbol][cutoff] vix = self.closes['^VIX'][cutoff] if cutoff: close = self.closes[symbol][cutoff - DAYS_IN_A_YEAR:cutoff] high = np.array( self.hists[symbol].get('High')[cutoff - DAYS_IN_A_YEAR:cutoff]) low = np.array( self.hists[symbol].get('Low')[cutoff - DAYS_IN_A_YEAR:cutoff]) else: close = self.closes[symbol][-DAYS_IN_A_YEAR:] high = np.array(self.hists[symbol].get('High')[-DAYS_IN_A_YEAR:]) low = np.array(self.hists[symbol].get('Low')[-DAYS_IN_A_YEAR:]) # Basic stats day_range_change = price / np.max(close[-DATE_RANGE:]) - 1 today_change = price / close[-1] - 1 yesterday_change = close[-1] / close[-2] - 1 day_before_yesterday_change = close[-2] / close[-3] - 1 twenty_day_change = price / close[-20] - 1 year_high_change = price / np.max(close) - 1 year_low_change = price / np.min(close) - 1 all_changes = [ close[t + 1] / close[t] - 1 for t in range(len(close) - 1) if close[t + 1] > 0 and close[t] > 0 ] # Technical indicators close = np.append(close, price) high = np.append(high, price) low = np.append(low, price) pd_close = pd.Series(close) pd_high = pd.Series(high) pd_low = pd.Series(low) rsi = momentum.rsi(pd_close).values[-1] macd_rate = trend.macd_diff(pd_close).values[-1] / price wr = momentum.wr(pd_high, pd_low, pd_close).values[-1] tsi = momentum.tsi(pd_close).values[-1] feature = { 'Today_Change': today_change, 'Yesterday_Change': yesterday_change, 'Day_Before_Yesterday_Change': day_before_yesterday_change, 'Twenty_Day_Change': twenty_day_change, 'Day_Range_Change': day_range_change, 'Year_High_Change': year_high_change, 'Year_Low_Change': year_low_change, 'Change_Average': np.mean(all_changes), 'Change_Variance': np.var(all_changes), 'RSI': rsi, 'MACD_Rate': macd_rate, 'WR': wr, 'TSI': tsi, 'VIX': vix } return feature
def WilliamsR(df, intervals): """ Williams Percent Range. Momentum indicator. Moves between 0 to-100 and measures overbought and oversold levels. Used to find entry and exit points in the market. Reference: https://www.investopedia.com/terms/w/williamsr.asp """ from ta.momentum import wr from tqdm.auto import tqdm for interval in tqdm(intervals): df["wr_" + str(interval)] = wr(df['high'], df['low'], df['close'], interval)
def williams(datos, start, end= '', window = 10): ''' ENTRADA datos: Pandas dataframe que contiene al menos una columna de fechas (DATE) y otra columna numérica start, end: strings en formato 'YYYY-MM-DD' representando la fecha de inicio y la fecha final respectivamente window: Entero que representa la ventan de tiempo a utilizar SALIDA resultado: Dataframe datos con una columna extra conteniendo la información del indicador ''' #Localiza la fecha de inicio y revisa si hay suficiente información indiceInicio=datos[datos['Date']==start].index[0] if window > indiceInicio + 1: print 'No hay suficiente historia para esta fecha' return datos #Último índice if end=='': lastIndex=datos.shape[0] - 1 else: lastIndex=datos[datos['Date']==end].index[0] #calcula el indicador indicador = wr(datos['High'], datos['Low'], datos['Adj Close'], window) #agrega la nueva columna resultado = deepcopy(datos) resName = 'Williams-R-' + str(window) resultado[resName] = indicador #Filtra a partir del índice correspondiente a la fecha start resultado=resultado.iloc[indiceInicio:lastIndex+1,:] resultado=resultado.reset_index(drop=True) #añade metadatos resultado.tipo = 'williams' resultado.resName = resName return resultado
def williams_r_cb_pressed(stock_analysis_tool: QMainWindow, cb: QCheckBox) -> None: """ :param cb: :return: """ if cb.isChecked(): # Add WilliamsR to Display Graph stock_analysis_tool.df['WilliamsR'] = wr( stock_analysis_tool.df['high'], stock_analysis_tool.df['low'], stock_analysis_tool.df['close']) stock_analysis_tool.df['WilliamsR overbought'] = -20 stock_analysis_tool.df['WilliamsR oversold'] = -80 stock_analysis_tool.add_column_to_graph( column_name='WilliamsR') stock_analysis_tool.add_column_to_graph( column_name='WilliamsR overbought', color=stock_analysis_tool.protected_colors['red']) stock_analysis_tool.add_column_to_graph( column_name='WilliamsR oversold', color=stock_analysis_tool.protected_colors['green']) else: # Remove WilliamsR from Display Graph stock_analysis_tool.remove_column_from_graph( column_name='WilliamsR') stock_analysis_tool.remove_column_from_graph( column_name='WilliamsR overbought') stock_analysis_tool.remove_column_from_graph( column_name='WilliamsR oversold') stock_analysis_tool.df = stock_analysis_tool.df.drop( "WilliamsR", axis=1) stock_analysis_tool.df = stock_analysis_tool.df.drop( "WilliamsR overbought", axis=1) stock_analysis_tool.df = stock_analysis_tool.df.drop( "WilliamsR oversold", axis=1)
def engineer_data_over_single_interval(df: pd.DataFrame, indicators: list, ticker: str = "", rsi_n: int = 14, cmo_n: int = 7, macd_fast: int = 12, macd_slow: int = 26, macd_sign: int = 9, roc_n: int = 12, cci_n: int = 20, dpo_n: int = 20, cmf_n: int = 20, adx_n: int = 14, mass_index_low: int = 9, mass_index_high: int = 25, trix_n: int = 15, stochastic_oscillator_n: int = 14, stochastic_oscillator_sma_n: int = 3, ultimate_oscillator_short_n: int = 7, ultimate_oscillator_medium_n: int = 14, ultimate_oscillator_long_n: int = 28, ao_short_n: int = 5, ao_long_n: int = 34, kama_n: int = 10, tsi_high_n: int = 25, tsi_low_n: int = 13, eom_n: int = 14, force_index_n: int = 13, ichimoku_low_n: int = 9, ichimoku_medium_n: int = 26): from ta.momentum import rsi, wr, roc, ao, stoch, uo, kama, tsi from ta.trend import macd, macd_signal, cci, dpo, adx, mass_index, trix, ichimoku_a from ta.volume import chaikin_money_flow, acc_dist_index, ease_of_movement, force_index # Momentum Indicators if Indicators.RELATIVE_STOCK_INDEX in indicators: Logger.console_log(message="Calculating " + Indicators.RELATIVE_STOCK_INDEX.value + " for stock " + ticker, status=Logger.LogStatus.EMPHASIS) df[Indicators.RELATIVE_STOCK_INDEX.value] = rsi(close=df['close'], n=rsi_n) if Indicators.WILLIAMS_PERCENT_RANGE in indicators: Logger.console_log(message="Calculating " + Indicators.WILLIAMS_PERCENT_RANGE.value + " for stock " + ticker, status=Logger.LogStatus.EMPHASIS) df[Indicators.WILLIAMS_PERCENT_RANGE.value] = wr( df['high'], df['low'], df['close']) if Indicators.CHANDE_MOMENTUM_OSCILLATOR in indicators: Logger.console_log(message="Calculating " + Indicators.CHANDE_MOMENTUM_OSCILLATOR.value + " for stock " + ticker, status=Logger.LogStatus.EMPHASIS) df[Indicators.CHANDE_MOMENTUM_OSCILLATOR. value] = chande_momentum_oscillator(close_data=df['close'], period=cmo_n) if Indicators.RATE_OF_CHANGE in indicators: Logger.console_log(message="Calculating " + Indicators.RATE_OF_CHANGE.value + " for stock " + ticker, status=Logger.LogStatus.EMPHASIS) df[Indicators.RATE_OF_CHANGE.value] = roc(close=df['close'], n=roc_n) if Indicators.STOCHASTIC_OSCILLATOR in indicators: Logger.console_log(message="Calculating " + Indicators.STOCHASTIC_OSCILLATOR.value + " for stock " + ticker, status=Logger.LogStatus.EMPHASIS) df[Indicators.STOCHASTIC_OSCILLATOR.value] = stoch( high=df['high'], low=df['low'], close=df['close'], n=stochastic_oscillator_n, d_n=stochastic_oscillator_sma_n) if Indicators.ULTIMATE_OSCILLATOR in indicators: Logger.console_log(message="Calculating " + Indicators.ULTIMATE_OSCILLATOR.value + " for stock " + ticker, status=Logger.LogStatus.EMPHASIS) df[Indicators.ULTIMATE_OSCILLATOR.value] = uo( high=df['high'], low=df['low'], close=df['close'], s=ultimate_oscillator_short_n, m=ultimate_oscillator_medium_n, len=ultimate_oscillator_long_n) if Indicators.AWESOME_OSCILLATOR in indicators: Logger.console_log(message="Calculating " + Indicators.AWESOME_OSCILLATOR.value + " for stock " + ticker, status=Logger.LogStatus.EMPHASIS) df[Indicators.AWESOME_OSCILLATOR.value] = ao(high=df['high'], low=df['low'], s=ao_short_n, len=ao_long_n) if Indicators.KAUFMAN_ADAPTIVE_MOVING_AVERAGE in indicators: Logger.console_log(message="Calculating " + Indicators.KAUFMAN_ADAPTIVE_MOVING_AVERAGE.value + " for stock " + ticker, status=Logger.LogStatus.EMPHASIS) df[Indicators.KAUFMAN_ADAPTIVE_MOVING_AVERAGE.value] = kama( close=df['close'], n=kama_n) if Indicators.TRUE_STRENGTH_INDEX in indicators: Logger.console_log(message="Calculating " + Indicators.TRUE_STRENGTH_INDEX.value + " for stock " + ticker, status=Logger.LogStatus.EMPHASIS) df[Indicators.TRUE_STRENGTH_INDEX.value] = tsi(close=df['close'], r=tsi_high_n, s=tsi_low_n) # Trend Indicator if Indicators.MOVING_AVERAGE_CONVERGENCE_DIVERGENCE in indicators: Logger.console_log( message="Calculating " + Indicators.MOVING_AVERAGE_CONVERGENCE_DIVERGENCE.value + " for stock " + ticker, status=Logger.LogStatus.EMPHASIS) df[Indicators.MOVING_AVERAGE_CONVERGENCE_DIVERGENCE.value] = macd(close=df['close'], n_slow=macd_slow, n_fast=macd_fast) - \ macd_signal(close=df['close'], n_slow=macd_slow, n_fast=macd_fast, n_sign=macd_sign) if Indicators.COMMODITY_CHANNEL_INDEX in indicators: Logger.console_log(message="Calculating " + Indicators.COMMODITY_CHANNEL_INDEX.value + " for stock " + ticker, status=Logger.LogStatus.EMPHASIS) df[Indicators.COMMODITY_CHANNEL_INDEX.value] = cci(high=df['high'], low=df['low'], close=df['close'], n=cci_n) if Indicators.DETRENDED_PRICE_OSCILLATOR in indicators: Logger.console_log(message="Calculating " + Indicators.DETRENDED_PRICE_OSCILLATOR.value + " for stock " + ticker, status=Logger.LogStatus.EMPHASIS) df[Indicators.DETRENDED_PRICE_OSCILLATOR.value] = dpo( close=df['close'], n=dpo_n) if Indicators.AVERAGE_DIRECTIONAL_INDEX in indicators: Logger.console_log(message="Calculating " + Indicators.AVERAGE_DIRECTIONAL_INDEX.value + " for stock " + ticker, status=Logger.LogStatus.EMPHASIS) df[Indicators.AVERAGE_DIRECTIONAL_INDEX.value] = adx(high=df['high'], low=df['low'], close=df['close'], n=adx_n) if Indicators.MASS_INDEX in indicators: Logger.console_log(message="Calculating " + Indicators.MASS_INDEX.value + " for stock " + ticker, status=Logger.LogStatus.EMPHASIS) df[Indicators.MASS_INDEX.value] = mass_index(high=df['high'], low=df['low'], n=mass_index_low, n2=mass_index_high) if Indicators.TRIPLE_EXPONENTIALLY_SMOOTHED_MOVING_AVERAGE in indicators: Logger.console_log( message="Calculating " + Indicators.TRIPLE_EXPONENTIALLY_SMOOTHED_MOVING_AVERAGE.value + " for stock " + ticker, status=Logger.LogStatus.EMPHASIS) df[Indicators.TRIPLE_EXPONENTIALLY_SMOOTHED_MOVING_AVERAGE. value] = trix(close=df['close'], n=trix_n) if Indicators.ICHIMOKU_A in indicators: Logger.console_log(message="Calculating " + Indicators.ICHIMOKU_A.value + " for stock " + ticker, status=Logger.LogStatus.EMPHASIS) df[Indicators.ICHIMOKU_A.value] = ichimoku_a(high=df['high'], low=df['low'], n1=ichimoku_low_n, n2=ichimoku_medium_n) # Volume Indicator if Indicators.CHAIKIN_MONEY_FLOW in indicators: Logger.console_log(message="Calculating " + Indicators.CHAIKIN_MONEY_FLOW.value + " for stock " + ticker, status=Logger.LogStatus.EMPHASIS) df[Indicators.CHAIKIN_MONEY_FLOW.value] = chaikin_money_flow( high=df['high'], low=df['low'], close=df['close'], volume=df['volume'], n=cmf_n) if Indicators.ACCUMULATION_DISTRIBUTION_INDEX in indicators: Logger.console_log(message="Calculating " + Indicators.ACCUMULATION_DISTRIBUTION_INDEX.value + " for stock " + ticker, status=Logger.LogStatus.EMPHASIS) df[Indicators.ACCUMULATION_DISTRIBUTION_INDEX.value] = acc_dist_index( high=df['high'], low=df['low'], close=df['close'], volume=df['volume']) if Indicators.EASE_OF_MOVEMENT in indicators: Logger.console_log(message="Calculating " + Indicators.EASE_OF_MOVEMENT.value + " for stock " + ticker, status=Logger.LogStatus.EMPHASIS) df[Indicators.EASE_OF_MOVEMENT.value] = ease_of_movement( high=df['high'], low=df['low'], volume=df['volume'], n=eom_n) if Indicators.FORCE_INDEX in indicators: Logger.console_log(message="Calculating " + Indicators.FORCE_INDEX.value + " for stock " + ticker, status=Logger.LogStatus.EMPHASIS) df[Indicators.FORCE_INDEX.value] = force_index(close=df['close'], volume=df['volume'], n=force_index_n)
def test_wr2(self): target = 'Williams_%R' result = wr(**self._params) pd.testing.assert_series_equal(self._df[target].tail(), result.tail(), check_names=False)
from ta.momentum import uo # Ultimate Oscilator from ta.momentum import wr #William Percent Range from ta.trend import macd # Moving Average Convergence/Divergence # *************************** DATA PREPROCESSING ****************************** # Load Data df = pd.read_csv('^GSPC.csv') # Data Augmentation df['Relative_Strength_Index'] = rsi(df['Close']) df['Money_Flow_Index'] = money_flow_index(df['High'], df['Low'], df['Close'], df['Volume']) df['Stoch_Oscilator'] = stoch(df['High'], df['Low'], df['Close']) df['Ultimate_Oscilator'] = uo(df['High'], df['Low'], df['Close']) df['William_Percent'] = wr(df['High'], df['Low'], df['Close']) df['MACD'] = macd(df['Close']) # Some indicators require many days in advance before they produce any # values. So the begining rows of our df may have NaNs. Lets drop them: df = df.dropna() # Scaling Data from sklearn.preprocessing import MinMaxScalern sc = MinMaxScaler(feature_range=(0, 1)) scaled_df = sc.fit_transform(df.iloc[:, 1:].values) # Creating a data structure with 60 timesteps and 1 output X_train = np.array( [scaled_df[i:i + 60, :] for i in range(len(scaled_df) - 60)]) y_train = np.array([scaled_df[i + 60, 0] for i in range(len(scaled_df) - 60)])
def calculate_Momentum_Indicators(): JSON_sent = request.get_json() df = pd.DataFrame(JSON_sent[0]) _, RSI, TSI, UO, STOCH, STOCH_SIGNAL, WR, AO, KAMA, ROC = JSON_sent indicator_RSI = RSIIndicator(close=df["close"], n=RSI['N']) df['rsi'] = indicator_RSI.rsi() if TSI['displayTSI']: indicator_TSI = TSIIndicator(close=df["close"], r=TSI['rTSI'], s=TSI['sTSI']) df['tsi'] = indicator_TSI.tsi() if UO['displayUO']: indicator_UO = uo(high=df['high'], low=df['low'], close=df['close'], s=UO['sForUO'], m=UO['mForUO'], len=UO['lenForUO'], ws=UO['wsForUO'], wm=UO['wmForUO'], wl=UO['wlForUO']) df['uo'] = indicator_UO if STOCH['displaySTOCH']: indicator_Stoch = stoch(high=df['high'], low=df['low'], close=df['close'], n=STOCH['nForSTOCH'], d_n=STOCH['dnForSTOCH']) df['stoch'] = indicator_Stoch if STOCH_SIGNAL['displayStochSignal']: indicator_StochSignal = stoch_signal( high=df['high'], low=df['low'], close=df['close'], n=STOCH_SIGNAL['nForStochSignal'], d_n=STOCH_SIGNAL['dnForStochSignal']) df['stoch_signal'] = indicator_StochSignal if WR['displayWR']: indicator_wr = wr(high=df['high'], low=df['low'], close=df['close'], lbp=WR['lbpForWR']) df['wr'] = indicator_wr if AO['displayAO']: indicator_ao = ao(high=df['high'], low=df['low'], s=AO['sForAO'], len=AO['lenForAO']) df['ao'] = indicator_ao if KAMA['displayKama']: indicator_kama = kama(close=df['close'], n=KAMA['nForKama'], pow1=KAMA['pow1ForKama'], pow2=KAMA['pow2ForKama']) df['kama'] = indicator_kama if ROC['displayROC']: indicator_roc = roc(close=df['close'], n=ROC['nForROC']) df['roc'] = indicator_roc df.fillna(0, inplace=True) export_df = df.drop(columns=['open', 'high', 'low', 'close', 'volume']) return (json.dumps(export_df.to_dict('records')))