Financial modeling Blog #1 - BTC price prediction using basic neural networks

in #bitcoin6 years ago (edited)

Code available on Githhub

Introduction

Recently I've taken an interest in financial markets as complex mathematical systems and contemporary methods for predictions, forecasts and gathering data on how particular markets are behaving. This is a blog post summarising the previous few days of work where I developed a basic neural network to predict the next day's bitcoin price based on the previous 10 days using Google's tensorflow library.

It is my goal over the comming months to develop a range of computational modelling techniques, in order to forecast the price, test the markets stability and asses the probabilities associated with financial forecasts. I plan on posting my findings on this site.

An Impressive Graph

Red line: prediction. Blue line: actual price.
NNoutput.png
I was quite surprised by the accuracy of the neural network's prediction, especially over period when the BTC price approached $20000 as this was rather abnormal behaviour even for bitcoin. The network was trained on BTC-USD price data from Coinbase (source) from when Coinbase first started until about a year and a half ago, and then tested against price data from the past year and a half. The data was just a list of transactions that happened on the Coinbase exchange (perhaps actually GDAX) and so I wrote aquire.py to sort through and find the daily volume and average price. The network averages about .7% error on the training data set. In order to be able to use the neural network to predict the price that data needs to be normalised so that it is between 0 and 1, which is achieved by
ql_82d52cccb1cf064951df5e6108d2d5d4_l3.png
where r is the range and m is the minimum price, p is the price and a is the data that is given to the network. In doing this the network is unable to tell the real size of the price, only the relative changes.

Taking closer look

closeupbtc.png
This is the network's prediction for the price action when bitcoin reached it's current all time high (as of April 2018). Unfortunately this shows a rather large flaw in the output. When there is extreme price action, the output is heavily reliant on the previous day's price and is only a little bit better than just predicting that tomorrow's price will be about the same as today's price. The main use of this neural network would have to be: 'assuming nothing that big will happen with bitcoin's price, this is what will probably happen'. If one was to try and forecast using this neural network around the 21st day mark then the network really has no idea when the price will stop rising. In order to get a better grasp on these situations, the network needs more information, perhaps in the form of the number of active users trading bitcoin, the number of new users entering the market and the volume. I'll discuss this more in the next blog where I'll be looking at recurrent neural networks, as in this form giving this data to the neural network is easier.

Attempting forecasting

Given that the neural network's error is about 0.7% and assuming a normal distribution of errors, in theory I should be able to create a distribution of possible future prices by making a prediction, giving it a random bit of error and then feeding that result back into the neural network to predict the next day. Here is a graph of an example prediction, taken from around November time.
extrapolation.png
As you can see the network does an alright job predicting the price for the first 4 or so days (this is a rather calm period, the network isn't capable of knowing whether large price movements are likely). After 4 days however the neural network just seems to converge on a price that is around that of the input as I suppose this is the general case when predicting the next days value, especially as the network was trained on old (pre 2017 data).

Over the coming weeks I'll investigate other techniques to model the price action of bitcoin and the interaction of other cryptocurrencies.