Bollinger Bands measure price volatility and highlight potential overbought or oversold conditions. A 20-period simple moving average forms the middle band; the upper and lower bands sit two price standard deviations above and below it. section 10.2.2
Read the indicator through its components and changing width:
- Middle band: the moving average of price over the selected window, with 20 periods as the default. section 10.2.2
- Upper band: the middle band plus two standard deviations, often treated as an overbought or resistance area. section 10.2.2
- Lower band: the middle band minus two standard deviations, often treated as an oversold or support area. section 10.2.2
- Wider bands indicate higher volatility, while narrower bands indicate more stable price movement; changing the window can make signals too sensitive or insufficiently responsive. section 10.2.2
Plotting bands for NVIDIA
A Python workflow downloads NVIDIA prices with yfinance, calculates a rolling 20-day SMA and standard deviation, creates upper and lower columns, and plots them with Matplotlib. The resulting chart exposes changing volatility and potential days to investigate for buying or selling, rather than guaranteeing a trade outcome. section 10.2.2 section 10.4
Listing 10.4 Plotting Bollinger Bands
import yfinance as yf
import pandas as pd
import matplotlib.pyplot as plt
ticker = "NVDA"
data = yf.download(ticker, start="2024-01-01")
window = 20 #1
std_dev_factor = 2 #1
data['SMA'] = data['Close'].rolling(
window=sma_window_length).mean() #1
rolling_std = data['Close'].rolling(
window=sma_window_length).std() #1
data['rf'] = (rolling_std * std_dev_factor) #1
data['upper'] = data['SMA'] + data['rf'] #1
data['lower'] = data['SMA'] - data['rf'] #1
plt.figure(figsize=(12, 6))
plt.plot(data.index, data['Close'], label="Close Price", color="blue",
linewidth=1)
plt.plot(data.index, data['SMA'], label=f"{window}-Day SMA",
color="orange", linewidth=1.5)
plt.plot(data.index, data['upper'], label="Upper Bollinger Band",
color="green", linestyle="--", linewidth=1)
plt.plot(data.index, data['lower'], label="Lower Bollinger Band",
color="red", linestyle="--", linewidth=1)
plt.title(f"Bollinger Bands for {ticker} (20-day window)", fontsize=14)
plt.xlabel("Date", fontsize=12)
plt.ylabel("Price", fontsize=12)
plt.legend()
plt.grid(alpha=0.3)
plt.show()
Use the plotting listing to inspect the rolling-window calculations, standard-deviation factor, derived bands, and chart construction. section 10.2.2
