def ROC(df, col_name, intervals): """ Rate of Change = ((close_price_n - close_price_(n-1)) / close_price_(n-1)) * 100 Positive values indicate buying pressure while negative values indicate selling pressure Reference: https://www.investopedia.com/terms/p/pricerateofchange.asp https://school.stockcharts.com/doku.php?id=technical_indicators:rate_of_change_roc_and_momentum """ from tqdm.auto import tqdm from ta.momentum import roc for interval in tqdm(intervals): df['roc_' + str(interval)] = roc(df[col_name], n=interval, fillna=True)
def roc_cb_pressed(stock_analysis_tool: QMainWindow, cb: QCheckBox) -> None: """ :param cb: :return: """ if cb.isChecked(): # Add ROC to Display Graph stock_analysis_tool.df['roc'] = roc( close=stock_analysis_tool.df['close'], n=stock_analysis_tool.roc_n) stock_analysis_tool.add_column_to_graph(column_name='roc') else: # Remove ROC from Display Graph stock_analysis_tool.remove_column_from_graph(column_name='roc') stock_analysis_tool.df = stock_analysis_tool.df.drop("roc", 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_roc(self): target = 'ROC' result = roc(**self._params) pd.testing.assert_series_equal(self._df[target].tail(), result.tail(), check_names=False)
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')))
def test_roc(self): target = 'ROC' result = roc(close=self._df['Close'], n=12, fillna=False) pd.testing.assert_series_equal(self._df[target].tail(), result.tail(), check_names=False)
def calculate(self) -> pd.Series: """ Calculates the rate of change :return: Series containing the rate of change values for each closing price """ return momentum.roc(self._history['Close'], self.__days)