Back to blog

Ethereum Price Prediction with Python

Data Science

Ethereum Price Prediction with Python

A short guide to time series forecasting using the Prophet library

Photo by Maxim Hopman on Unsplash

Disclaimer: This article expresses thoughts and ideas that are mine alone and do not reflect the view of bitgrit. This article is for educational purposes only and should NOT be taken as financial advice.

The rise of cryptocurrencies

Cryptocurrency is a major topic of discussion recently as its market cap surged to a record $2 trillion in April 2021. To put that into comparison, the market cap of Apple, a 45 year old company has a market cap of around 2 trillion dollars as well.

If you don’t know about cryptocurrencies yet, it might be the time to start learning about them. They are branded as the future of not just money, but many processes and operations that power our day-to-day lives. In simple terms, it’s like an amalgamation of cryptography, programming, and finance.

Talking about cryptocurrency, Ethereum is the second-largest cryptocurrency by market capitalization, right behind Bitcoin. Its main purpose is to help execute decentralized smart contracts. As of today, its market cap is larger that big companies like Walmart, Netflix and Disney.

In this article, I will be predicting the price of Ethereum for the following year.

Predicting cryptocurrency price

Predicting cryptocurrency price is difficult and some might even say it’s a waste of time, this is because of how volatile it is, especially since it’s still nascent in its development. Some people say that cryptocurrency is like internet in the 1980s, and I think that describes it very well.

Nonetheless, predicting cryptocurrency price is a very interesting topic and can be a fun project to work on if you’re interested in time series analysis, in finance or data science in general.

Time Series data

Cryptocurrency price, like stock price, is time-series data. As you know, there are many different algorithms in machine learning, each has its own purpose for different use cases.

Speaking of ML algorithms, read about the top 5 machine learning algorithm used by data scientists in 2020

A bit of background behind time series data, it is assumed to have four main components:

  • trend (ex: increase in prices, pollution, decrease in sales…)
  • seasonal (ex: seasons, festivals, religious activities, climate…)
  • cyclical (ex: business cycles)
  • irregular (ex: unexpected events like natural disasters or accidents)

More time series components

A popular library that focuses on forecasting on time series data is the prophet library.

Get access to all medium stories today (you’ll be supporting me directly!)

What is Prophet?

The Prophet library developed by Facebook is a popular library that is used specifically for forecasting time series data.

Based on their website, it is — an additive regression model with four main components:

  • A piecewise linear or logistic growth curve trend. Prophet automatically detects changes in trends by selecting changepoints from the data.
  • A yearly seasonal component modeled using Fourier series.
  • A weekly seasonal component using dummy variables.
  • A user-provided list of important holidays.

I won’t go into detail about what this means, but Prophet is a very straightforward and customizable library that is used to create accurate and reasonable forecasts.

Let’s now dive into the code of predicting Ethereum prices using Prophet.

You can find the code for this article here.

Loading libraries

As always, we start by loading the libraries we need.

import pandas as pd
import yfinance as yf
from datetime import datetime
from datetime import timedelta
import plotly.graph_objects as go
from fbprophet import Prophet
from fbprophet.plot import plot_plotly, plot_components_plotly
import warnings
warnings.filterwarnings('ignore')
pd.options.display.float_format = '${:,.2f}'.format

Note: If you’re having trouble with installation of the fbprophet library because of pystan, try downgrading pystan to a lower version like so:

pip install --no-cache-dir -I pystan==2.19.1.1

Getting the data

To get the data on Ethereum prices, we’ll be using the yfinance library, which is a Yahoo! Finance market data downloader.

We’ll also use the today function from the datetime library, so whenever you run this notebook, the date for today will be updated.

The price for ethereum started late 2015, so we’ll just set the start date as January 1st of 2016.

today = datetime.today().strftime('%Y-%m-%d')
start_date = '2016-01-01'
eth_df = yf.download('ETH-USD',start_date, today)
eth_df.tail()

We see that our data has date, open, high, low, close, adjusted close price, and volume.

We’ll be using the open price as our price value. The other columns aren’t needed for our prophet model so we’ll be dropping them later.

EDA

Now we’ll do a bit of analysis on our data running info()

eth_df.info()

As well as checking for null values just in case.

eth_df.isnull().sum()

Phew! we don’t have to do any data cleaning.

If you want to brush up your data cleaning skills, check out our Data Cleaning using Python article.

