Moving Average



import pandas as pd
import numpy as np

import plotly.graph_objects as go
import plotly.io as pio


df = pd.read_excel('Asxdata.xlsx', sheet_name='St Barbara', usecols=['Date', 'Close'])
ma10 = []
ma50 = []

#Close = df['Close'].to_frame()

# Calculate 30-day Simple Moving Average (SMA)
df['SMA30'] = df['Close'].rolling(20).mean()
df['SMA50'] = df['Close'].rolling(50).mean()
# Remove NULL values
df.dropna(inplace=True)

# Print DataFrame
print(df.head())



fig = go.Figure()
fig.add_trace(go.Scatter(x=df.Date, y=df.Close,
mode='lines',
name='Close',
line=dict(
dash = "dot",
color='green',
width=4)
))
fig.add_trace(go.Scatter(x=df.Date, y=df.SMA30,
mode='lines',
line_color='#CB27F5',
name='SMA30'))
fig.add_trace(go.Scatter(x=df.Date, y=df.SMA50,
mode='lines',
name='SMA50',
line=dict(color="#ffe476")
))


fig.update_xaxes(title_text='Date', type='date')
fig.update_yaxes(title_text='Close')

fig.update_layout(title_text='St Barbara')
pio.write_html(fig, file='hello_world.html', auto_open=True)