
To code the Augmented Bollinger Bands — ABB in Python, we need first to code the exponential moving average, which we can see how to do below:
def ma(Data, lookback, what, where):for i in range(len(Data)):
try:
Data[i, where] = (Data[i - lookback + 1:i + 1, what].mean())except IndexError:
def ema(Data, alpha, lookback, what, where):
pass
return Data# alpha is the smoothing factor
# window is the lookback period
# what is the column that needs to have its average calculated
# where is where to put the exponential moving averagealpha = alpha / (lookback + 1.0)
beta = 1 - alpha# First value is a simple SMA
Data = ma(Data, lookback, what, where)# Calculating first EMA
# Calculating the rest of EMA
Data[lookback + 1, where] = (Data[lookback + 1, what] * alpha) + (Data[lookback, where] * beta)
for i in range(lookback + 2, len(Data)):
try:
Data[i, where] = (Data[i, what] * alpha) + (Data[i - 1, where] * beta)except IndexError:
pass
return Data
Then, we can incorporate the function above to the one below and get the Augmented Bollinger Bands.
def augmented_BollingerBands(Data, boll_lookback, standard_distance, what, high, low, where):# Calculating means
ema(Data, 2, boll_lookback, high, where)
ema(Data, 2, boll_lookback, low, where + 1)
volatility(Data, boll_lookback, high, where + 2)
volatility(Data, boll_lookback, low, where + 3)
Data[:, where + 4] = Data[:, where] + (standard_distance * Data[:, where + 2])
Data[:, where + 5] = Data[:, where + 1] - (standard_distance * Data[:, where + 3])
return Data
The below plot shows the AUDUSD hourly data with the Augmented Bollinger Bands of 20-periods and 2 standard deviation. We can abbreviate it to ABB(20, 2) to facilitate communication.