IniziaInizia gratis

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.

Questo esercizio fa parte del corso

Financial Trading in Python

Visualizza il corso

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

# 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()
Modifica ed esegui il codice