Time Series Forecasting Forecast Bitcoin

Khawlajlassi
6 min readJun 20, 2021

This blog post is about my experience in Time Series Forecasting which was not easy at all for me.
I have tried to create a time series to predict the value of BTC (bitcoin) at the close of the following hour (approximately how long the average transaction takes).
I’m going to share with you the steps from the introduction of what’s time series to how I preprocessing my data and setting up of tf.data.Dataset for model inputs and then creates the model that is an RNN (Recurrent Neural Network) architecture use LSTM gates (Long Short Term Memory), with visualization of each these steps.
Finally, I’m sharing my GitHub link to show you the whole process.

Introduction to Time Series Forecasting

Time-series forecasting is one of the important areas of machine learning. This is very important when it comes to prediction problems that involve a time component.

What is a Time series?

Time series can be defined as a sequence of a metric that is recorded over regular time intervals. Depending on the frequency, a time series can be of yearly, quarterly, monthly etc.

There are 2 things which Time-series makes different from the regular regression problem. First one is Time-dependent. In linear regression models, observations are independent but in this case, observations depend on time. And there can be Seasonality trends, where variations specific to a particular time frame.

There are 2 methods used for time series forecasting.

  1. Uni-variate Time-series Forecasting: only two variables in which one is time and the other is the field to forecast.
  2. Multi-variate Time-series Forecasting: contain multiple variables keeping one variable as time and others will be multiple in parameters.

But before we start creating our time series forecasting model we must preprocessing the data to give us the best results.

Preprocessing the data:

First, we import some libraries that will be helpful throughout our forecasting.
Then, we import our datasets (mine is Coinbase) and we get the description for it, and you should get:

screen shot form google colab

As showing The datasets are formatted so that every row represents a 60 second time window containing:

  • Timestamp: The start time of the time window in Unix time
  • Open: The open price in USD at the start of the time window
  • High: The high price in USD within the time window
  • Low: The low price in USD within the time window
  • Close: The close price in USD at end of the time window
  • Volume_(BTC): The amount of BTC transacted in the time window
  • Volume_(Currency): The amount of Currency (USD) transacted in the time window
  • Weighted_Price: The volume-weighted average price in USD for the time window

Then we clean the data. Therefore, we delete all the NaN unuseful data using ‘dropna()’.
As showing first we print the sum of the NaN of each column then after the ‘dropna()’ command the sum of NaN becomes 0.

There is an important step which is converting the date from Timestamp to Datetime. Because, Timestamp is the most basic type of time series data that associates values with points in time(definitely unreadable for human) thus we are using pandas to_datetime function, to convert them to Datetime and put it in a standard format=’%d.%m.%Y %H:%M:%S

The next step is visualizing and grouping the data.

plotting of the Volume(BTC), Volume(Currency), and Weighted_Price In date progressing

We can see that the data before the year 2017 has the “weighted_price” and “Volume_(currency)” stable , we can remove those data for better results, then we know that each row of data is an information stock of every minute but we have to predict one hour in the future BTC price based on the previous 24 hours, that’s why we use the line below to group the data:

Then we get this visualization:

plotting of the Volume(BTC), Volume(Currency), and Weighted_Price In date progressing after 2017

Now we are left with two steps to end the preprocessing data.
First, split the data into the training, validation, and test sets. For us, we split it to (70%, 20%, 10%).
Second, normalize the data, we use Min-Max normalization.

Data windowing

The model in this blog post will make a set of predictions based on a window of consecutive samples from the data.

The main features of the input windows are:

  • The width (number of time steps) of the input and label windows
  • The time offset between them.
  • Which features are used as inputs, labels, or both.

First, define a WindowGenerator class. This class can:

  1. Handle the indexes and offsets .
  2. Split windows of features into a (features, labels) pairs.
  3. Efficiently generate batches of these windows from the training, evaluation, and test data, using tf.data.Datasets.
  4. Plot the content of the resulting windows.

For our time series forecasting, we have to take input width for the 24h and the label width is 1h, shifted by 1 for the column “Weighted_Price”

plotting after WindowGenerator of w1

Single step models:

Compile and fit model:

Define a compile and fit function using mean-squared error (MSE) as it’s a cost function and Adam-optimizer as an optimization function as showing below.

Recurrent neural network model:

A Recurrent Neural Network (RNN) is a type of neural network well-suited to time series data. RNNs process a time series step-by-step, maintaining an internal state from time-step to time-step.

I built a RNN model with LSTM layer(Long Short Term Memory), and used it to predict a single feature’s value, 1 timestep (1h) in the future based only on the current conditions.

To avoid the overfitting problem, I have tried to add more than one LSTM layer and in between, I’ve added a Dropout layer for better results.

Results:

We reuse the plotting function that’s created in WindowGenerator class to show the result of the prediction that our trained model makes.

Conclusion

In this blog post, I focused more on technical details of the implementation than explain how it works, as I said before this wasn’t an easy project for me.
This is my best performance model and I’m open to any advice for better results.
This is my code, check it out, and feel free to play with it.
The last thing, Thank you for getting this far.

--

--