We need a date column for our prophet model, but it’s not listed as one of the columns. Let’s figure out why that’s the case.

eth_df.columns
# OUTPUT
Index(['Open', 'High', 'Low', 'Close', 'Adj Close', 'Volume'], dtype='object')

From our output, we see that the date column wasn’t indexed.

We’ll reset the index, and we can have our Date as a column.

eth_df.reset_index(inplace=True)
eth_df.columns 
# OUTPUT
# Index(['Date', 'Open', 'High', 'Low', 'Close', 'Adj Close', 'Volume'], dtype='object')

The prophet library requires us to have only two columns in our data frame — “ds” and “y”, which is the dateandopen columns respectively.

So let’s grab the necessary columns and put it into a new data frame. Then we use the rename function to change the column names.

df = eth_df[["Date", "Open"]]
new_names = {
"Date": "ds",
"Open": "y",
}
df.rename(columns=new_names, inplace=True)

Voila! Running the tail function again, we see our data is ready for Prophet.

df.tail()

But before that, let’s do a quick visualization of our price column using the plotly library, which provides interactivity.

# plot the open price
x = df["ds"]
y = df["y"]
fig = go.Figure()
fig.add_trace(go.Scatter(x=x, y=y))
# Set title
fig.update_layout(
title_text="Time series plot of Ethereum Open Price",
)
# full code in notebook

We see that from our plot there are two major spikes that might be influential on our prophet model.

We can also tell that the fluctuation in our price exaggerates as year increases. This could signify the type of time series data this is. We’ll talk about that later on.

If you want to perform time series decomposition to have a clearer understanding of your data, check out this article by Alex Mitrani which uses the statsmodels package to do so.

Now let’s start building our prophet model.

Prophet model

First, we define our model and tune it according to your purpose, then you can fit it to your data frame. (Note this is a very simple model and there can be more tuning done to it to improve its accuracy.)

m = Prophet(
seasonality_mode="multiplicative"
)
m.fit(df)

Running this should give you something like this.

INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
<fbprophet.forecaster.Prophet at 0x7f79c1ddbf10>

The reason we set seasonality mode to “multiplicative” is we can assume it’s a multiplicative time series because of how cryptocurrency price fluctuates by the year, which also means the seasonal component changes with the trend.

Read more about how to compare additive and multiplicative time series here.

Now we create an entire year’s worth of date data for our prophet model to make predictions

future = m.make_future_dataframe(periods = 365)
future.tail()

We see the date is one year from today’s date.

Model predictions

Then, running the predictions is as easy as calling the predict function

Then we grab the essential columns we need.

forecast = m.predict(future)
forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail()

We can also get the price prediction for the next day just for fun.

next_day = (datetime.today() + timedelta(days=1)).strftime('%Y-%m-%d')
forecast[forecast['ds'] == next_day]['yhat'].item()
# OUTPUT
2021-05-25
2525.5788173913843

Forecast plots

Prophet also has built-in plotly functions that can help us easily visualize our forecast.

plot_plotly(m, forecast)

Forecast components

Our forecasting model also includes growth curve trend, weekly seasonal, and yearly seasonal components which can be visualized like this.

plot_components_plotly(m, forecast)

Our model tells us that:

  • There will be an upward trend for the price of Ethereum
  • The price of ETH is lowest around November on a Thursday.
  • ETH is most expensive around May on a Wednesday.

Summary

This article was purely for fun and as you can see I did not do any in-sample prediction, or try to increase it’s accuracy. It had zero hyperparameter tuning, and no other regressor features were added.

Also, one thing to watch out for is whether your time series is a random walk. If it is, your model might only be producing the previous day’s price and not any useful predictions.

The purpose of this article was to provide a gentle introduction to Prophet, and you’re welcome to take this further and even turn this into your portfolio project.

You could even experiment with alternatives like the ARIMA model or Deep learning (LSTM Models) to perform forecasting, and then compare their performance using diagnostics like R-squared or RMSE.

That’s all for this article, thank you for reading and I hope you learned something new! We have a new competition released — the Viral Tweets Prediction Challenge. Don’t forget to register by July 6th, 2021 to win prizes up to $3000!

Follow Bitgrit’s socials to stay updated!

Below you can find links to a few really well-written articles that can inspire you to take this further.

Links

Further Reading

Here’s one that highlights how you should NOT predict stock prices.

Books