Time Series Analysis in Python
Welcome to your webinar workspace! You can follow along as we go through an introduction to time series analysis in Python.
This first cell imports some of the main packages we will be using, as well as sets the visualization theme we will be using.
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 the yfinance
package to download market data from the Yahoo! Finance API.
We will define the date range that we want to use, as well as the ticker we want to download.
# Import yfinance
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
We can also use the .describe()
method to get a sense of the data over the period.
# 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)"
)
# Show the plot
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="Early 2021 GameStop Saga"
)
# 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="Halt")
fig.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.
# Define the candlestick data
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 chart)',
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'll use the past 28 days of data.
This also smooths out the line, and still gives day-by-day performance.
# Calculate the 28 day rolling mean price
gme_rolling = gme.rolling("28D").mean()
# Plot the rolling average
fig = px.line(gme_rolling,
x=gme_rolling.index,
y="Close",
template="dc",
title="GameStop Closing Price (rolling 28 day average)"
)
# Show the plot
fig.show()
Comparing to a benchmark
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).