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 = "SPY"
# 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-6-15")
low_14 = ticker_df["Low"].rolling(14).min()
high_14 = ticker_df["High"].rolling(14).max()
ticker_df["%K"] = (ticker_df["Close"] - low_14) * 100 / (high_14 - low_14)
ticker_df["%D"] = ticker_df["%K"].rolling(3).mean()
# Plot the chart
ticker_df.iplot(
title="Stochastic Oscillator of " + ticker_symbol,
xTitle="Date",
yTitle="Price",
theme="pearl",
)