Cryptocurrency Prediction Machine Learning

Cryptocurrency prediction machine learning

If you were to pick the three most ridiculous fads of 2017, they would definitely be fidget spinners (are they still cool? Do kids still use that word “cool”?), artificial intelligence and, yes, cryptocurrencies. Joking aside, I’m actually impressed by the underlying concept and I’m quite bullish on the long term prospects of this disruptive technology.

But enough about fidget spinners!!! I’m actually not a hodler of any cryptos. So, while I may not have a ticket to the moon, I can at least get on board the hype train by successfully predicting the price of cryptos by harnessing deep learning, machine learning and artificial intelligence (yes, all of them!).

I thought this was a completely unique concept to combine deep learning and cryptos (blog-wise at least), but in researching this post (i.e.

Cryptocurrency prediction machine learning

looking for code to copy+paste), I came across something quite similar. That post only touched on Bitcoin (the most famous crypto of them all), but I’ll also discuss Ethereum (commonly known as ether, eth or lambo-money). And since Ether is clearly superior to Bitcoin (have you not heard of Metropolis?), this post will definitely be better than that other one.

We’re going to employ a Long Short Term Memory (LSTM) model; it’s a particular type of deep learning model that is well suited to time series data (or any data with temporal/spatial/structural order e.g.

movies, sentences, etc.). If you wish to truly understand the underlying theory (what kind of crypto enthusiast are you?), then I’d recommend this blog or this blog or the original (white)paper. As I’m shamelessly trying to appeal to a wider non-machine learning audience, I’ll keep the code to a minimum.

There’s a Jupyter (Python) notebook available here, if you want to play around with the data or build your own models.

Cryptocurrency-predicting RNN intro - Deep Learning w/ Python, TensorFlow and Keras p.8

Let’s get started!


Before we build the model, we need to obtain some data for it. There’s a dataset on Kaggle that details minute by minute Bitcoin prices (plus some other factors) for the last few years (featured on that other blog post). Over this timescale, noise could overwhelm the signal, so we’ll opt for daily prices. The issue here is that we may have not sufficient data (we’ll have hundreds of rows rather than thousands or millions).

In deep learning, no model can overcome a severe lack of data. I also don’t want to rely on static files, as that’ll complicate the process of updating the model in the future with new data. Instead, we’ll aim to pull data from websites and APIs.

As we’ll be combining multiple cryptos in one model, it’s probably a good idea to pull the data from one source.

We’ll use For now, we’ll only consider Bitcoin and Ether, but it wouldn’t be hard to add the latest overhyped altcoin using this approach. Before we import the data, we must load some python packages that will make our lives so much easier.

DateOpenHighLowCloseVolumeMarket Cap

To explain what’s just happened, we’ve loaded some python packages and then imported the table that you see on this site.

With a little bit of data cleaning, we arrive at the above table.

Cryptocurrency prediction machine learning

We also do the same thing for ether by simply replacing ‘bitcoin’ with ‘ethereum’ in the url (code omitted).

To prove that the data is accurate, we can plot the price and volume of both cryptos over time.

Training, Test & Random Walks

We have some data, so now we need to build a model.

In deep learning, the data is typically split into training and test sets. The model is built on the training set and subsequently evaluated on the unseen test set. In time series models, we generally train on one period of time and then test on another separate period.

Cryptocurrency prediction machine learning

Rather arbitrarily, I’ll set the cut-off date to June 1st 2017 (i.e. model will be trained on data before that date and assessed on data after it).

You can see that the training period mostly consists of periods when cryptos were relatively cheaper.

As such, the training data may not be representative of the test data, undermining the model’s ability to generalise to unseen data (you could try to make your data stationary- discussed here). But why let negative realities get in the way of baseless optimism? Before we take our deep artificially intelligent machine learning model to the moon, it’s worth discussing a simpler model.

The most basic model is to set tomorrow’s price equal to today’s price (which we’ll crudely call a lag model). This is how we’d define such a model in mathematical terms:

Extending this trivial lag model, stock prices are commonly treated as random walks, which can be defined in these mathematical terms:

We’ll determine μ and σ from the training sets and apply the random walk model to the Bitcoin and Ethereum test sets.


Look at those prediction lines.

Different trading algorithms types:

Apart from a few kinks, it broadly tracks the actual closing price for each coin. It even captures the eth rises (and subsequent falls) in mid-June and late August. At this stage, if I was to announce the launch of sheehanCoin, I’m sure that ICO would stupidly over-subscribed. As pointed out on that other blog, models that only make predictions one point into the future are often misleadingly accurate, as errors aren’t carried over to subsequent predictions. No matter how large the error, it’s essentially reset at each time point, as the model is fed the true price.

