Bitcoin Price Prediction Using Recurrent Neural Networks and LSTM

Ana Lucia Lima 21 Jul, 2022
6 min read
This article was published as a part of the Data Science Blogathon


  1. Introduction
  2. Understanding Bitcoin
  3. Technologies used
  4. Getting real-time cryptocurrency data (bitcoin)
  5. Normalization
  6. Predict the price of cryptocurrency using LSTM neural network (deep learning)
  7. Test Dataset
  8. Conclusion

1. Introduction

Recurrent neural networks (RNN) are the state-of-the-art algorithm for sequential data and are used by Apple’s Siri and Google’s voice search. It is an algorithm that remembers its input due to its internal memory, which makes the algorithm perfectly suited for solving machine learning problems involving sequential data. It is one of the algorithms that have great results in deep learning. In this article, it is discussed how to predict the price of Bitcoin by analyzing the information of the last 6 years. We implemented a simple model that helps us better understand how time series works using Python and RNNs.

2. Understanding Bitcoin

Bitcoin is a cryptocurrency that was created in January 2009. It is the world’s most valuable cryptocurrency and is traded on over 40 exchanges around the world, accepting over 30 different currencies. As a currency, Bitcoin offers a new opportunity for price forecasting as it has high volatility, which is much higher compared to traditional currencies.

The bitcoin system is a set of decentralized nodes that run the bitcoin code and store its blockchain. Metaphorically, a blockchain can be considered a collection of blocks. In each block, there is a collection of transactions. Because all the computers running the blockchain has the same list of blocks and transactions, and can transparently see these new blocks being filled with new bitcoin transactions, no one can cheat the system.

Bitcoin uses peer-to-peer technology to facilitate instant payments. Miners are responsible for processing transactions on the blockchain and are driven by repo fees.

The way bitcoin works are key to understanding why it is so popular. Unlike other investments, cryptocurrency is not tied to physical assets or the US dollar. Its primary purpose is to allow two people anywhere to exchange value directly. What this means is that there is no centralized controlling this network. There is no government, no central bank that can shut down or arbitrarily raise or lower the value.

It will be interesting to see to which degree central banks start to digitize their own currencies. As financial systems become more digital, it’s leading to bitcoin more mainstream, but the digital currency’s resurgence is also closely tied to the state of global finance.

Every time someone makes a transaction, a unique encrypted signature is added to the ledger for verification

3. Technologies used

1. Recurrent Neural Networks

RNNs are a robust and powerful type of neural network and are considered one of the most professional algorithms because they are the only ones with internal memory.

Recurrent neural networks were first created in the 1980s, but only in recent years has their true potential been realized. The increase in its computational power, along with the gigantic amounts of data we now have to work with, and the invention of short-term memory (LSTM) in the 1990s, has really brought RNNs to the fore.

The algorithm performs very well for sequential data such as time series, speech, text, financial data, audio, video, weather, and more. RNNs are able to form a much deeper understanding of a sequence and its context compared to other algorithms.

In an RNN, the information goes through a cycle. When making a decision, it considers the current input and also what it has learned from the inputs it has received previously.

The image below illustrates how the flow of information works in the RNN algorithm.


Bitcoin Price Prediction RNN

2. Long Short-Term Memory (LSTM)

Long short-term memory networks are an extension of recurrent neural networks, which basically extend the memory. Therefore it is well suited to learn from important experiences that have very long time lags in between.

LSTMs enable RNNs to remember inputs over a long period of time. This is because LSTMs contain information in a memory, much like the memory of a computer. The LSTM can read, write and delete information from its memory.

In an LSTM you have three gates: input, forget and output gate. These gates determine whether or not to let new input in (input gate), delete the information because it isn’t important (forget gate), or let it impact the output at the current timestep (output gate). Below is an illustration of an RNN with its three gates:

Bitcoin Price Prediction LSTM

The gates in an LSTM are analog in the form of sigmoids, meaning they range from zero to one. The fact that they are analog enables them to do backpropagation.

4. Getting real-time cryptocurrency data (bitcoin)

The data is collected the current data for Bitcoin from Yahoo Finance

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
Bitcoin Price Prediction data
data_training = data[data['Date']< '2020-01-01'].copy()
data Bitcoin Price Prediction
data_test = data[data['Date']< '2020-01-01'].copy()
Bitcoin Price Prediction test data
 training_data = data_training.drop(['Date', 'Adj Close'], axis = 1)
training Bitcoin Price Prediction

5. Normalization

The first step we will take to our data is to normalize its values. The goal of normalization is to change the values of numeric columns in the data set to a common scale, without distorting differences in the ranges of values.

MinMaxScaler is used to normalize the data
scaler = MinMaxScaler()
training_data = scaler.fit_transform(training_data)
Bitcoin Price Prediction normalization
X_train = [] 
Y_train = []
for i in range(60, training_data.shape[0]):
X_train, Y_train = np.array(X_train), np.array(Y_train)

6. Predict the price of cryptocurrency using LSTM neural network (deep learning)

This is the model-building stage. Finding the right model is an art, and it will take several tweaks and attempts to find the right layers and hyperparameters for each one.

The model building is quite simple and standard for this type of problem.

