class TestPSARIndicator(unittest.TestCase): """ https://school.stockcharts.com/doku.php?id=technical_indicators:parabolic_sar """ _filename = 'ta/tests/data/cs-psar.csv' def setUp(self): self._df = pd.read_csv(self._filename, sep=',') self._indicator = PSARIndicator(high=self._df['High'], low=self._df['Low'], close=self._df['Close'], fillna=False) def tearDown(self): del (self._df) def test_psar_up(self): target = 'psar_up' result = self._indicator.psar_up() pd.testing.assert_series_equal(self._df[target].tail(), result.tail(), check_names=False) def test_psar_down(self): target = 'psar_down' result = self._indicator.psar_down() pd.testing.assert_series_equal(self._df[target].tail(), result.tail(), check_names=False) def test_psar_up_indicator(self): target = 'psar_up_ind' result = self._indicator.psar_up_indicator() pd.testing.assert_series_equal(self._df[target].tail(), result.tail(), check_names=False) def test_psar_down_indicator(self): target = 'psar_down_ind' result = self._indicator.psar_down_indicator() pd.testing.assert_series_equal(self._df[target].tail(), result.tail(), check_names=False)
def add_trend_ta( df: pd.DataFrame, high: str, low: str, close: 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. 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. """ # MACD indicator_macd = MACD(close=df[close], window_slow=26, window_fast=12, window_sign=9, fillna=fillna) df[f"{colprefix}trend_macd"] = indicator_macd.macd() df[f"{colprefix}trend_macd_signal"] = indicator_macd.macd_signal() df[f"{colprefix}trend_macd_diff"] = indicator_macd.macd_diff() # SMAs df[f"{colprefix}trend_sma_fast"] = SMAIndicator( close=df[close], window=12, fillna=fillna).sma_indicator() df[f"{colprefix}trend_sma_slow"] = SMAIndicator( close=df[close], window=26, fillna=fillna).sma_indicator() # EMAs df[f"{colprefix}trend_ema_fast"] = EMAIndicator( close=df[close], window=12, fillna=fillna).ema_indicator() df[f"{colprefix}trend_ema_slow"] = EMAIndicator( close=df[close], window=26, fillna=fillna).ema_indicator() # Vortex Indicator indicator_vortex = VortexIndicator(high=df[high], low=df[low], close=df[close], window=14, fillna=fillna) df[f"{colprefix}trend_vortex_ind_pos"] = indicator_vortex.vortex_indicator_pos( ) df[f"{colprefix}trend_vortex_ind_neg"] = indicator_vortex.vortex_indicator_neg( ) df[f"{colprefix}trend_vortex_ind_diff"] = indicator_vortex.vortex_indicator_diff( ) # TRIX Indicator df[f"{colprefix}trend_trix"] = TRIXIndicator(close=df[close], window=15, fillna=fillna).trix() # Mass Index df[f"{colprefix}trend_mass_index"] = MassIndex(high=df[high], low=df[low], window_fast=9, window_slow=25, fillna=fillna).mass_index() # DPO Indicator df[f"{colprefix}trend_dpo"] = DPOIndicator(close=df[close], window=20, fillna=fillna).dpo() # KST Indicator indicator_kst = KSTIndicator( close=df[close], roc1=10, roc2=15, roc3=20, roc4=30, window1=10, window2=10, window3=10, window4=15, nsig=9, fillna=fillna, ) df[f"{colprefix}trend_kst"] = indicator_kst.kst() df[f"{colprefix}trend_kst_sig"] = indicator_kst.kst_sig() df[f"{colprefix}trend_kst_diff"] = indicator_kst.kst_diff() # Ichimoku Indicator indicator_ichi = IchimokuIndicator( high=df[high], low=df[low], window1=9, window2=26, window3=52, visual=False, fillna=fillna, ) df[f"{colprefix}trend_ichimoku_conv"] = indicator_ichi.ichimoku_conversion_line( ) df[f"{colprefix}trend_ichimoku_base"] = indicator_ichi.ichimoku_base_line() df[f"{colprefix}trend_ichimoku_a"] = indicator_ichi.ichimoku_a() df[f"{colprefix}trend_ichimoku_b"] = indicator_ichi.ichimoku_b() # Schaff Trend Cycle (STC) df[f"{colprefix}trend_stc"] = STCIndicator( close=df[close], window_slow=50, window_fast=23, cycle=10, smooth1=3, smooth2=3, fillna=fillna, ).stc() if not vectorized: # Average Directional Movement Index (ADX) indicator_adx = ADXIndicator(high=df[high], low=df[low], close=df[close], window=14, fillna=fillna) df[f"{colprefix}trend_adx"] = indicator_adx.adx() df[f"{colprefix}trend_adx_pos"] = indicator_adx.adx_pos() df[f"{colprefix}trend_adx_neg"] = indicator_adx.adx_neg() # CCI Indicator df[f"{colprefix}trend_cci"] = CCIIndicator( high=df[high], low=df[low], close=df[close], window=20, constant=0.015, fillna=fillna, ).cci() # Ichimoku Visual Indicator indicator_ichi_visual = IchimokuIndicator( high=df[high], low=df[low], window1=9, window2=26, window3=52, visual=True, fillna=fillna, ) df[f"{colprefix}trend_visual_ichimoku_a"] = indicator_ichi_visual.ichimoku_a( ) df[f"{colprefix}trend_visual_ichimoku_b"] = indicator_ichi_visual.ichimoku_b( ) # Aroon Indicator indicator_aroon = AroonIndicator(close=df[close], window=25, fillna=fillna) df[f"{colprefix}trend_aroon_up"] = indicator_aroon.aroon_up() df[f"{colprefix}trend_aroon_down"] = indicator_aroon.aroon_down() df[f"{colprefix}trend_aroon_ind"] = indicator_aroon.aroon_indicator() # PSAR Indicator indicator_psar = PSARIndicator( high=df[high], low=df[low], close=df[close], step=0.02, max_step=0.20, fillna=fillna, ) # df[f'{colprefix}trend_psar'] = indicator.psar() df[f"{colprefix}trend_psar_up"] = indicator_psar.psar_up() df[f"{colprefix}trend_psar_down"] = indicator_psar.psar_down() df[f"{colprefix}trend_psar_up_indicator"] = indicator_psar.psar_up_indicator( ) df[f"{colprefix}trend_psar_down_indicator"] = indicator_psar.psar_down_indicator( ) return df