Stock Prices

Apply time series analysis techniques to forecast stock prices.

We'll cover the following...

Context

We'll work with stock prices from an American gaming retailer called GameStop Corp. to predict its close price.

Press + to interact

Let's start by taking a look at the data to gather some basic information.

Press + to interact
main.py
data.csv
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('data.csv')
# Changing the datatype
df["date"] = pd.to_datetime(df['date'], format='%Y-%m-%d')
# Looking at the dataframe
print(df.head())
print(df.tail())
# Setting the Date as index
df = df.set_index('date')
# Choosing the variable we will work with
close_price = df['close_price']
# Plotting
fig, ax = plt.subplots(figsize=(7, 4.5), dpi=300)
ax.plot(close_price)
# Labelling
plt.xlabel("Date")
plt.ylabel("Price (USD)")
plt.title("GameStop stock close price")
fig.savefig("output/output.png")
plt.close(fig)

There's much relevant information we can get from this first look.

  • There doesn't seem to be a trend.

  • There doesn't seem to be a seasonal pattern.

  • There's one huge outlier at the very end.

  • Data is daily, and it goes from 2002 to 2021.

The first thing we need to do is get rid of that outlier. After a quick online search, we see that there was indeed an outstanding event in January 2021 that made that specific stock price soar. That means we are dealing with a different kind of outlier—one that is a true value. Not only that, but we have no data after that peak, so it gets harder to ...