def create_trade_sign(self, stock_price: pd.DataFrame) -> pd.DataFrame: stock_price = stock_price.sort_values("date") kd = StochasticOscillator( high=stock_price["max"], low=stock_price["min"], close=stock_price["close"], n=self.kdays, d_n=self.ddays, ) stock_price["K"] = kd.stoch() stock_price["D"] = kd.stoch_signal() stock_price.index = range(len(stock_price)) stock_price["signal"] = 0 stock_price.loc[stock_price["K"] <= self.kd_lower, "signal"] = 1 stock_price.loc[stock_price["K"] >= self.kd_upper, "signal"] = -1 return stock_price
def analyze(self): stochastic_df = StochasticOscillator(self.df.high, self.df.low, self.df.close, n=5) self.df["stoch_k"] = stochastic_df.stoch() self.df["stoch_d"] = stochastic_df.stoch_signal() self.df["high_close"] = ((self.df.high - self.df.close) / (self.df.high + self.df.close / 2) * 100) self.df["buy_signal"] = ( ((self.df['stoch_k'] > self.df['stoch_d']) & (self.df['stoch_k'].shift(1) < self.df['stoch_d'].shift(1))) & (self.df['stoch_d'] < 26)) self.df["stoch_buy_signal_early"] = ( (self.df['stoch_k'] < self.df['stoch_d']) & ((self.df["stoch_d"]) - self.df["stoch_k"] < 3) & (self.df['stoch_d'] < 26))
def init(self, base_data): base_data = base_data.sort_values("date") kd = StochasticOscillator( high=base_data["max"], low=base_data["min"], close=base_data["close"], n=self.kdays, d_n=self.ddays, ) base_data["K"] = kd.stoch() base_data["D"] = kd.stoch_signal() base_data.index = range(len(base_data)) base_data["signal"] = 0 base_data.loc[base_data["K"] <= self.kd_lower, "signal"] = 1 base_data.loc[base_data["K"] >= self.kd_upper, "signal"] = -1 return base_data
def add_momentum_indicators(data: pd.DataFrame) -> pd.DataFrame: """Adds the momentum indicators. Parameters ---------- data : pd.DataFrame A dataframe with daily stock values. Must include: open, high, low, close and volume. It should also be sorted in a descending manner. Returns ------- pd.DataFrame The input dataframe with the indicators added. """ rsi = RSIIndicator(data['close']) stoch_osc = StochasticOscillator(data['high'], data['low'], data['close']) data.loc[:, 'rsi'] = rsi.rsi() data.loc[:, 'stoch_osc'] = stoch_osc.stoch() data.loc[:, 'stoch_osc_signal'] = stoch_osc.stoch_signal() return data
class TestStochasticOscillator(unittest.TestCase): """ https://school.stockcharts.com/doku.php?id=technical_indicators:stochastic_oscillator_fast_slow_and_full """ _filename = 'ta/tests/data/cs-soo.csv' def setUp(self): self._df = pd.read_csv(self._filename, sep=',') self._indicator = StochasticOscillator( high=self._df['High'], low=self._df['Low'], close=self._df['Close'], n=14, d_n=3, fillna=False) def tearDown(self): del(self._df) def test_so(self): target = 'SO' result = self._indicator.stoch() pd.testing.assert_series_equal(self._df[target].tail(), result.tail(), check_names=False) def test_so_signal(self): target = 'SO_SIG' result = self._indicator.stoch_signal() pd.testing.assert_series_equal(self._df[target].tail(), result.tail(), check_names=False)
def STOCHASTIC(self, df): df_STOCH = df.copy() stoch = StochasticOscillator(df['High'], df['Low'], df['Close']) df['%K'] = stoch.stoch() df['%D'] = stoch.stoch_signal() return df
def add_momentum_ta( df: pd.DataFrame, high: str, low: str, close: str, volume: 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. volume (str): Name of 'volume' 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. """ # Relative Strength Index (RSI) df[f"{colprefix}momentum_rsi"] = RSIIndicator(close=df[close], window=14, fillna=fillna).rsi() # Stoch RSI (StochRSI) indicator_srsi = StochRSIIndicator(close=df[close], window=14, smooth1=3, smooth2=3, fillna=fillna) df[f"{colprefix}momentum_stoch_rsi"] = indicator_srsi.stochrsi() df[f"{colprefix}momentum_stoch_rsi_k"] = indicator_srsi.stochrsi_k() df[f"{colprefix}momentum_stoch_rsi_d"] = indicator_srsi.stochrsi_d() # TSI Indicator df[f"{colprefix}momentum_tsi"] = TSIIndicator(close=df[close], window_slow=25, window_fast=13, fillna=fillna).tsi() # Ultimate Oscillator df[f"{colprefix}momentum_uo"] = UltimateOscillator( high=df[high], low=df[low], close=df[close], window1=7, window2=14, window3=28, weight1=4.0, weight2=2.0, weight3=1.0, fillna=fillna, ).ultimate_oscillator() # Stoch Indicator indicator_so = StochasticOscillator( high=df[high], low=df[low], close=df[close], window=14, smooth_window=3, fillna=fillna, ) df[f"{colprefix}momentum_stoch"] = indicator_so.stoch() df[f"{colprefix}momentum_stoch_signal"] = indicator_so.stoch_signal() # Williams R Indicator df[f"{colprefix}momentum_wr"] = WilliamsRIndicator( high=df[high], low=df[low], close=df[close], lbp=14, fillna=fillna).williams_r() # Awesome Oscillator df[f"{colprefix}momentum_ao"] = AwesomeOscillatorIndicator( high=df[high], low=df[low], window1=5, window2=34, fillna=fillna).awesome_oscillator() # Rate Of Change df[f"{colprefix}momentum_roc"] = ROCIndicator(close=df[close], window=12, fillna=fillna).roc() # Percentage Price Oscillator indicator_ppo = PercentagePriceOscillator(close=df[close], window_slow=26, window_fast=12, window_sign=9, fillna=fillna) df[f"{colprefix}momentum_ppo"] = indicator_ppo.ppo() df[f"{colprefix}momentum_ppo_signal"] = indicator_ppo.ppo_signal() df[f"{colprefix}momentum_ppo_hist"] = indicator_ppo.ppo_hist() # Percentage Volume Oscillator indicator_pvo = PercentageVolumeOscillator(volume=df[volume], window_slow=26, window_fast=12, window_sign=9, fillna=fillna) df[f"{colprefix}momentum_pvo"] = indicator_pvo.pvo() df[f"{colprefix}momentum_pvo_signal"] = indicator_pvo.pvo_signal() df[f"{colprefix}momentum_pvo_hist"] = indicator_pvo.pvo_hist() if not vectorized: # KAMA df[f"{colprefix}momentum_kama"] = KAMAIndicator(close=df[close], window=10, pow1=2, pow2=30, fillna=fillna).kama() return df
def add_momentum_ta(df: pd.DataFrame, high: str, low: str, close: str, volume: str, fillna: bool = False, colprefix: str = "") -> 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 Returns: pandas.core.frame.DataFrame: Dataframe with new features. """ # Relative Strength Index (RSI) df[f'{colprefix}momentum_rsi'] = RSIIndicator(close=df[close], n=14, fillna=fillna).rsi() # TSI Indicator df[f'{colprefix}momentum_tsi'] = TSIIndicator(close=df[close], r=25, s=13, fillna=fillna).tsi() # Ultimate Oscillator df[f'{colprefix}momentum_uo'] = UltimateOscillator(high=df[high], low=df[low], close=df[close], s=7, m=14, len=28, ws=4.0, wm=2.0, wl=1.0, fillna=fillna).uo() # Stoch Indicator indicator = StochasticOscillator(high=df[high], low=df[low], close=df[close], n=14, d_n=3, fillna=fillna) df[f'{colprefix}momentum_stoch'] = indicator.stoch() df[f'{colprefix}momentum_stoch_signal'] = indicator.stoch_signal() # Williams R Indicator df[f'{colprefix}momentum_wr'] = WilliamsRIndicator(high=df[high], low=df[low], close=df[close], lbp=14, fillna=fillna).wr() # Awesome Oscillator df[f'{colprefix}momentum_ao'] = AwesomeOscillatorIndicator( high=df[high], low=df[low], s=5, len=34, fillna=fillna).ao() # KAMA df[f'{colprefix}momentum_kama'] = KAMAIndicator(close=df[close], n=10, pow1=2, pow2=30, fillna=fillna).kama() # Rate Of Change df[f'{colprefix}momentum_roc'] = ROCIndicator(close=df[close], n=12, fillna=fillna).roc() return df