The Bitcoin random walk is particularly deceptive, as the scale of the y-axis is quite wide, making the prediction line appear quite smooth.

Single point predictions are unfortunately quite common when evaluating time series models ( and here). A better idea could be to measure its accuracy on multi-point predictions. That way, errors from previous predictions aren’t reset but rather are compounded by subsequent predictions. Thus, poor models are penalised more heavily.

Best scoring optional subject for wbcs

In mathematical terms:

Let’s get our random walk model to predict the closing prices over the total test set.

The model predictions are extremely sensitive to the random seed.

I’ve selected one where the full interval random walk looks almost decent for Ethereum. In the accompanying Jupyter notebook, you can interactively play around with the seed value below to see how badly it can perform.

Notice how the single point random walk always looks quite accurate, even though there’s no real substance behind it.

Hopefully, you’ll be more suspicious of any blog that claims to accurately predict prices. I probably shouldn’t worry; it’s not like crypto fans to be seduced by slick marketing claims.

Long Short Term Memory (LSTM)

Like I said, if you’re interested in the theory behind LSTMs, then I’ll refer you to this, this and this.

Luckily, we don’t need to build the network from scratch (or even understand it), there exists packages that include standard implementations of various deep learning algorithms (e.g.

TensorFlow, Keras, PyTorch, etc.). I’ll opt for Keras, as I find it the most intuitive for non-experts.

Predicting Cryptocurrency Prices with Machine Learning

If you’re not familiar with Keras, then check out my previous tutorial.


I’ve created a new data frame called .

I’ve removed some of the previous columns (open price, daily highs and lows) and reformulated some new ones. represents the gap between the closing price and price high for that day, where values of -1 and 1 mean the closing price was equal to the daily low or daily high, respectively.

The columns are simply the difference between high and low price divided by the opening price. You may also notice that is arranged in order of earliest to latest. We don’t actually need the date column anymore, as that information won’t be fed into the model.

Our LSTM model will use previous data (both bitcoin and eth) to predict the next day’s closing price of a specific coin. We must decide how many previous days it will have access to. Again, it’s rather arbitrary, but I’ll opt for 10 days, as it’s a nice round number.

We build little data frames consisting of 10 consecutive days of data (called windows), so the first window will consist of the 0-9th rows of the training set (Python is zero-indexed), the second will be the rows 1-10, etc.

Picking a small window size means we can feed more windows into our model; the downside is that the model may not have sufficient information to detect complex long term behaviours (if such things exist).

Deep learning models don’t like inputs that vary wildly.

Share this

Looking at those columns, some values range between -1 and 1, while others are on the scale of millions. We need to normalise the data, so that our inputs are somewhat consistent.

Cryptocurrency prediction machine learning

Typically, you want values between -1 and 1. The and columns are fine as they are.

How to make profits in cryptocurrency trading with machine learning

For the remaining columns, like that other blog post, we’ll normalise the inputs to the first value in the window.


This table represents an example of our LSTM model input (we’ll actually have hundreds of similar tables).

We’ve normalised some columns so that their values are equal to 0 in the first time point, so we’re aiming to predict changes in price relative to this timepoint. We’re now ready to build the LSTM model.

Informed trading and option prices evidence from activist trading

This is actually quite straightforward with Keras, you simply stack componenets on top of each other (better explained here).

So, the functions constructs an empty model unimaginatively called model (), to which an LSTM layer is added.

That layer has been shaped to fit our inputs (n x m tables, where n and m represent the number of timepoints/rows and columns, respectively). The function also includes more generic neural network features, like dropout and activation functions. Now, we just need to specify the number of neurons to place in the LSTM layer (I’ve opted for 20 to keep runtime reasonable), as well as the data on which the model will be trained.

We’ve just built an LSTM model to predict tomorrow’s Ethereum closing price.

Let’s see how well it performs.

Why you should be cautious with neural networks for trading

We start by examining its performance on the training set (data before June 2017). That number below the code represents the model’s mean absolute error (mae) on the training set after the 50th training iteration (or epoch).

Instead of relative changes, we can view the model output as daily closing prices.

We shouldn’t be too surprised by its apparent accuracy here. The model could access the source of its error and adjust itself accordingly. In fact, it’s not hard to attain almost zero training errors.

We could just cram in hundreds of neurons and train for thousands of epochs (a process known as overfitting, where you’re essentially predicting noise- I included the call in the function to mitigate this risk for our relatively small model). We should be more interested in its performance on the test dataset, as this represents completely new data for the model.

Caveats aside about the misleading nature of single point predictions, our LSTM model seems to have performed well on the unseen test set.

