Cryptocurrency Price Prediction Using Deep Learning

Subramanya Nagabhushanaradhya
Coinmonks
7 min readSep 22, 2020

--

A complete machine learning real-world application walk-through using LSTM neural networks.

The popularity of cryptocurrencies skyrocketed in 2017 due to several consecutive months of the exponential growth of their market capitalization. The prices peaked at more than $800 billion in January 2018.

Although machine learning has been successful in predicting stock market prices through a host of different time series models, its application in predicting cryptocurrency prices has been quite restrictive. The reason behind this is obvious as prices of cryptocurrencies depend on a lot of factors like technological progress, internal competition, pressure on the markets to deliver, economic problems, security issues, political factors, etc. Their high volatility leads to the great potential of high profit if intelligent inventing strategies are taken. Unfortunately, due to their lack of indexes, cryptocurrencies are relatively unpredictable compared to traditional financial predictions like a stock market prediction.

In this blog, I will be going through a four-step process to predict cryptocurrency prices:

  1. Getting real-time cryptocurrency data.
  2. Prepare data for training and testing.
  3. Predict the price of cryptocurrency using the LSTM neural network.
  4. Visualize the prediction results.

The Challenge

To forecast cryptocurrency prices using all the trading features like price, volume, open, high, low values present in the dataset.

Data

The dataset can be downloaded from the CryptoCompare website which can be found here.

The dataset contains a total of 5 features. The details for them are as follows:

  1. Close Price — It is the market close price for currency for that particular day.
  2. High Price — It is the highest price of currency for the day.
  3. Low Price — It is the lowest price for currency for that day.
  4. Open Price — It is the market open price for currency for that day.
  5. Volume — The volume of currency that is being in the trade for that day

Where is the code?

Without much ado, let’s get started with the code. The complete project on GitHub can be found here.

I started with loading all the libraries and dependencies required.

I have used the Dollar exchange rate and stored the real-time data into a pandas data frame. I used to_datetime() method to convert string Date time into Python Date time object. This is necessary as Date time objects in the file are read as a string object. Performing operations like time difference on a string rather a Date Time object is much easy.

Let’s see how the dataset looks like with all the trading features like price, volume, open, high, low.

Next, I split the data into two sets — training set and test set with 80% and 20% data respectively. The decision made here is just for the purpose of this tutorial. In real projects, you should always split your data into training, validation, testing (like 60%, 20%, 20%).

Now let’s plot the cryptocurrency prices in dollars as a function of time using the below code:

We can observe that there is a clear dip in prices between December 2018 and April 2019. The prices keep on increasing from April 2019 to August 2019 with fluctuations happening in the months of July and August. From September 2019 onward prices are constantly decreasing. The interesting thing to be noted from this price fluctuation is that the prices are low in winter and it increases in the summer. Although this can’t be generalized as the dataset under consideration is just a small sample that is for a year. Also with cryptocurrency it’s hard to generalize anything.

Next, I made a couple of functions to normalize the values. Normalization is a technique often applied as part of data preparation for machine learning. The goal of normalization is to change the values of numeric columns in the dataset to a common scale, without distorting differences in the ranges of values.

Next, I made a function to extract data of windows which are of size 5 each as shown in the code below:

I continued with making a function to prepare the data in a format to be later fed into the neural network. I used the same concept of splitting the data into two sets — training set and test set with 80% and 20% data respectively as shown in the code below:

LSTM

It works by using special gates to allow each LSTM layer to take information from both previous layers and the current layer. The data goes through multiple gates (like forget gate, input gate, etc.) and various activation functions (like the tanh function, relu function) and is passed through the LSTM cells. The main advantage of this is that it allows each LSTM cell to remember patterns for a certain amount of time. The thing to be noted is that LSTM can remember important information and at the same time forget irrelevant information. The LSTM architectures is shown below:

Now let’s build the model. Sequential model is used for stacking all the layers (input, hidden, and output). The neural network comprises a LSTM layer followed by 20% Dropout layer and a Dense layer with a linear activation function. I complied with the model using Adam as the optimizer and Mean Squared Error as the loss function.

Next, I set up some of the parameters to be used later. These parameters are — random number seed, length of the window, test set size, number of neurons in LSTM layer, epochs, batch size, loss, dropouts, and optimizer.

Now let’s train the model using inputs x_train and labels y_train.

I used Mean Absolute Error (MAE) as the evaluation metric. The reason behind choosing MAE over Root Mean Squared Error (RMSE) is that MAE is more interpretable. RMSE does not describe average error alone and hence is much more difficult to understand. Since we want the model to be readily explained even to the non-technical audience, MAE looks like a better choice.

Mean Absolute Error

It measures the average magnitude of the errors in a set of predictions, without considering their direction. It’s the average over the test sample of the absolute differences between actual and predicted observations where all individual differences have equal weight.

The MAE value 0.029311972877135727 looks good. Finally, let’s plot the actual and predicted prices using the below code:

Conclusions

In this article, I demonstrated how to predict cryptocurrency prices in real-time using LSTM neural network. I went through a four-step process of getting real-time cryptocurrency data, preparing data for training and testing, predicting the prices using the LSTM neural network, and visualizing the prediction results. Feel free to play with the hyper-parameters or try out different neural network architectures for better results.

Also, Read

--

--