def add_volatility_indicators(data: pd.DataFrame) -> pd.DataFrame: """Adds the volatility 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. """ bb = BollingerBands(data['close']) data.loc[:, 'bollinger_hband'] = bb.bollinger_hband() data.loc[:, 'bollinger_hband_indicator'] = bb.bollinger_hband_indicator() data.loc[:, 'bollinger_lband'] = bb.bollinger_lband() data.loc[:, 'bollinger_lband_indicator'] = bb.bollinger_lband_indicator() data.loc[:, 'bollinger_mavg'] = bb.bollinger_mavg() data.loc[:, 'bollinger_pband'] = bb.bollinger_pband() data.loc[:, 'bollinger_wband'] = bb.bollinger_wband() return data
def get_signal(self, input_df, ticker, run_id): df = input_df.copy() indicator_bb = BollingerBands( close=df["Close"], window=self.indicator["window"], window_dev=self.indicator["window_dev"], fillna=self.indicator["fillna"], ) # Add Bollinger Bands features df["bb_bbm"] = indicator_bb.bollinger_mavg() df["bb_bbh"] = indicator_bb.bollinger_hband() df["bb_bbl"] = indicator_bb.bollinger_lband() df["bb_bbhi"] = indicator_bb.bollinger_hband_indicator() df["bb_bbli"] = indicator_bb.bollinger_lband_indicator() df["bb_bbp"] = indicator_bb.bollinger_pband() row = df.iloc[-1] if row.bb_bbhi.item(): sell_signal = { "ticker": ticker, "datetime": row.Date, "indicator": self.name, "param": self.param, "reason": "High BollingerBand percentage - currently at {}%".format( int(row.bb_bbp.item() * 100.0)), "image": self.draw_image(df, ticker, run_id), } else: sell_signal = None if row.bb_bbli.item(): buy_signal = { "ticker": ticker, "datetime": row.Date, "indicator": self.name, "param": self.param, "reason": "Low BollingerBand percentage - currently at {}%".format( int(row.bb_bbp.item() * 100.0)), "image": self.draw_image(df, ticker, run_id), } else: buy_signal = None return buy_signal, sell_signal
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
class TestBollingerBands(unittest.TestCase): """ https://school.stockcharts.com/doku.php?id=technical_indicators:bollinger_bands """ _filename = 'ta/tests/data/cs-bbands.csv' def setUp(self): self._df = pd.read_csv(self._filename, sep=',') self._indicator = BollingerBands(close=self._df['Close'], n=20, ndev=2, fillna=False) def tearDown(self): del (self._df) def test_mavg(self): target = 'MiddleBand' result = self._indicator.bollinger_mavg() pd.testing.assert_series_equal(self._df[target].tail(), result.tail(), check_names=False) def test_hband(self): target = 'HighBand' result = self._indicator.bollinger_hband() pd.testing.assert_series_equal(self._df[target].tail(), result.tail(), check_names=False) def test_lband(self): target = 'LowBand' result = self._indicator.bollinger_lband() pd.testing.assert_series_equal(self._df[target].tail(), result.tail(), check_names=False) def test_wband(self): target = 'WidthBand' result = self._indicator.bollinger_wband() pd.testing.assert_series_equal(self._df[target].tail(), result.tail(), check_names=False) def test_hband_indicator(self): target = 'CrossUp' result = self._indicator.bollinger_hband_indicator() pd.testing.assert_series_equal(self._df[target].tail(), result.tail(), check_names=False) def test_lband_indicator(self): target = 'CrossDown' result = self._indicator.bollinger_lband_indicator() pd.testing.assert_series_equal(self._df[target].tail(), result.tail(), check_names=False)
def bollinger_bands(df): indicator_bb = BollingerBands(close=df["Close"]) df['bb_bbh'] = indicator_bb.bollinger_hband() df['bb_bbl'] = indicator_bb.bollinger_lband() df['bb_avg'] = indicator_bb.bollinger_mavg() df['bb_bbh_ind'] = indicator_bb.bollinger_hband_indicator() df['bb_bbl_ind'] = indicator_bb.bollinger_lband_indicator() df['bb_pband'] = indicator_bb.bollinger_pband() df['bb_wband'] = indicator_bb.bollinger_wband()
def bollinger_bands(data, days=20, std=2, price="Close"): df = data # Initialize Bollinger Bands Indicator indicator_bb = BollingerBands(close=df[price], window=days, window_dev=std) # Add Bollinger Bands features df["bb_ma"] = indicator_bb.bollinger_mavg() df["bb_high"] = indicator_bb.bollinger_hband() df["bb_low"] = indicator_bb.bollinger_lband() # Add Bollinger Band high indicator df["bb_bbhi"] = indicator_bb.bollinger_hband_indicator() # Add Bollinger Band low indicator df["bb_bbli"] = indicator_bb.bollinger_lband_indicator() return df
def analysis(df, ma_f, ma_s, period): df["ema_f"] = ta.ema(high=df.high, low=df.low, close=df.close, length=ma_f) df["ema_s"] = ta.ema(high=df.high, low=df.low, close=df.close, length=ma_s) df.fillna(0, inplace=True) df['s'] = (df['ema_s'] - df['ema_s'].shift(1)) >= 0 df['f'] = (df['ema_f'] - df['ema_f'].shift(1)) >= 0 df['buy_ema'] = df['ema_f'] > df['ema_s'] df['sell_ema'] = df['ema_f'] <= df['ema_s'] df['buy_change'] = (df['buy_ema'] != df['buy_ema'].shift(1)) & df['buy_ema'] df['sell_change'] = (df['sell_ema'] != df['sell_ema'].shift(1)) & df['sell_ema'] # df["RSI"] = ta.rsi(high=df.high, low=df.low, close=df.close, length=period) # df['RSIs'] = (df['RSI'] - df['RSI'].shift(1)) >= 0 # df['RSI_ups'] = df.groupby( # (df['RSIs'] != df['RSIs'].shift(1)).cumsum()).cumcount() + 1 # df['adx'] = ta.adx(high=df.high, low=df.low, close=df.close, length=period)['ADX_%s' % period] # df['adx_s'] = (df['adx'] - df['adx'].shift(1)) >= 0 # df['adx_ups'] = df.groupby( # (df['adx_s'] != df['adx_s'].shift(1)).cumsum()).cumcount() + 1 # df['ATR'] = df.ta.atr() indicator_bb = BollingerBands(close=df.close, window=10, window_dev=1.8) df['bb_bbm'] = indicator_bb.bollinger_mavg() df['b_m'] = (df['bb_bbm'] - df['bb_bbm'].shift(1)) >= 0 df['bb_bbh'] = indicator_bb.bollinger_hband() df['bb_bbl'] = indicator_bb.bollinger_lband() df['bb_bbhi'] = indicator_bb.bollinger_hband_indicator() df['bb_bbli'] = indicator_bb.bollinger_lband_indicator() df['pvt'] = ta.pvt(close=df.close, volume=df.volume) df['pvt_t'] = (df['pvt'] - df['pvt'].shift(1)) >= 0 df['close_variation'] = df['close'] - df['close'].shift(1) # # df = ema(df, ma_f, ma_s) df = rsi(df, period) df = momentum(df) df['trend'] = df['momentum_s'] & df['RSIs'] return df
def transform_one(self, entity_id, df: pd.DataFrame) -> pd.DataFrame: indicator_bb = BollingerBands(close=df["close"], window=20, window_dev=2) # Add Bollinger Bands features df["bb_bbm"] = indicator_bb.bollinger_mavg() df["bb_bbh"] = indicator_bb.bollinger_hband() df["bb_bbl"] = indicator_bb.bollinger_lband() # Add Bollinger Band high indicator df["bb_bbhi"] = indicator_bb.bollinger_hband_indicator() # Add Bollinger Band low indicator df["bb_bbli"] = indicator_bb.bollinger_lband_indicator() # Add Width Size Bollinger Bands df["bb_bbw"] = indicator_bb.bollinger_wband() # Add Percentage Bollinger Bands df["bb_bbp"] = indicator_bb.bollinger_pband() return df
def __init__(self, symbols): # data = json.loads(symbols) # df_stock = pd.json_normalize(symbols) # df_stock = pd.read_csv(fn,names = ['sym']).drop_duplicates() df_stock = pd.DataFrame(symbols) ls_stock = df_stock['sym'].to_list() df_stock = df_stock.reset_index() df_stock.columns = ['sort', 'sym'] df_stock.head() # In[3]: start = dt.date.today() + relativedelta(days=-150) end = dt.date.today() + relativedelta(days=-0) ls_tickers = ls_stock ls_df = [] for ticker in ls_tickers: try: df = web.DataReader(ticker, 'yahoo', start, end) except Exception as e: print(str(e)) continue df['sym'] = ticker ls_df.append(df.copy()) df_price = pd.concat(ls_df).reset_index() df_price.columns = [ 'dte', 'hgh', 'low', 'opn', 'cls', 'vol', 'cls_adj', 'sym' ] df_price.sort_values(['sym', 'dte'], inplace=True) df_price = df_price[['dte', 'sym', 'hgh', 'low', 'cls', 'vol']].copy() df_price['curr'] = end df_price['curr'] = pd.to_datetime(df_price['curr']) df_price['dte'] = pd.to_datetime(df_price['dte']) df_price['ndays'] = (df_price['curr'] - df_price['dte']).dt.days df_price['ndays'] = df_price.groupby(['sym'])['ndays'].rank() df_price[df_price['sym'] == 'SPY'].head() # In[4]: ls_df = [] ls_tickers = ls_stock for ticker in ls_tickers: #df = dropna(df_price[df_price['sym']==ticker]) df = df_price[df_price['sym'] == ticker].copy() indicator_bb = BollingerBands(close=df['cls'], window=20, window_dev=2) indicator_macd = MACD(close=df['cls'], window_fast=12, window_slow=26, window_sign=9) indicator_rsi14 = RSIIndicator(close=df['cls'], window=14) indicator_cci20 = cci(high=df['hgh'], low=df['low'], close=df['cls'], window=20, constant=0.015) indicator_obv = OnBalanceVolumeIndicator(close=df['cls'], volume=df['vol'], fillna=True) indicator_vol_sma20 = SMAIndicator(close=df['vol'], window=20) indicator_ema03 = EMAIndicator(close=df['cls'], window=3) indicator_ema05 = EMAIndicator(close=df['cls'], window=5) indicator_ema08 = EMAIndicator(close=df['cls'], window=8) indicator_ema10 = EMAIndicator(close=df['cls'], window=10) indicator_ema12 = EMAIndicator(close=df['cls'], window=12) indicator_ema15 = EMAIndicator(close=df['cls'], window=15) indicator_ema30 = EMAIndicator(close=df['cls'], window=30) indicator_ema35 = EMAIndicator(close=df['cls'], window=35) indicator_ema40 = EMAIndicator(close=df['cls'], window=40) indicator_ema45 = EMAIndicator(close=df['cls'], window=45) indicator_ema50 = EMAIndicator(close=df['cls'], window=50) indicator_ema60 = EMAIndicator(close=df['cls'], window=60) # Add Bollinger Band high indicator df['bb_bbhi'] = indicator_bb.bollinger_hband_indicator() # Add Bollinger Band low indicator df['bb_bbli'] = indicator_bb.bollinger_lband_indicator() #df['macd'] = indicator_macd.macd() df['macd'] = indicator_macd.macd_diff() #df['macd_signal'] = indicator_macd.macd_signal() df['obv'] = indicator_obv.on_balance_volume() df['vol_sma20'] = indicator_vol_sma20.sma_indicator() df['ema03'] = indicator_ema03.ema_indicator() df['ema05'] = indicator_ema05.ema_indicator() df['ema08'] = indicator_ema08.ema_indicator() df['ema10'] = indicator_ema10.ema_indicator() df['ema12'] = indicator_ema12.ema_indicator() df['ema15'] = indicator_ema15.ema_indicator() df['ema30'] = indicator_ema30.ema_indicator() df['ema35'] = indicator_ema35.ema_indicator() df['ema40'] = indicator_ema40.ema_indicator() df['ema45'] = indicator_ema45.ema_indicator() df['ema50'] = indicator_ema50.ema_indicator() df['ema60'] = indicator_ema60.ema_indicator() df['rsi14'] = indicator_rsi14.rsi() df['cci20'] = indicator_cci20 ls_df.append(df.copy()) df = pd.concat(ls_df) df['score_vol_sma20'] = df[['vol', 'vol_sma20']].apply(lambda x: x[0] / x[1], axis=1) df['emash_min'] = df[[ 'ema03', 'ema05', 'ema08', 'ema10', 'ema12', 'ema15' ]].min(axis=1) df['emash_max'] = df[[ 'ema03', 'ema05', 'ema08', 'ema10', 'ema12', 'ema15' ]].max(axis=1) df['emash_avg'] = df[[ 'ema03', 'ema05', 'ema08', 'ema10', 'ema12', 'ema15' ]].mean(axis=1) #df['score_short'] = df[['cls','emash_min','emash_max','emash_min']].apply(lambda x: 100 * (x[0]-x[1])/(x[2]-x[3]),axis=1) df['emalg_min'] = df[[ 'ema30', 'ema35', 'ema40', 'ema45', 'ema50', 'ema60' ]].min(axis=1) df['emalg_max'] = df[[ 'ema30', 'ema35', 'ema40', 'ema45', 'ema50', 'ema60' ]].max(axis=1) df['emalg_avg'] = df[[ 'ema30', 'ema35', 'ema40', 'ema45', 'ema50', 'ema60' ]].mean(axis=1) #df['score_long'] = df[['cls','emalg_min','emalg_max','emalg_min']].apply(lambda x: 100 * (x[0]-x[1])/(x[2]-x[3]),axis=1) df['ema_min'] = df[[ 'ema03', 'ema05', 'ema08', 'ema10', 'ema12', 'ema15', 'ema30', 'ema35', 'ema40', 'ema45', 'ema50', 'ema60' ]].min(axis=1) df['ema_max'] = df[[ 'ema03', 'ema05', 'ema08', 'ema10', 'ema12', 'ema15', 'ema30', 'ema35', 'ema40', 'ema45', 'ema50', 'ema60' ]].max(axis=1) df['score_ovlp_ema'] = df[[ 'emash_min', 'emalg_max', 'ema_max', 'ema_min' ]].apply(lambda x: 100 * (x[0] - x[1]) / (x[2] - x[3]), axis=1) df = pd.merge(df_stock, df, on=['sym'], how='inner').sort_values(['sort', 'ndays']) decimals = pd.Series([1, 0, 0, 2, 0, 0, 2, 0, 0, 0, 0], index=[ 'cls', 'ndays', 'vol', 'score_vol_sma20', 'bb_bbhi', 'bb_bbli', 'macd', 'obv', 'rsi14', 'cci20', 'score_ovlp_ema' ]) cols = [ 'ndays', 'dte', 'sort', 'sym', 'cls', 'vol', 'score_vol_sma20', 'bb_bbhi', 'bb_bbli', 'macd', 'obv', 'rsi14', 'cci20', 'score_ovlp_ema' ] df = df[df['ndays'] <= 10][cols].round(decimals).copy() print(df['score_ovlp_ema'].min(), df['score_ovlp_ema'].max()) df[df['sym'] == 'QQQ'].head(50) self.df = df
] # features_considered = ['Close', 'MA_short', 'MA_long', 'Wilders_EMA'] # Initialize Bollinger Bands Indicator e = EquityData('data/SPY.csv', 'SPY') indicator_bb = BollingerBands(close=e.close(), n=20, ndev=2) e.data['MA_short'] = moving_average(e, window=history_size) e.data['MA_long'] = moving_average(e, window=5) e.data['Wilders_EMA'] = e.close().ewm(alpha=1 / history_size, adjust=False).mean() # Add Bollinger Bands features e.data['bb_bbm'] = indicator_bb.bollinger_mavg() e.data['bb_bbh'] = indicator_bb.bollinger_hband() e.data['bb_bbl'] = indicator_bb.bollinger_lband() # Add Bollinger Band high indicator e.data['bb_bbhi'] = indicator_bb.bollinger_hband_indicator() # Add Bollinger Band low indicator e.data['bb_bbli'] = indicator_bb.bollinger_lband_indicator() e.data = e.data[21:] EVALUATION_INTERVAL = int(e.data.shape[0] / BATCH_SIZE) * 2 features = e.data[features_considered] assert (list(features)[0] == 'Close') features.index = e.date() # features.plot(subplots=True) # plt.show() dataset = features.values x_train_single, y_train_single, x_val_single, y_val_single = split_multivariate( dataset,
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 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 GetPriceDataFromExchangeFinnHub(event, context): retVal = {} retVal["data"] = [] # For debugging the input, write the EVENT to CloudWatch logs print(json.dumps(event)) # Data is sent to Lambda via a HTTPS POST call. We want to get to the payload send by Snowflake event_body = event["body"] payload = json.loads(event_body) for row in payload["data"]: sflkRowRef = row[ 0] # This is how Snowflake keeps track of data as it gets returned symbol = row[ 1] # The data passed in from Snowflake that the input row contains. fromDate = row[2] toDate = row[3] # Will return URL without token to Snowflake for tracking URL = f'https://finnhub.io/api/v1/stock/candle?symbol={symbol}&resolution=D&from={fromDate}&to={toDate}' # Add our FinnHubAPI Key to the end of the URL. # This is in a new variable which will not be returned to Snowflake URLWithToken = f'{URL}&token={FinnHubAPI.TOKEN}' # GET data from the API httpData = requests.get(url=URLWithToken).json() # Convert to Pandas DataFrame df = pd.DataFrame(httpData) # Add the column names print("Adding column names") df.columns = [ "Close", "High", "Low", "Open", "Status", "OpenTime", "Volume" ] # Set DateTime columns to correct type df['OpenTime'] = pd.to_datetime(df['OpenTime'], unit='ms') df['Open'] = df['Open'].astype(float) df['High'] = df['High'].astype(float) df['Low'] = df['Low'].astype(float) df['Close'] = df['Close'].astype(float) df['Volume'] = df['Volume'].astype(float) # Clean NaN values print("Cleaning NA values") df = dropna(df) # Calculate the Bollinger Bands indicator indicator_bb = BollingerBands(close=df["Close"], n=20, ndev=2) df['bb_bbm'] = indicator_bb.bollinger_mavg() df['bb_bbh'] = indicator_bb.bollinger_hband() df['bb_bbl'] = indicator_bb.bollinger_lband() df['bb_bbhi'] = indicator_bb.bollinger_hband_indicator() df['bb_bbli'] = indicator_bb.bollinger_lband_indicator() df['bb_bbw'] = indicator_bb.bollinger_wband() df['bb_bbp'] = indicator_bb.bollinger_pband() print("converting OHLC pandas to JSON. This does it as a string") buffer = df.to_json(orient="records") print("Interpret the JSON string into a dictionary for output") jsonResponse = json.loads(buffer) # Prepare the output response response = {} response["url"] = URL response["response"] = jsonResponse retVal["data"].append([sflkRowRef, response]) # For debugging the output, write the RETurn VALue to CloudWatch logs # print(json.dumps(retVal)) return retVal