def test_mavg2(self): target = "MiddleBand" result = bollinger_mavg(close=self._df["Close"], window=20, fillna=False) pd.testing.assert_series_equal(self._df[target].tail(), result.tail(), check_names=False)
def BB_MAV(df, col_name, intervals): """ Bollinger Bands Moving Average. Reference: https://www.investopedia.com/ask/answers/112814/how-do-i-create-trading-strategy-bollinger-bands-and-moving-averages.asp """ from ta.volatility import bollinger_mavg from tqdm.auto import tqdm for interval in tqdm(intervals): df['bb_' + str(interval)] = bollinger_mavg(df[col_name], n=interval, fillna=True)
def calculate_Volitality_Indicators(): JSON_sent = request.get_json() df = pd.DataFrame(JSON_sent[0]) _, atr, bbsma, bbupper, bblower, keltnerC = JSON_sent # Average True Range if atr['displayATR']: indicator_ATR = average_true_range(high=df['high'], low=df['low'], close=df['close'], n=atr['nForATR']) df['atr'] = indicator_ATR # # Bollinger Band SMA if bbsma['displayBBSMA']: indicator_BBSMA = bollinger_mavg(close=df['close'], n=bbsma['nForBBSMA']) df['bbsma'] = indicator_BBSMA # # Bollinger Band Upper if bbupper['displayBBUpper']: indicator_BBUpper = bollinger_hband(close=df['close'], n=bbupper['nForBBUpper'], ndev=bbupper['ndevBBUpper']) df['BBupper'] = indicator_BBUpper # # Bollinger Band Lower if bblower['displayBBLower']: indicator_BBLower = bollinger_lband(close=df['close'], n=bblower['nForBBLower'], ndev=bblower['ndevBBLower']) df['BBlower'] = indicator_BBLower # # Keltner Channel Central if keltnerC['displayKeltnerC']: indicator_keltnerC = keltner_channel_mband(high=df['high'], low=df['low'], close=df['close'], n=keltnerC['nForKeltnerC']) df['keltnerC'] = indicator_keltnerC df.fillna(0, inplace=True) return (json.dumps(df.to_dict('records')))
def test_mavg2(self): target = 'MiddleBand' result = bollinger_mavg(close=self._df['Close'], n=20, fillna=False) pd.testing.assert_series_equal(self._df[target].tail(), result.tail(), check_names=False)
def Bollinger(df0, n=160): df = df0 df['bollinger_center'] = bollinger_mavg(df['close'], n, fillna=False) df['bollinger_upper'] = bollinger_hband(df['close'], n, fillna=False) df['bollinger_lower'] = bollinger_lband(df['close'], n, fillna=False) return df
def squeeze(history, sqz, symbol, t): bbh = ta.bollinger_hband(history[symbol]['close'].dropna(), n=20, ndev=2) bbl = ta.bollinger_lband(history[symbol]['close'].dropna(), n=20, ndev=2) bba = ta.bollinger_mavg(history[symbol]['close'].dropna(), n=20) kca = ta.keltner_channel_central(history[symbol]['high'], history[symbol]['low'], history[symbol]['close'], n=20) kcl = ta.keltner_channel_lband(history[symbol]['high'], history[symbol]['low'], history[symbol]['close'], n=20) kch = ta.keltner_channel_hband(history[symbol]['high'], history[symbol]['low'], history[symbol]['close'], n=20) mom = momentum.ao(history[symbol]['high'], history[symbol]['low']) #print(bbl[-1], kcl[-1], bbh[-1], kch[-1]) sqzon = (bbl[-1] > kcl[-1]) and (bbh[-1] < kch[-1]) sqzoff = bbl[-1] < kcl[-1] and bbh[-1] > kch[-1] nosqz = sqzon == False and sqzoff == False """ value = (Highest[lengthKC](high)+Lowest[lengthKC](low)+average[lengthKC](close))/3 val = linearregression[lengthKC](close-value) val = (max(history[symbol]['high'][-20:-1]) + min(history[symbol]['low'][-20:-1]) + history[symbol]['close'].mean())/3.0 v = lr(history[symbol]['close'] - val) """ flag = -1 #print("hi") if sqz[symbol][0] and sqzon: pass if sqz[symbol][0] and sqzoff: sqz[symbol][0] = False sqz[symbol][1] = True flag = 0 if sqz[symbol][1] and sqzoff: pass if sqz[symbol][1] and sqzon: sqz[symbol][1] = False sqz[symbol][0] = True flag = 1 if sqz[symbol][2] and sqzon: sqz[symbol][2] = False sqz[symbol][0] = True flag = 1 if sqz[symbol][2] and sqzoff: sqz[symbol][2] = False sqz[symbol][1] = True #print("sqeeze is OFF") #print(time.time()) flag = -1 if flag == -1: #print('No Change') pass if flag == 0: send_message(symbol, t + " pop : " + str(mom[-1])) if flag == 1: send_message(symbol, "Squeeze On " + t)
def __init__(self, N_day): super().__init__(indicator=lambda x: bollinger_mavg(x, N_day)) self.lookback_window = N_day