# Predicting Bitcoin’s Price With Recurrent Neural Networks

Source: [https://cnbc.com](https://image.cnbcfm.com/api/v1/image/106820278-1609972654383-hand-holding-a-bitcoin-in-front-of-a-computer-screen-with-a-dark-graph-blockchain-mining-bitcoin_t20_pRrrjP.jpg?v=1610580302)

Who hasn’t heard about the most famous digital money in the world, the cryptocurrency of the decade? That’s right, I am talking about [**Bitcoin**](https://bitcoin.org/en/). Everyone is talking about Bitcoin and what will happen to its record high price, including famous financial brands such as [**JPMorgan**](http://www.jpmorganchase.com) and [**Morgan Stanley**](https://www.morganstanley.com). This year, Bitcoin reported its all-time high price of $61,556.59 in March of 2021, according to the CoinDesk 20.

The biggest question is, of course, what will happen to the price of this magic money. This is a question that no one has a certain answer to because predicting the exact price of Bitcoin, or in fact, any other financial instrument, is nearly impossible. However, what we can do is to use **Data Science** techniques to predict the Bitcoin price with high precision and that’s exactly what we are going to do in this article. We will built a **Deep Learning** model that will use Bitcoin’s historical data to predict its price.

By the end of this article you will learn about:

*   Deep Learning (RNNs, LSTMs)
*   Data Preprocessing
*   Evaluation of the Bitcoin Price Prediction Model

### About Bitcoin

[Bitcoin](https://www.investopedia.com/terms/b/bitcoin.asp) offers an efficient money tranferes over the internet and is controlled by a decentralized network with a transparent set of rules and works as an alternative to central bank-controlled fiat money. The key technology behind this cryptocurrency is the phenomenon of [**blockchain**](https://www.blockchain.com) which is a growing list of records (*blocks)* linked using cryptography where each block contains a cryptographic hash of the previous block, a timestamp, and transaction data.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723919547075/5162a080-e60c-404e-a8c2-401f31ce67a2.gif)

Source: ShamLatech

By design, a blockchain cannot be modified because once it is recorded, the data in any given block cannot be altered afterwards without alteration of all subsequent [blocks](https://www.wikipedia.org). Hence, any changes in a single block will make all following blocks in a blockchain to be invalid making it obvious that the original data has been manipulated.

### Bitcoin Historical Price Data

To train our pricing model, we use Bitcoin’s public price data reported in the period of 2016–2020, 5 years of financial data downloaded from [**Yahoo Finance**](https://finance.yahoo.com). To evaluate our model we use Bitcoin price data in the period of January 2021-March 2021. We use the model to predict the Bitcoin prices for this period and compare it to the real prices in order to understand how good the model was able to predict the prices.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723919549453/b6a32ff8-a548-4220-9d20-8e0965060424.png)

Bitcoin Price During 2016–2020

#### Data Preprocessing

**Step 1:** **Normalization** Our model is based on RNN with LSTM layers which uses Sigmoid Activation Function to transform input vectors to vectors with entries that have value in a range of \[0,1\]. Therefore, we use normalization to scale model features given that the denominator will always be larger than the nominator, the output value will always be a number between 0 and 1.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723919550791/7d54fa95-b94a-4eee-8e53-c2e1853912a5.png)

Normalization of a Vector

from sklearn.preprocessing import MinMaxScaler

sc = MinMaxScaler(feature\_range=(0,1))  
training\_set\_scaled = sc.fit\_transform(training\_set)

**Step 2: Data Transformation** For the RNN we make an assumption regarding the number of time periods the model needs to go back in time to learn about the current time period. Given that there are in general 30 days in a month, 3 month, that is 90 days, time period seems like a reasonable assumption to make for our RNN model to learn from the Bitcoin’s prices in the past. Hence, per price, the X\_train will contain the Bitcoin prices of the last 60 days and the Y\_train will be the Bitcoin price of that day and this holds for all time periods.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723919552334/831eaf0a-3318-4b4d-916a-bd97c7bc5f3f.png)

Data Transformation for RNN with 90 days time-step

import numpy as np

X\_train = \[\]  
y\_train = \[\]  
X\_test  = \[\]

for i in range(90,training\_set\_scaled.size):  
    X\_train.append(training\_set\_scaled\[i-90:i, 0\])  
    y\_train.append(training\_set\_scaled\[i, 0\])  
X\_train, y\_train = np.array(X\_train), np.array(y\_train)

for i in range(90,inputs.size):  
    X\_test.append(inputs\[i-90:i, 0\])  
X\_test = np.array(X\_test)

**Step 3: Shaping Data** Adding extra dimension to the model that will allow using extra indicators helping to predict the price (e.g. external events causing public anxiety). In this way we transform the data from 2D to 3D.

X\_train = np.reshape(X\_train, \[X\_train.shape\[0\], X\_train.shape\[1\], 1\])  
X\_test = np.reshape(X\_test, \[X\_test.shape\[0\], X\_test.shape\[1\], 1\])

**Download Bitcoin Training and Test Data here:** [https://github.com/TatevKaren/recurrent-neural-network-stock-price-predicition/tree/main/bitcoin\_data](https://github.com/TatevKaren/recurrent-neural-network-stock-price-predicition/tree/main/bitcoin_data)

### Deep Learining

The entire idea behind [**Deep Learning**](https://en.wikipedia.org/wiki/Deep_learning) is to mimic the functional capabilities of human brain. If Artificial Neural Networks [(ANNs)](https://en.wikipedia.org/wiki/Artificial_neural_network) are responsible for the long term memory, like the Temporal lobe of our brain, and if Convolutional Neural Networks ([CNNs](https://en.wikipedia.org/wiki/Convolutional_neural_network)) are responsible for the image recognition and visual interpretation, like the Occipital lobe of our brain, then the Recurrent Neural Networks ([RNNs](https://en.wikipedia.org/wiki/Recurrent_neural_network)) are responsible for the short term memory, like the Frontal Lobe of our brain.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723919553798/9c6a6d14-a892-4b20-9b29-a752616c7896.jpeg)

Source: Queensland Health

#### Recurrent Neural Networks (RNNs)

On one hand the information flows from the input layers to the output layer and on the other hand, the calculated eerror is back propagated through the network to update the weights. Unlike ANN and CNN, in case of RNN, hidden layers not only give an output but also feed themselves.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723919555691/e4f57611-45f1-4fa2-a142-7f914f9fa860.png)

What RNN does is that it translates the provided input features to a machine readable vectors. Then the system processes each of this sequence of vectors one by one, moving from very first vector to the next one in a sequential order. While processing, the system passes the information through the hidden state (memory state) to the next step of this sequence.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723919556929/b5a18e5a-5027-4e00-90c4-5de69bac9230.gif)

[Source: MichaelPhi](https://towardsdatascience.com/illustrated-guide-to-lstms-and-gru-s-a-step-by-step-explanation-44e9eb85bf21)

Once the hidden state has collected all the existing information in the system from the previous steps, it is ready to move towards the next step and combine this information with this current step’s input (Xt) to form a new information vector.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723919558866/5b9aeb67-beb6-4d3c-a9b9-58a9be3e086a.gif)

[Source: MichaelPhi](https://towardsdatascience.com/illustrated-guide-to-lstms-and-gru-s-a-step-by-step-explanation-44e9eb85bf21)

#### **Long Short-Term Memories (**[**LSTMs**](https://en.wikipedia.org/wiki/Long_short-term_memory)**)**

When training a RNN model, during the optimization process, in the very initial epoch, the weights are randomly chosen values and in case these chosen values are very small, multiplying them with the same recurrent weight for many times, the gradient becomes less and less and at some point the gradient vanishes. Then, the lower the gradient is, the harder it is to update weight which means the slower the optimization process will be.

Additionally, there is a domino effect and one improperly updated weight effects the calculation of the remaining weights and makes them inaccurate as well, given that all weights are related. This issue in RNNs is referred as **Vanishing Gradient Problem** and **Long Short-Term Memories (**[**LSTMs**](https://en.wikipedia.org/wiki/Long_short-term_memory)**)** are solving this problem of RNNs. The difference between the usual RNN and LSTM is the set of operations that are performed on the passedinformation and the input value in that specific step. The information in LSTMs flows through its gates:

*   **forget gate:** *a gate that decides what information should be thrown away or kept*
*   **input gate:** *a gate to update the cell state*
*   **cell state:** *a gate to update the cell state to new values that the network finds relevant*
*   **output gate:** *a gate to decide what the next hidden state should be*

### Building Bitcoin Pricing Model

The goal is to predict the stock prices which is a continuous output value, hence we have a regression rather than classification problem. We initialize this regressor as an object with sequential layers for which we use *Sequential* module from [**Keras**](https://keras.io) that allows to create a Neural Network object with sequential layers. Then, we add LSTM layers and given that predicting a price of a financial product is a quite complex task, we like to have a model with high dimensionality that can capture upward and downward trends in the stock price, hence we use large number of LSTM units per LSTM layer and we use multiple LSTM layers. Moreover, we add a *Dropout* layer, for regularization to ignore part of the neurons in the LSTM layers. Finally, we use the *Dense* module to add an output layer

from keras.models import Sequential  
from keras.layers import Dense  
from keras.layers import LSTM  
from keras.layers import Dropout

regressor = Sequential()

\# LSTM layer 1  
regressor.add(LSTM(units = 50, return\_sequences=True, input\_shape = (X\_train.shape\[1\], 1)))  
regressor.add(Dropout(0.2))

\# LSTM layer 2,3,4  
regressor.add(LSTM(units = 50, return\_sequences=True))  
regressor.add(Dropout(0.2))  
regressor.add(LSTM(units = 50, return\_sequences=True))  
regressor.add(Dropout(0.2))  
regressor.add(LSTM(units = 50, return\_sequences=True))  
regressor.add(Dropout(0.2))  
regressor.add(LSTM(units = 50, return\_sequences=True))  
regressor.add(Dropout(0.2))

\# LSTM layer 5  
regressor.add(LSTM(units = 50))  
regressor.add(Dropout(0.2))

\# Fully connected layer  
regressor.add(Dense(units = 1))

\# Compiling the RNN  
regressor.compile(optimizer = 'adam', loss = 'mean\_squared\_error')

#Fitting the RNN model  
regressor.fit(X\_train, y\_train, epochs = 120, batch\_size = 32)

### Bitcoin Pricing Model Predictions

Once the pricing model is trained, we use it in combination with test data to generate the Bitcoin price predictions for the testing period, January 2021-March 2021 which we then compare with the real Bitcoin prices reported in the same time period.

As we can see our RNN model based on multiple LSTM layers, was able to properly predict upward and downward trends because we see that the blue line corresponding to the predicted Bitcoin prices follows the same pattern as the yellow line which corresponds the real Bitcoin prices.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723919561241/27b31f46-c5bc-445b-8f67-f407c314d7c8.png)

import matplotlib.pyplot as plt

predicted\_stock\_price= regressor.predict(X\_test)  
predicted\_stock\_price= sc.inverse\_transform(predicted\_stock\_price)

plt.plot(real\_stock\_price, color = '#ffd700', label = "Real Price January - March 2021")  
plt.plot(predicted\_stock\_price, color = '#4782B4', label = "Predicted Price January - March 2021")  
plt.title("Bitcoin Price Prediction")  
plt.xlabel("Time")  
plt.ylabel("Bitcoin Price")  
plt.legend()  
plt.show()

#### Resources

**Code**: [https://github.com/TatevKaren/recurrent-neural-network-stock-price-predicition/blob/main/Bitcoin\_Pricing\_Model.py](https://github.com/TatevKaren/recurrent-neural-network-pricing-model/blob/main/Bitcoin_Pricing_Model.py)

**Data**: [https://github.com/TatevKaren/recurrent-neural-network-stock-price-predicition/tree/main/bitcoin\_data](https://github.com/TatevKaren/recurrent-neural-network-stock-price-predicition/tree/main/bitcoin_data)

**Google Stock Pricing Model with RNN**: [https://github.com/TatevKaren/recurrent-neural-network-price-predicition](https://github.com/TatevKaren/recurrent-neural-network-stock-price-predicition)

**Customer Churn Prediction Model with ANN:** [https://github.com/TatevKaren/artificial-neural-network-business\_case\_study](https://github.com/TatevKaren/artificial-neural-network-business_case_study)

**Image Recognition Model with CNN:** [https://github.com/TatevKaren/convolutional-neural-network-image\_recognition\_case\_study](https://github.com/TatevKaren/convolutional-neural-network-image_recognition_case_study)

### If you liked this article, here are some other articles you may enjoy:

[**Fundamentals Of Statistics For Data Scientists and Data Analysts**  
*Key statistical concepts for your data science or data analytics journey*towardsdatascience.com](https://towardsdatascience.com/fundamentals-of-statistics-for-data-scientists-and-data-analysts-69d93a05aae7 "https://towardsdatascience.com/fundamentals-of-statistics-for-data-scientists-and-data-analysts-69d93a05aae7")[](https://towardsdatascience.com/fundamentals-of-statistics-for-data-scientists-and-data-analysts-69d93a05aae7)

[**Simple and Complete Guide to A/B Testing**  
*End-to-end A/B testing for your Data Science experiments for non-technical and technical specialists with examples and…*towardsdatascience.com](https://towardsdatascience.com/simple-and-complet-guide-to-a-b-testing-c34154d0ce5a "https://towardsdatascience.com/simple-and-complet-guide-to-a-b-testing-c34154d0ce5a")[](https://towardsdatascience.com/simple-and-complet-guide-to-a-b-testing-c34154d0ce5a)

[**Monte Carlo Simulation and Variants with Python**  
*Your Guide to Monte Carlo Simulation and Must Know Statistical Sampling Techniques With Python Implementation*towardsdatascience.com](https://towardsdatascience.com/monte-carlo-simulation-and-variants-with-python-43e3e7c59e1f "https://towardsdatascience.com/monte-carlo-simulation-and-variants-with-python-43e3e7c59e1f")[](https://towardsdatascience.com/monte-carlo-simulation-and-variants-with-python-43e3e7c59e1f)

[**How To Crack Spotify Data Science Technical Screen Interview**  
*List of exact Python/SQL commands and experimentation topics you should know to nail Spotify Tech Screen*towardsdatascience.com](https://towardsdatascience.com/how-to-crack-spotify-data-science-technical-screen-interview-23f0f7205928 "https://towardsdatascience.com/how-to-crack-spotify-data-science-technical-screen-interview-23f0f7205928")[](https://towardsdatascience.com/how-to-crack-spotify-data-science-technical-screen-interview-23f0f7205928)

[**Bias-Variance Trade-off in Machine Learning**  
*Introduction to bias-variance trade-off in Machine Learning and Statistical models*tatev-aslanyan.medium.com](https://tatev-aslanyan.medium.com/bias-variance-trade-off-in-machine-learning-7f885355e847 "https://tatev-aslanyan.medium.com/bias-variance-trade-off-in-machine-learning-7f885355e847")[](https://tatev-aslanyan.medium.com/bias-variance-trade-off-in-machine-learning-7f885355e847)

[**Data Sampling Methods in Python**  
*A ready-to-run code with different data sampling techniques to create a random sample in Python*tatev-aslanyan.medium.com](https://tatev-aslanyan.medium.com/data-sampling-methods-in-python-a4400628ea1b "https://tatev-aslanyan.medium.com/data-sampling-methods-in-python-a4400628ea1b")[](https://tatev-aslanyan.medium.com/data-sampling-methods-in-python-a4400628ea1b)

[**PySpark Cheat Sheet: Big Data Analytics**  
*Here is a cheat sheet for the essential PySpark commands and functions. Start your big data analysis in PySpark.*medium.com](https://medium.com/analytics-vidhya/pyspark-cheat-sheet-big-data-analytics-161a8e1f6185 "https://medium.com/analytics-vidhya/pyspark-cheat-sheet-big-data-analytics-161a8e1f6185")[](https://medium.com/analytics-vidhya/pyspark-cheat-sheet-big-data-analytics-161a8e1f6185)

***Thanks for the read***

*I encourage you to* [***join Medium today***](https://tatev-aslanyan.medium.com/membership) *to have* *complete access to all of the great locked content published across Medium and on my feed where I publish about various Data Science, Machine Learning, and Deep Learning topics.*

*Follow me up on* [***Medium***](https://medium.com/@tatev-aslanyan) *to read more articles about various Data Science and Data Analytics topics. For more hands-on applications of Machine Learning, Mathematical and Statistical concepts check out my* [***Github***](https://github.com/TatevKaren) *account.  
I welcome feedback and can be reached out on* [***LinkedIn***](https://www.linkedin.com/in/tatev-karen-aslanyan/)*.*

***Happy learning!***