Training this model is something you can do even without a GPU, the amount of data is very low and the network architecture is very simple. When it comes to more advanced models with more granular information, it can take hours or days to train.

from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, LSTM, Dropout
#Initialize the RNN
model = Sequential() model.add(LSTM(units = 50, activation = 'relu', return_sequences = True, input_shape = (X_train.shape[1], 5)))
model.add(Dropout(0.2)) model.add(LSTM(units = 60, activation = 'relu', return_sequences = True))
model.add(Dropout(0.3)) model.add(LSTM(units = 80, activation = 'relu', return_sequences = True))
model.add(Dropout(0.4)) model.add(LSTM(units = 120, activation = 'relu'))
model.add(Dropout(0.5)) model.add(Dense(units =1))
model Bitcoin Price Prediction
model.compile(optimizer = 'adam', loss = 'mean_squared_error')
history=, Y_train, epochs = 20, batch_size =50, validation_split=0.1
epoch Bitcoin Price Prediction
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(len(loss))
plt.plot(epochs, loss, 'b', label='Training loss')
plt.plot(epochs, val_loss, 'r', label='Validation loss')
plt.title("Training and Validation Loss")
Training and Validation Loss

7. Test Dataset

part_60_days = data_training.tail(60)
df= part_60_days.append(data_test, ignore_index = True)
df = df.drop(['Date', 'Adj Close'], axis = 1)
Test data
inputs = scaler.transform(df) inputs
scaler transformer
X_test = []
Y_test = []
for i in range (60, inputs.shape[0]):
    X_test.append(inputs[i-60:i]) Y_test.append(inputs[i, 0])
    X_test, Y_test = np.array(X_test), np.array(Y_test) X_test.shape, Y_test.shape
    Y_pred = regressor.predict(X_test) Y_pred, Y_test


scale = 1/5.18164146e-05
Y_test = Y_test*scale Y_pred = Y_pred*scale






plt.plot(Y_test, color = 'red', label = 'Real Bitcoin Price')
plt.plot(Y_pred, color = 'green', label = 'Predicted Bitcoin Price')
plt.title('Bitcoin Price Prediction using RNN-LSTM')
Bitcoin price prediction


RNNs and LSTM are excellent technologies and have great architectures that can be used to analyze and predict time-series information. The focus of the article was to implement a simple model, if you are interested in the subject, try different things and want to play with hyperparameters and layers.

The media shown in this article on Bitcoin Price Prediction are not owned by Analytics Vidhya and is used at the Author’s discretion.

Ana Lucia Lima 21 Jul, 2022

Frequently Asked Questions

Lorem ipsum dolor sit amet, consectetur adipiscing elit,

Responses From Readers


Typo 27 May, 2021

looks like here an error: history=, Y_train, epochs = 20, batch_size =50, validation_split=0.1 ValueError: Data cardinality is ambiguous: x sizes: 1684 y sizes: 1 Make sure all arrays contain the same number of samples.

Margie Aaron
Margie Aaron 10 Jun, 2021

Don't put all your eggs in 1 basket. Bitcoin will always be around because of the coin exchanges and its own blockchain. Given.. I Ran into a video few months ago, I found so many people showering Accolades to digitalcurrency14 @gmailCom who has made a great Impact to so many young crypto investors since last year, some said he has the best strategy to trade Forex, Bitcoin and mine, I was tired of Gdax and Binance small profits i was getting, so I reached out to via email, and he gave me the best tips to excel in the world of Crypto, i bet you his signals, investment management, fund recovery of lost investors funds from their scam broker platforms and mining team are unlike anything you have ever seen. Now I don’t just Hold, I make more Gains, after 3months I can Boast of increasing my Portfolio from 2Btc to 12.5Btc in less than six months, all Thanks to Digital currency Almighty Formula for Trading mining and investment, Y'all can reach out to him via his email

Haoyang Li
Haoyang Li 27 Mar, 2022

Hello, Thanks for your article, absolutely a great help! But I have a question, you used "Y_pred = regressor.predict(X_test)" but "regressor" is not defined before, could you please explain what is that? Thanks!

Alex 02 Jun, 2022

Hello! Firstly thanks for this awesome project! I'm try to simulate your code for a school project, In point 7 (Test Datasheet) when you do the "for I in range( 60, input.shape[0]" I have a mistake in the code. --> AttributeError: 'list' object has no attribute 'shape' Is it posible that you have installed library or something similar. Thanks in advanced for you time.

ErikD 28 Sep, 2022

I like this article (I think it gives very useful insight into preparing data and evaluation etc), but I have doubts about the conclusion in this specific context (market prediction using only market 'candlesticks' data). I mean LSTMs are indeed very useful, but at the end we see a graph that follows the actual market trend seemingly nicely. But I don't think the 'predicted' graph is a full prediction all the way from the beginning, but instead a prediction per timestep compared to the actual data at that particular timestep. And that looks way off all the way through. (If I'm wrong here, then feel free to disregard the rest of my post) So my conclusion would be that this model doesn't seem to have much significant predictive value at all. It would've been quite magical with that limited data and that little training, and to be honest it's quite impressive how much the model doesn't screw up after only 20 epochs of training. But it is what it is imho.