bitcoin, investment, finance-7168736.jpg

Bitcoin Price Forecasting: Building a Bidirectional LSTM Neural Network with TensorFlow 2

n this project, you will learn how to construct a basic Deep Neural Network to forecast Bitcoin prices utilizing historical data. While this model can be applied in various ways, any risks associated with its use are solely your responsibility.

If you’re contemplating whether cryptocurrency can still offer lucrative returns, the answer is complex. In this tutorial, we will explore how to create a model that might assist you on this volatile investment path.

Perhaps you’re facing financial challenges? Here’s one strategy you might consider:

Here’s the agenda:

  • Overview of cryptocurrency data
  • Understanding time series data
  • Preprocessing steps
  • Developing and training an LSTM model with TensorFlow 2
  • Predicting future Bitcoin prices using the model

Data Overview

Our data is sourced from Yahoo! Finance and encompasses all the available information on the Bitcoin-USD exchange rate up to the present moment. We’ll import this data into a Pandas dataframe for analysis.

For data consistency, we ensure it’s sorted by date. Here’s a glimpse at the relevant data:

The dataset contains 3201 entries, reflecting the daily Bitcoin-USD exchange rate over approximately nine years. The goal is to predict the closing price for upcoming dates.

Bitcoin trading has resulted in substantial wealth for some, while others have faced losses. It raises the question: could such dramatic changes in fortune happen again? We’ll examine the insights from one potential model on this matter.

Unlike the datasets used in previous examples, this one is organized chronologically, with data points captured at regular one-day intervals—a format known as a time series.

Understanding Time Series Data

Time series data, which captures how values change over time, is prevalent across various domains. From personal energy consumption to stock market fluctuations, time series data encapsulates a wealth of information about temporal changes.

Key characteristics of time series data include:

  • Autocorrelation: This refers to how data points are related to subsequent ones after a certain period or lag.
  • Seasonality: Cyclical patterns that emerge at regular intervals, though not necessarily on an annual basis.
  • Stationarity: A time series with a consistent mean, variance, and covariance that doesn’t depend on time.

When analyzing time series data, one might ask whether current values can predict future ones, a concept central to time series forecasting.

We’ll employ a Deep Neural Network to forecast future Bitcoin prices in this case.

Modeling

Previous models we’ve developed aren’t suited for sequential data. That’s where Recurrent Neural Networks (RNNs) come in, offering the ability to use outputs as new inputs indefinitely.

A notable challenge with RNNs is their struggle with long-term dependencies. Long Short-Term Memory networks (LSTMs), a variant of RNNs, are designed to overcome this hurdle by retaining information for extended periods.

Next, let’s delve into how to implement an LSTM using Keras.

Data Preprocessing

Initially, we scale the price data to a [0, 1] range, enhancing the optimization algorithm’s efficiency:

We’ll use scikit-learn’s MinMaxScaler for scaling:

The data needs to be reshaped into a (x, y) format before scaling.

We’ll also eliminate NaN values, as they could hinder the model’s performance:

We filter out NaNs using isnan and reshape the data accordingly.

Creating Sequences

LSTMs require data in three dimensions. We segment the data into sequences of a set length to achieve the desired structure:

We’ll retain 5% of the data for testing. The datasets will look something like this:

Our training model will use sequences representing 99 days of Bitcoin price changes to forecast prices for the following 156 days.

Building the LSTM Model

Our LSTM network consists of three layers with a 20% dropout rate to prevent overfitting:

Bidirectional RNNs process sequence data in both forward and backward directions, enhancing LSTM performance.

The CuDNNLSTM represents a fast LSTM implementation powered by cuDNN.

The output layer has one neuron with a linear activation function, proportional to the input.

Training

We use Mean Squared Error for the loss function and the Adam optimizer:

Remember, shuffling the data is not advised due to the sequential nature of time series.

Predicting Bitcoin Prices

Finally, let’s use our model to forecast Bitcoin prices:

We reverse the scaling transformation so that prices are back to their original range.

Our concise model seems promising based on its test data performance. Why not test it with other currencies?

Conclusion

Well done on constructing a Bidirectional LSTM Neural Network with TensorFlow 2. The model and preprocessing pipeline are versatile and can be adapted to various datasets.

Refer to the complete source code available in a Google Colab Notebook.

Future research might explore how correlations between different cryptocurrencies impact model performance.