The most obvious flaw is that it fails to detect the inevitable downturn when the eth price suddenly shoots up (e.g mid-June and October).

Don’t be fooled — Deceptive Cryptocurrency Price Predictions Using Deep Learning

In fact, this is a persistent failure; it’s just more apparent at these spikes. The predicted price regularly seems equivalent to the actual price just shifted one day later (e.g. the drop in mid-July). Furthermore, the model seems to be systemically overestimating the future value of Ether (join the club, right?), as the predicted line near always runs higher than the actual line. I suspect this is because the training data represents a period during which the price of Ether rose astronomically, so it expects that trend to continue (don’t we all).

We can also build a similar LSTM model for Bitcoin- test set predictions are plotted below (see Jupyter notebook for full code).

As I’ve stated earlier, single point predictions can be deceptive.

Looking more closely, you’ll notice that, again, the predicted values regularly mirror the previous values (e.g. October). Our fancy deep learning LSTM model has partially reproducted a autregressive (AR) model of some order , where future values are simply the weighted sum of the previous values. We can define an AR model in these mathematical terms:

The good news is that AR models are commonly employed in time series tasks (e.g. stock market prices), so the LSTM model appears to have landed on a sensible solution.

The bad news is that it’s a waste of the LSTM capabilities, we could have a built a much simpler AR model in much less time and probably achieved similar results (though the title of this post would have been much less clickbaity).

More complex does not automatically equal more accurate.

The predictions are visibly less impressive than their single point counterparts.

Nevertheless, I’m pleased that the model returned somewhat nuanced behaviours (e.g. the second line on the eth graph); it didn’t simply forecast prices to move uniformly in one direction.

Cryptocurrency prediction machine learning

So there are some grounds for optimism.

Moving back to the single point predictions, our deep machine artificial neural model looks okay, but so did that boring random walk model.

Like the random walk model, LSTM models can be sensitive to the choice of random seed (the model weights are initially randomly assigned). So, if we want to compare the two models, we’ll run each one multiple (say, 25) times to get an estimate for the model error. The error will be calculated as the absolute difference between the actual and predicted closing prices changes in the test set.

Maybe AI is worth the hype after all!

Those graphs show the error on the test set after 25 different initialisations of each model.

Cryptocurrency prediction machine learning

The LSTM model returns an average error of about 0.04 and 0.05 on the bitcoin and eth prices, respectively, crushing the corresponding random walk models.

Aiming to beat random walks is a pretty low bar.

It would be more interesting to compare the LSTM model against more appropriate time series models (weighted average, autoregression, ARIMA or Facebook’s Prophet algorithm). On the other hand, I’m sure it wouldn’t be hard to improve our LSTM model (gratuitously adding more layers and/or neurons, changing the batch size, learning rate, etc.). That said, hopefully you’ve detected my scepticism when it comes to applying deep learning to predict changes in crypto prices.

That’s because we’re overlooking the best framework of all: human intelligence. Clearly, the perfect model* for predicting cryptos is:

* This blog does not constitute financial advice and should not be taken as such. While cryptocurrency investments will definitely go up in value forever, they may also go down.


We’ve collected some crypto data and fed it into a supercool deeply intelligent machine learning LSTM model. Unfortunately, its predictions were not that different from just spitting out the previous value.

Hackernoon Newsletter curates great stories by real tech professionals

How can we make the model learn more sophisticated behaviours?

  • Change Loss Function: MAE doesn’t really encourage risk taking. For example, under mean squared error (MSE), the LSTM model would be forced to place more importance on detecting spikes/troughs.

    More bespoke trading focused loss functions could also move the model towards less conservative behaviours.

  • Penalise conservative AR-type models: This would incentivise the deep learning algorithm to explore more risky/interesting models. Easier said than done!

  • Get more and/or better data: If past prices alone are sufficient to decently forecast future prices, we need to include other features that provide comparable predictive power.

    That way, the LSTM model wouldn’t be so reliant on past prices, potentially unlocking more complex behaviours. This is probably the best and hardest solution.

If that’s the positive spin, then the negative reality is that it’s entirely possible that there is no detectable pattern to changes in crypto prices; that no model (however deep) can separate the signal from the noise (similar to the merits of using deep learning to predict earthquakes).

And any pattern that does appear can disappear as quickly (see efficient market hypothesis). Just think how different Bitcoin in 2016 is to craze-riding Bitcoin of late 2017. Any model built on 2016 data would surely struggle to replicate these unprecedented movements. All of this suggests you might as well save yourself some time and stick to autoregression (unless you’re writing a blog, of course).

But I’m sure they’ll eventually find some use cases for deep learning.

In the meantime, you can build your own LSTM model by downloading the Python code here. Thanks for reading!