text_plain_output_1.png
from datetime import datetime

from plotly import offline

import datetime

import pandas as pd

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import pandas_datareader.data as web

import plotly.graph_objects as go

import time

import yfinance as yf

ticker = 'SPY'
start_time = datetime(2020, 5, 1)
end_time = datetime(2023, 5, 1)
spy = yf.download(ticker, start=start_time, end=end_time)

connected = False
while not connected:
    try:
        ticker_df = web.get_data_yahoo(ticker, start=start_time, end=end_time)
        connected = True
    except Exception as e:
        time.sleep(5)
        pass
ticker_df = ticker_df.reset_index()
df = pd.DataFrame(ticker_df)

df = df[df['Volume'] != 0]
df.reset_index(drop=True, inplace=True)
df.isna().sum()

ma5 = df['Close'].rolling(window=5).mean()
trace_ma5 = go.Scatter(x=df.Date, y=ma5, name='5-day Moving Average', line=dict(color='blue'))
ma30 = df['Close'].rolling(window=30).mean()
trace_ma30 = go.Scatter(x=df.Date, y=ma30, name='30-day Moving Average', line=dict(color='orange'))

trace_ma5 = go.Scatter(x=df.Date, y=ma5, name='5-day Moving Average', line=dict(color='blue'))
trace_ma30 = go.Scatter(x=df.Date, y=ma30, name='30-day Moving Average', line=dict(color='orange'))
trace = go.Candlestick(x=df.Date, open=df.Open, high=df.High, low=df.Low, close=df.Close)
chart_data = [trace, trace_ma30, trace_ma5]
layout = go.Layout(title='SPDR S&P 500 ETF Trust (SPY) with Moving Average', width=1800, height=900)
figure = go.Figure(data=chart_data, layout=layout)
offline.iplot(figure)