Time Series Analysis in Python
Welcome to your live training workspace! You can follow along as we go through an introduction to time series analysis in Python. To consult the solution, head over to the file browser and select notebook-solution.ipynb
.
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
# Set the date range
# Set the ticker we want to use (GameStop)
# Get the data for the ticker GME
# Preview DataFrame
We can also use the .describe()
method to get a sense of the data over the period.
# Get a numeric summary of the data
Visualizing the data
Next, we can use a Plotly line plot to examine the data over time.
# Create a Plotly figure
# Show the plot
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
# Create a Plotly figure
# Define three key events
# Add these as lines
# Show the plot
Alternatively, we can use a candlestick chart to get a good sense of price action.
# Define the candlestick data
# Create a candlestick figure
# Show the plot
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
# Plot the rolling average
# Show the plot
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).