def STOCHRSI(self, df): #momentum ''' data range (0,100) ''' df_STRSI = df.copy() strsi = StochRSIIndicator(df['Close']) df['storsi'] = strsi.stochrsi() df['%K'] = strsi.stochrsi_k() df['%D'] = df['%K'].ewm(span=3).mean() # df['STOCHRSI'] = np.where((df_STRSI['%K']>df_STRSI['%D'])&(df_STRSI['%K'].shift()<df_STRSI['%D'].shift())&(df_STRSI['%K']<20),'buy', # (np.where((df_STRSI['%K'] < df_STRSI['%D']) & (df_STRSI['%K'].shift()>df_STRSI['%D'].shift()) & (df_STRSI['%K']>80),'sell','wait or hold'))) return df
def handle(self, *args, **options): # import pdb # pdb.set_trace() if not options['update']: NSETechnical.objects.all().delete() symbols = Symbol.objects.all() for symbol in symbols: nse_history_data = NSEHistoricalData.objects.filter( symbol__symbol_name=symbol).order_by('timestamp') if not nse_history_data: continue nse_technical = pd.DataFrame( list( nse_history_data.values('timestamp', 'open', 'high', 'low', 'close', 'total_traded_quantity'))) ''' Moving average convergence divergence ''' indicator_macd = MACD(close=nse_technical['close'], window_slow=26, window_fast=12, window_sign=9, fillna=False) nse_technical["trend_macd"] = indicator_macd.macd() nse_technical["trend_macd_signal"] = indicator_macd.macd_signal() nse_technical["trend_macd_diff"] = indicator_macd.macd_diff() ''' Simple Moving Average ''' nse_technical["trend_sma_fast"] = SMAIndicator( close=nse_technical['close'], window=12, fillna=False).sma_indicator() nse_technical["trend_sma_slow"] = SMAIndicator( close=nse_technical['close'], window=26, fillna=False).sma_indicator() ''' Exponential Moving Average ''' nse_technical["trend_ema_fast"] = EMAIndicator( close=nse_technical['close'], window=12, fillna=False).ema_indicator() nse_technical["trend_ema_slow"] = EMAIndicator( close=nse_technical['close'], window=26, fillna=False).ema_indicator() ''' Ichimoku Indicator ''' indicator_ichi = IchimokuIndicator( high=nse_technical['high'], low=nse_technical['low'], window1=9, window2=26, window3=52, visual=False, fillna=False, ) nse_technical[ "trend_ichimoku_conv"] = indicator_ichi.ichimoku_conversion_line( ) nse_technical[ "trend_ichimoku_base"] = indicator_ichi.ichimoku_base_line() nse_technical["trend_ichimoku_a"] = indicator_ichi.ichimoku_a() nse_technical["trend_ichimoku_b"] = indicator_ichi.ichimoku_b() indicator_ichi_visual = IchimokuIndicator( high=nse_technical['high'], low=nse_technical['low'], window1=9, window2=26, window3=52, visual=True, fillna=False, ) nse_technical[ "trend_visual_ichimoku_a"] = indicator_ichi_visual.ichimoku_a( ) nse_technical[ "trend_visual_ichimoku_b"] = indicator_ichi_visual.ichimoku_b( ) ''' Bollinger Band ''' indicator_bb = BollingerBands(close=nse_technical['close'], window=20, window_dev=2, fillna=False) nse_technical["volatility_bbm"] = indicator_bb.bollinger_mavg() nse_technical["volatility_bbh"] = indicator_bb.bollinger_hband() nse_technical["volatility_bbl"] = indicator_bb.bollinger_lband() nse_technical["volatility_bbw"] = indicator_bb.bollinger_wband() nse_technical["volatility_bbp"] = indicator_bb.bollinger_pband() nse_technical[ "volatility_bbhi"] = indicator_bb.bollinger_hband_indicator() nse_technical[ "volatility_bbli"] = indicator_bb.bollinger_lband_indicator() ''' Accumulation Distribution Index ''' nse_technical["volume_adi"] = AccDistIndexIndicator( high=nse_technical['high'], low=nse_technical['low'], close=nse_technical['close'], volume=nse_technical['total_traded_quantity'], fillna=False).acc_dist_index() ''' Money Flow Index ''' nse_technical["volume_mfi"] = MFIIndicator( high=nse_technical['high'], low=nse_technical['low'], close=nse_technical['close'], volume=nse_technical['total_traded_quantity'], window=14, fillna=False, ).money_flow_index() ''' Relative Strength Index (RSI) ''' nse_technical["momentum_rsi"] = RSIIndicator( close=nse_technical['close'], window=14, fillna=False).rsi() ''' Stoch RSI (StochRSI) ''' indicator_srsi = StochRSIIndicator(close=nse_technical['close'], window=14, smooth1=3, smooth2=3, fillna=False) nse_technical["momentum_stoch_rsi"] = indicator_srsi.stochrsi() nse_technical["momentum_stoch_rsi_k"] = indicator_srsi.stochrsi_k() nse_technical["momentum_stoch_rsi_d"] = indicator_srsi.stochrsi_d() nse_technical.replace({np.nan: None}, inplace=True) nse_technical.replace([np.inf, -np.inf], None, inplace=True) list_to_create = [] list_to_update = [] for index in range(len(nse_history_data) - 1, -1, -1): data = nse_history_data[index] if data.technicals: break technical = NSETechnical( nse_historical_data=data, trend_macd=nse_technical['trend_macd'][index], trend_macd_signal=nse_technical['trend_macd_signal'] [index], trend_macd_diff=nse_technical['trend_macd_diff'][index], trend_sma_fast=nse_technical['trend_sma_fast'][index], trend_sma_slow=nse_technical['trend_sma_slow'][index], trend_ema_fast=nse_technical['trend_ema_fast'][index], trend_ema_slow=nse_technical['trend_ema_slow'][index], trend_ichimoku_conv=nse_technical['trend_ichimoku_conv'] [index], trend_ichimoku_base=nse_technical['trend_ichimoku_base'] [index], trend_ichimoku_a=nse_technical['trend_ichimoku_a'][index], trend_ichimoku_b=nse_technical['trend_ichimoku_b'][index], trend_visual_ichimoku_a=nse_technical[ 'trend_visual_ichimoku_a'][index], trend_visual_ichimoku_b=nse_technical[ 'trend_visual_ichimoku_b'][index], volatility_bbm=nse_technical['volatility_bbm'][index], volatility_bbh=nse_technical['volatility_bbh'][index], volatility_bbl=nse_technical['volatility_bbl'][index], volatility_bbw=nse_technical['volatility_bbw'][index], volatility_bbp=nse_technical['volatility_bbp'][index], volatility_bbhi=nse_technical['volatility_bbhi'][index], volatility_bbli=nse_technical['volatility_bbli'][index], volume_adi=nse_technical['volume_adi'][index], volume_mfi=nse_technical['volume_mfi'][index], momentum_rsi=nse_technical['momentum_rsi'][index], momentum_stoch_rsi=nse_technical['momentum_stoch_rsi'] [index], momentum_stoch_rsi_k=nse_technical['momentum_stoch_rsi_k'] [index], momentum_stoch_rsi_d=nse_technical['momentum_stoch_rsi_d'] [index]) data.technicals = True list_to_update.append(data) list_to_create.append(technical) NSETechnical.objects.bulk_create(list_to_create) NSEHistoricalData.objects.bulk_update(list_to_update, ['technicals']) print(f"Technicals updated for {symbol}")
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() # Stoch RSI (StochRSI) indicator = StochRSIIndicator(close=df[close], n=14, d1=3, d2=3, fillna=fillna) df[f'{colprefix}momentum_stoch_rsi'] = indicator.stochrsi() df[f'{colprefix}momentum_stoch_rsi_k'] = indicator.stochrsi_k() df[f'{colprefix}momentum_stoch_rsi_d'] = indicator.stochrsi_d() # 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() # Percentage Price Oscillator indicator = PercentagePriceOscillator(close=df[close], n_slow=26, n_fast=12, n_sign=9, fillna=fillna) df[f'{colprefix}momentum_ppo'] = indicator.ppo() df[f'{colprefix}momentum_ppo_signal'] = indicator.ppo_signal() df[f'{colprefix}momentum_ppo_hist'] = indicator.ppo_hist() # Percentage Volume Oscillator indicator = PercentageVolumeOscillator(volume=df[volume], n_slow=26, n_fast=12, n_sign=9, fillna=fillna) df[f'{colprefix}momentum_ppo'] = indicator.pvo() df[f'{colprefix}momentum_ppo_signal'] = indicator.pvo_signal() df[f'{colprefix}momentum_ppo_hist'] = indicator.pvo_hist() return df