Get startedGet started for free

Implement Bollinger Bands

Bollinger Bands are envelopes plotted above and below a simple moving average of the price. Because the distance of the bands is based on the standard deviation, they adjust to volatility swings in the underlying price.

To better understand the impact of standard deviation specification on Bollinger bands, you will implement and plot two sets of Bollinger Bands on the same dataset.

You will use historical Bitcoin price data, which has been preloaded as bitcoin_data. The talib library has also been imported for you.

This exercise is part of the course

Financial Trading in Python

View Course

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Define the Bollinger Bands with 1-sd
upper_1sd, mid_1sd, lower_1sd = ____(bitcoin_data['Close'], 
                                     ____,
                                     ____,
                                     timeperiod=20)
# Plot the upper and lower Bollinger Bands 
plt.plot(bitcoin_data['Close'], color='green', label='Price')
plt.plot(____, color='tomato', label="Upper 1sd")
plt.plot(____, color='tomato', label='Lower 1sd')

# Customize and show the plot
plt.legend(loc='upper left')
plt.title('Bollinger Bands (1sd)')
plt.show()
Edit and Run Code