Last active
July 13, 2024 14:57
-
-
Save jace48/5136404194c8c3843e467923c485fd01 to your computer and use it in GitHub Desktop.
EMA and SMA of Stock with Rolling days of 250
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from datetime import datetime | |
import pandas as pd | |
import plotly.graph_objs as go | |
''' | |
Sample CSV File | |
Ticker,Date,Open,High,Low,Close,Volume | |
SBIN,2024-04-24,778.50,778.50,770.20,773.10,7842615 | |
SBIN,2024-04-25,770.65,814.40,769.65,812.70,36732976 | |
SBIN,2024-04-26,815.90,816.90,798.90,801.30,14964358 | |
''' | |
FileName = "C:\\Fibotrader\\csvexport\\{}.csv".format("SBIN") | |
df = pd.read_csv(FileName, index_col="Date", parse_dates=True, usecols=['Date', 'Close'], na_values=['nan']) | |
percentage_change = np.diff(df['Close']) / df['Close'][:-1] | |
df['PerChange'] = np.concatenate(([0], percentage_change)) | |
sma_250 = df['PerChange'].rolling(window=250).mean() | |
sma_250 = sma_250*250 | |
df['SMA250'] = np.concatenate(([0], sma_250.loc[df.index[:-1]])) | |
ema_250 = df['PerChange'].ewm(span=250, min_periods=0, adjust=False).mean() | |
ema_250 = ema_250 * 250 | |
df['EMA250'] = np.concatenate(([0], ema_250.loc[df.index[:-1]])) | |
fig = go.Figure() | |
fig.add_trace(go.Scatter(x=df.index, y=df['EMA250'], mode='lines', name='EMA250')) | |
fig.add_trace(go.Scatter(x=df.index, y=df['SMA250'], mode='lines', name='SMA250')) | |
fig.update_layout( | |
title="SMA250 and EMA250", | |
xaxis_title="Date", | |
yaxis_title="EMA&SMA's", | |
xaxis_rangeslider_visible=True | |
) | |
fig.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very nicely put, sir.