def add_volatility_ta(df: pd.DataFrame, high: str, low: str, close: str, fillna: bool = False, colprefix: str = "") -> pd.DataFrame: """Add volatility 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. """ # Average True Range df[f'{colprefix}volatility_atr'] = AverageTrueRange( close=df[close], high=df[high], low=df[low], n=10, fillna=fillna).average_true_range() # Bollinger Bands indicator_bb = BollingerBands(close=df[close], n=20, ndev=2, fillna=fillna) df[f'{colprefix}volatility_bbm'] = indicator_bb.bollinger_mavg() df[f'{colprefix}volatility_bbh'] = indicator_bb.bollinger_hband() df[f'{colprefix}volatility_bbl'] = indicator_bb.bollinger_lband() df[f'{colprefix}volatility_bbw'] = indicator_bb.bollinger_wband() df[f'{colprefix}volatility_bbhi'] = indicator_bb.bollinger_hband_indicator( ) df[f'{colprefix}volatility_bbli'] = indicator_bb.bollinger_lband_indicator( ) # Keltner Channel indicator_kc = KeltnerChannel(close=df[close], high=df[high], low=df[low], n=10, fillna=fillna) df[f'{colprefix}volatility_kcc'] = indicator_kc.keltner_channel_central() df[f'{colprefix}volatility_kch'] = indicator_kc.keltner_channel_hband() df[f'{colprefix}volatility_kcl'] = indicator_kc.keltner_channel_lband() df[f'{colprefix}volatility_kchi'] = indicator_kc.keltner_channel_hband_indicator( ) df[f'{colprefix}volatility_kcli'] = indicator_kc.keltner_channel_lband_indicator( ) # Donchian Channel indicator_dc = DonchianChannel(close=df[close], n=20, fillna=fillna) df[f'{colprefix}volatility_dcl'] = indicator_dc.donchian_channel_lband() df[f'{colprefix}volatility_dch'] = indicator_dc.donchian_channel_hband() df[f'{colprefix}volatility_dchi'] = indicator_dc.donchian_channel_hband_indicator( ) df[f'{colprefix}volatility_dcli'] = indicator_dc.donchian_channel_lband_indicator( ) return df
def _run(self): # Bollinger Bands bb_indicator = BollingerBands(close=self._close, n=self._chan_period, ndev=self._bol_band_std_dev, fillna=self._fillna) self._bb_hband = bb_indicator.bollinger_hband() self._bb_lband = bb_indicator.bollinger_lband() # Keltner Channel kb_indicator = KeltnerChannel(high=self._high, low=self._low, close=self._close, n=self._chan_period, n_atr=self._kelt_std_dev, fillna=self._fillna, ov=False) self._kb_hband = kb_indicator.keltner_channel_hband() self._kb_lband = kb_indicator.keltner_channel_lband() # Momentum Oscillator smo_indicator = SMOIndicator(high=self._high, low=self._low, close=self._close, n=self._mom_period, fillna=self._fillna) self._oscillator = smo_indicator.smo() # Bar and Signal Colors self._squeeze = bb_indicator.bollinger_wband( ) - kb_indicator.keltner_channel_wband() self._squeeze = self._squeeze.ge(0).astype(int)
def add_volatility_ta( df: pd.DataFrame, high: str, low: str, close: str, fillna: bool = False, colprefix: str = "", vectorized: bool = False, ) -> pd.DataFrame: """Add volatility 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 vectorized(bool): if True, use only vectorized functions indicators Returns: pandas.core.frame.DataFrame: Dataframe with new features. """ # Bollinger Bands indicator_bb = BollingerBands(close=df[close], window=20, window_dev=2, fillna=fillna) df[f"{colprefix}volatility_bbm"] = indicator_bb.bollinger_mavg() df[f"{colprefix}volatility_bbh"] = indicator_bb.bollinger_hband() df[f"{colprefix}volatility_bbl"] = indicator_bb.bollinger_lband() df[f"{colprefix}volatility_bbw"] = indicator_bb.bollinger_wband() df[f"{colprefix}volatility_bbp"] = indicator_bb.bollinger_pband() df[f"{colprefix}volatility_bbhi"] = indicator_bb.bollinger_hband_indicator( ) df[f"{colprefix}volatility_bbli"] = indicator_bb.bollinger_lband_indicator( ) # Keltner Channel indicator_kc = KeltnerChannel(close=df[close], high=df[high], low=df[low], window=10, fillna=fillna) df[f"{colprefix}volatility_kcc"] = indicator_kc.keltner_channel_mband() df[f"{colprefix}volatility_kch"] = indicator_kc.keltner_channel_hband() df[f"{colprefix}volatility_kcl"] = indicator_kc.keltner_channel_lband() df[f"{colprefix}volatility_kcw"] = indicator_kc.keltner_channel_wband() df[f"{colprefix}volatility_kcp"] = indicator_kc.keltner_channel_pband() df[f"{colprefix}volatility_kchi"] = indicator_kc.keltner_channel_hband_indicator( ) df[f"{colprefix}volatility_kcli"] = indicator_kc.keltner_channel_lband_indicator( ) # Donchian Channel indicator_dc = DonchianChannel(high=df[high], low=df[low], close=df[close], window=20, offset=0, fillna=fillna) df[f"{colprefix}volatility_dcl"] = indicator_dc.donchian_channel_lband() df[f"{colprefix}volatility_dch"] = indicator_dc.donchian_channel_hband() df[f"{colprefix}volatility_dcm"] = indicator_dc.donchian_channel_mband() df[f"{colprefix}volatility_dcw"] = indicator_dc.donchian_channel_wband() df[f"{colprefix}volatility_dcp"] = indicator_dc.donchian_channel_pband() if not vectorized: # Average True Range df[f"{colprefix}volatility_atr"] = AverageTrueRange( close=df[close], high=df[high], low=df[low], window=10, fillna=fillna).average_true_range() # Ulcer Index df[f"{colprefix}volatility_ui"] = UlcerIndex( close=df[close], window=14, fillna=fillna).ulcer_index() return df
def applyIndicator(self, full_company_price): self.data = full_company_price high = self.data['high'] low = self.data['low'] close = self.data['close'] volume = self.data['volume'] EMA12 = EMAIndicator(close, 12, fillna=False) EMA30 = EMAIndicator(close, 20, fillna=False) EMA60 = EMAIndicator(close, 60, fillna=False) MACD1226 = MACD(close, 26, 12, 9, fillna=False) MACD2452 = MACD(close, 52, 24, 18, fillna=False) ROC12 = ROCIndicator(close, 12, fillna=False) ROC30 = ROCIndicator(close, 30, fillna=False) ROC60 = ROCIndicator(close, 60, fillna=False) RSI14 = RSIIndicator(close, 14, fillna=False) RSI28 = RSIIndicator(close, 28, fillna=False) RSI60 = RSIIndicator(close, 60, fillna=False) AROON25 = AroonIndicator(close, 25, fillna=False) AROON50 = AroonIndicator(close, 50, fillna=False) AROON80 = AroonIndicator(close, 80, fillna=False) MFI14 = MFIIndicator(high, low, close, volume, 14, fillna=False) MFI28 = MFIIndicator(high, low, close, volume, 28, fillna=False) MFI80 = MFIIndicator(high, low, close, volume, 80, fillna=False) CCI20 = CCIIndicator(high, low, close, 20, 0.015, fillna=False) CCI40 = CCIIndicator(high, low, close, 40, 0.015, fillna=False) CCI100 = CCIIndicator(high, low, close, 100, 0.015, fillna=False) WILLR14 = WilliamsRIndicator(high, low, close, 14, fillna=False) WILLR28 = WilliamsRIndicator(high, low, close, 28, fillna=False) WILLR60 = WilliamsRIndicator(high, low, close, 60, fillna=False) BBANDS20 = BollingerBands(close, 20, 2, fillna=False) KC20 = KeltnerChannel(high, low, close, 20, 10, fillna=False) STOCH14 = StochasticOscillator(high, low, close, 14, 3, fillna=False) STOCH28 = StochasticOscillator(high, low, close, 28, 6, fillna=False) STOCH60 = StochasticOscillator(high, low, close, 60, 12, fillna=False) CMI20 = ChaikinMoneyFlowIndicator(high, low, close, volume, 20, fillna=False) CMI40 = ChaikinMoneyFlowIndicator(high, low, close, volume, 40, fillna=False) CMI100 = ChaikinMoneyFlowIndicator(high, low, close, volume, 100, fillna=False) self.data['ema12'] = (close - EMA12.ema_indicator()) / close self.data['ema30'] = (close - EMA30.ema_indicator()) / close self.data['ema60'] = (close - EMA60.ema_indicator()) / close self.data['macd1226'] = MACD1226.macd() - MACD1226.macd_signal() self.data['macd2452'] = MACD2452.macd() - MACD2452.macd_signal() self.data['roc12'] = ROC12.roc() self.data['roc30'] = ROC30.roc() self.data['roc60'] = ROC60.roc() self.data['rsi14'] = RSI14.rsi() self.data['rsi28'] = RSI28.rsi() self.data['rsi60'] = RSI60.rsi() self.data['aroon25'] = AROON25.aroon_indicator() self.data['aroon50'] = AROON50.aroon_indicator() self.data['aroon80'] = AROON80.aroon_indicator() self.data['mfi14'] = MFI14.money_flow_index() self.data['mfi28'] = MFI28.money_flow_index() self.data['mfi80'] = MFI80.money_flow_index() self.data['cci20'] = CCI20.cci() self.data['cci40'] = CCI40.cci() self.data['cci100'] = CCI100.cci() self.data['willr14'] = WILLR14.wr() self.data['willr28'] = WILLR28.wr() self.data['willr60'] = WILLR60.wr() self.data['bband20up'] = (BBANDS20.bollinger_hband() - close) / close self.data['bband20down'] = (close - BBANDS20.bollinger_lband()) / close self.data['stoch14'] = STOCH14.stoch() self.data['stoch28'] = STOCH28.stoch() self.data['stoch60'] = STOCH60.stoch() self.data['cmi20'] = CMI20.chaikin_money_flow() self.data['cmi40'] = CMI40.chaikin_money_flow() self.data['cmi100'] = CMI100.chaikin_money_flow() self.data['kc20up'] = (KC20.keltner_channel_hband() - close) / close self.data['kc20down'] = (close - KC20.keltner_channel_lband()) / close return self.data