def techIndicator(df1): #OFtrader # Initialize Bollinger Bands Indicator from ta.volatility import BollingerBands indicator_bb = BollingerBands(close=df1["Close"], window=10, window_dev=2) # Add Bollinger Bands features df1['bb_bbh'] = indicator_bb.bollinger_hband() df1['bb_bbl'] = indicator_bb.bollinger_lband() # Initialize Bollinger Bands Indicator from ta.trend import PSARIndicator indicator_SAR = PSARIndicator(high=df1["high"], low=df1["low"], close=df1["Close"]) # Add Bollinger Bands features df1['sar_high'] = indicator_SAR.psar_up() df1['sar_low'] = indicator_SAR.psar_down() from ta.trend import EMAIndicator indicator_EMA = EMAIndicator(close=df1["Close"], window=7) df1['Media7'] = indicator_EMA.ema_indicator() df1['sar_low'] = df1['sar_low'].fillna(0) df1['sar_high'] = df1['sar_high'].fillna(0) df1['Distancia_M7'] = df1['Close'] / df1['Media7'] df1['Distancia_BBH'] = df1['Close'] / df1['bb_bbh'] df1['Distancia_BBL'] = df1['Close'] / df1['bb_bbl'] df1['Distancia_SAR'] = np.where(df1['sar_high'] > 0, df1['Close'] / df1['sar_high'], df1['sar_low'] / df1['Close']) df1['posicao_sar'] = np.where(df1['sar_high'] > 0, '1', '0') corte = 3 df1["Distancia_M7"] = pd.qcut(df1["Distancia_M7"], corte, labels=False) df1["Distancia_BBH"] = pd.qcut(df1["Distancia_BBH"], corte, labels=False) df1["Distancia_BBL"] = pd.qcut(df1["Distancia_BBL"], 15, labels=False) df1["Distancia_SAR"] = pd.qcut(df1["Distancia_SAR"], 15, labels=False) # # Padrão Bom com M7: 3 - BBH: 3 - BBL: 15 - DSAR: 15 df1 = df1.drop(["sar_high", "sar_low", "bb_bbh", "bb_bbl", "Media7"], axis=1) #df1.tail(50) return df1
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