In [ ]:
import sys

sys.path.insert(0, ".")
sys.path.append("../")  # import py files from parent folders/..*

Bollinger Bands¶

In [ ]:
import yfinance as yf
import cufflinks as cf
import plotly.offline

cf.go_offline()
cf.set_config_file(offline=False, world_readable=True)

# Define the Ticker Symbol
ticker_symbol = "GOOG"

# Get the data on this ticker
ticker_data = yf.Ticker(ticker_symbol)

# Get the historical prices for the specified period
ticker_df = ticker_data.history(period="1d", start="2023-1-1", end="2023-7-7")

# Calculate Middle Bollinger Band (SMA)
ticker_df["Middle Band"] = ticker_df["Close"].rolling(window=20).mean()

# Calculate Upper and Lower Bollinger Bands (2 standard deviations from the SMA)
ticker_df["Upper Band"] = (
    ticker_df["Middle Band"] + 2 * ticker_df["Close"].rolling(window=20).std()
)
ticker_df["Lower Band"] = (
    ticker_df["Middle Band"] - 2 * ticker_df["Close"].rolling(window=20).std()
)

# Plot the chart
ticker_df.iplot(
    title="Bollinger Bands of " + ticker_symbol,
    xTitle="Date",
    yTitle="Price",
    theme="pearl",
)
In [ ]: