Bitcoin price prediction using LSTM

Simeon Kostadinov
Towards Data Science
4 min readFeb 1, 2018

--

The November 2017 intense discussions around Bitcoin grabbed my attention and I decided to dive deep into understanding what exactly is this. I read a bunch of papers, several books and many opinions on the topic in order to get a decent understanding of its value in the current market.

You have probably heard of Bitcoin, but if you want to fully acknowledge its existence, I recommend reading Andreas’ book — The Internet of Money.

Of course, the thing that is most attractive to the vast majority of people is the price volatility of this asset. The increase/decrease in Bitcoin’s price with large percentages over short periods of time is an interesting phenomenon which cannot be predicted at all. However, I thought it would be nice to see the effect of any powerful machine learning model over this price.

So I picked a Recurrent Neural Network and a collection of Bitcoin’s prices to predict the future of the golden cryptocurrency.

I used Bitcoin’s closing price for every day from 01/02/2009 until today as well as a little help from this wonderful Kaggle kernel.

#1. Start implementation

Let’s first define our libraries:

We will be using keras for training the model.

Keras is a high-level neural networks API, written in Python and capable of running on top of TensorFlow, CNTK, or Theano.

We also declare numpy (matrix manipulations), panda (defines data structures), matplotlib (visualization) and sklearn (normalizing our data)

#2. Prepare data

Our next step will be to prepare the data. This includes fetching from a 3rd party source, clearing up and splitting into training and testing.

Here is an excellent link to access a .csv file with the prices.

  • Load data and remove the unused fields (in this case ‘Date’)

We are using pandas to read the .csv file and remove the useless columns.

  • Split into training and testing

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 (usually 80%, 20%, 20%). We have picked the training set to be 30 days which means that we are going to test our model over the last month.

  • Normalize and prepare for training

We use fit_transform to center the data in a way that it has 0 mean and 1 standard error. I recommend reading this Stackoverflow answer for clarification. Then, we divide the data into x_train and y_train. Essentially, that is made because we are using Recurrent neural network. Our model will get the 0-th element from x_train and try to predict the 0-th element from y_train (which is the first element of x_train). This way we are creating a sequence — exactly what RNNs need for training. If you need more information about how these wonderful networks work, you may find one of my posts useful.

Finally, we reshape the x_train data to match the requirements for training using keras (see below).

#3. Train model

Now we need to train our model using the above data. With keras, this process is extremely easy and understandable. You don’t need to know the math behind any of the operations in order to produce decent results. However, if you are interested in diving into equations and algorithms, you can find more information here (LSTM network) and here (GRU network).

We are building the model as follows:

  • Line 1: the number of units (dimension of the output state) used for the LSTM cell.
  • Line 2: the activation function used for the LSTM cell (sigmoid in our case).
  • Line 3: the optimizer used to minimize the loss function (Adam in our case).
  • Line 4: the loss function which we need to minimize while adjusting the weights and biases of the network (Mean squared error in our case).
  • Line 5: neural networks are typically trained on batches which means that on every iteration we pick 5 examples from our training set and use them for training.
  • Line 6: the number of epochs determines how many iterations we need to make.

After explaining what each hyperparameter means, we start training the model:

  • Line 9: define the Sequential model which stacks all layers (input, hidden and output)
  • Line 12: add the LSTM cell to our model. Here is a nice explanation how it works in Keras.
  • Line 15: add the output layer — is a simple Dense with default linear activation function and dimension 1.
  • Line 18: this is the last step before the actual training. We need to set the optimizer and the loss function specified above.
  • Line 21: train the model using inputs x_train and labels y_train.

#4. Predict price

Finally we came to the long-awaited moment of predicting the price. We have 2 steps: predict the price and plot it to compare with the real results.

  • Predict the price for the next month

As you already saw, Keras makes everything so easy. Here the case remains the same:

Lines 1–6: we do exactly the same as for the training set. Using min_max_transform to scale the data and then reshape it for the prediction.

Line 7: predict.

Line 8: rescale the predicted data to match its real values (prices in USD)

  • Visualize results

Finally, we are going to plot test and predicted prices using the below snippet:

The results happen to be really interesting. Of course, the accuracy of prediction is not excellent but still, it is cool to be seen:

For more AI content, Follow me on LinkedIn.

Thank you for reading. If you enjoyed the article, give it some claps 👏 . Hope you have a great day!

--

--

Obsessed with creating a positive impact. Love blogging about AI and reading books. For more content, follow me 👉 https://www.linkedin.com/in/simeonkostadinov/