Last active
May 22, 2023 13:46
-
-
Save SajidLhessani/28b1a87351964ba5f310c5acf87204d5 to your computer and use it in GitHub Desktop.
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
# Raw Package | |
import numpy as np | |
import pandas as pd | |
#Data Source | |
import yfinance as yf | |
#Data viz | |
import plotly.graph_objs as go | |
data = yf.download(tickers='SPY', period='1d', interval='1m') | |
#Interval required 1 minute | |
data['Middle Band'] = data['Close'].rolling(window=21).mean() | |
data['Upper Band'] = data['Middle Band'] + 1.96*data['Close'].rolling(window=21).std() | |
data['Lower Band'] = data['Middle Band'] - 1.96*data['Close'].rolling(window=21).std() | |
#declare figure | |
fig = go.Figure() | |
fig.add_trace(go.Scatter(x=data.index, y= data['Middle Band'],line=dict(color='blue', width=.7), name = 'Middle Band')) | |
fig.add_trace(go.Scatter(x=data.index, y= data['Upper Band'],line=dict(color='red', width=1.5), name = 'Upper Band (Sell)')) | |
fig.add_trace(go.Scatter(x=data.index, y= data['Lower Band'],line=dict(color='green', width=1.5), name = 'Lower Band (Buy)')) | |
#Candlestick | |
fig.add_trace(go.Candlestick(x=data.index, | |
open=data['Open'], | |
high=data['High'], | |
low=data['Low'], | |
close=data['Close'], name = 'market data')) | |
# Add titles | |
fig.update_layout( | |
title='SPY live share price evolution', | |
yaxis_title='Stock Price (USD per Shares)') | |
# X-Axes | |
fig.update_xaxes( | |
rangeslider_visible=True, | |
rangeselector=dict( | |
buttons=list([ | |
dict(count=15, label="15m", step="minute", stepmode="backward"), | |
dict(count=45, label="45m", step="minute", stepmode="backward"), | |
dict(count=1, label="HTD", step="hour", stepmode="todate"), | |
dict(count=3, label="3h", step="hour", stepmode="backward"), | |
dict(step="all") | |
]) | |
) | |
) | |
#Show | |
fig.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment