**Time series analysis in python **
Importing packages and setting visualization themes
import pandas as pd
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
import plotly.io as pio
from datetime import datetime
# set colors
dc_colors=["#2B3A64", "#96aae3", "#C3681D", "#EFBD95", "#E73F74", "#80BA5A", "#E68310", "#008695", "#CF1C90", "#f97b72", "#4b4b8f", "#A5AA99"]
# set template
pio.templates["dc"] = go.layout.Template(
layout = dict(
font = {"family": "Poppins, Sans-serif", "color": "#505050"},
title={"font": {"family": "Poppins, Sans-serif", "color":"black"},
"yanchor": "top",
"y": 0.92,
"xanchor": "left",
"x":0.025},
plot_bgcolor ="white",
paper_bgcolor = "white",
hoverlabel=dict(bgcolor = "white"),
margin = dict(l=100,r=50,t=75,b=70),
colorway=dc_colors,
xaxis=dict(showgrid=False),
yaxis=dict(showgrid=True,
gridwidth=0.1,
gridcolor='lightgrey',
showline=True,
nticks=10,
linewidth=1,
linecolor='black',
rangemode='tozero')
)
)
Loading and Inspecting the Data
The first thing we will do is use yfinance package to download market data from the yahoo!Finance API
we will define the data range that we want to use, as well as the ticker we want to download.
#import y finance
import yfinance as yf
#set the date range
start = "2020-01-01"
stop = "2023-02-01"
#set the ticker we want to use (Gamestop)
ticker = "GME"
#get the data for the ticker GME
gme=yf.download(ticker, start, stop)
#preview dataframe
gme
# get a numeric summary of the data
gme.describe()
Visualizing the data
Next we can use a Plotly line plot to examine the data over time
#create a plotly figure
fig = px.line(gme,
x=gme.index,
y="Close",
template="dc",
title="GameStop Closing Price (daily)"
)
fig.show()
Let's add an annotation to make it clear when key events happened. We will cover three key events in the timeline:
- The date that the new board was announced, and r/wallstreetbets began hyping the stock.
- The date when the trading app RobinHood restricted trading for GameStop(and some other stocks).
- An late February surge fueld by more activity on r/wallstreetbets.
Note : due to a bug with Plotly, we need to use strptime() to convert the dates to milliseconds to enable our annotations.
# create a filtered dataframe for early 2021
gme_2021 = gme["2021-01": "2021-03"]
# create a plotly figure
fig = px.line(gme_2021,
x=gme_2021.index,
y="Close",
template="dc",
title="GameStop Closing Price (early 2021)"
)
# define three key events
short = datetime.strptime("2021-01-11", "%Y-%m-%d").timestamp() * 1000
robin = datetime.strptime("2021-01-28", "%Y-%m-%d").timestamp() * 1000
late_feb = datetime.strptime("2021-02-23", "%Y-%m-%d").timestamp() * 1000
# add these as lines
fig.add_vline(x=short, line_width=0.5, annotation_text="r/wallstreetbets")
fig.add_vline(x=robin, line_width=0.5, annotation_text="Robinhood") \
.add_vline(x=late_feb, line_width=0.5, annotation_text="Memes")
# show the plot
fig.show()
Alternatively, we can use a candlestick chart to get a good sense of price action
# defoine the candlestick data
import plotly.graph_objs as go
candlestick = go.Candlestick(x=gme.index,
open=gme["Open"],
high=gme["High"],
low=gme["Low"],
close=gme["Close"])
#create a candlestick figure
fig = go.Figure(data=candlestick)
fig.update_layout(title="GME Prices (Candlestick)", template ="dc")
#show the plot
fig.show()
Rolling averages
The data is quite noisy. We can also use a window function to calculate the rolling mean over a certain number of periods. In our case, we will use the past 28 days of data
This also smooths out the line and sill gives day by day performance.
#calculate the 28 day rolling mean price
#gme_rolling = gme.rolling("28D").mean()
gme_rolling = gme["Close"].rolling("28D").mean()
#create a plotly figure
fig = px.line(gme_rolling,
x=gme_rolling.index,
y="Close",
template="dc",
title="GameStop Closing Price (rolling 28 day mean)"
)
#show the plot
fig.show()
comparing the bench mark
It would be nice to be able to compare the performance of GameStop against a stock market index such as the S&P 500 (an index tracking the performance of 500 large US companies).
# Get the data for the ticker GSPC
sp=yf.download("^GSPC", start,stop)
#rename close columns
sp = sp.rename(columns={"Close":"S&P Close"})
gme=gme.rename(columns={"Close":"GameStop Close"})
#concatenate the data
all_data=pd.concat([gme["GameStop Close"],sp["S&P Close"]], axis=1)
#preview the data
all_data