Part 1: A n00bs Guide To Deep Cryptocurrency Trading

Leon Fedden
11 min readNov 3, 2017

--

This blog post is a high level overview of my foray into using neural networks to trade bitcoins.

For about two years, I’ve doggy-paddled well out of my depth through the endless ocean of deep learning soup. During this, I’ve become ever more lazy; typically, I can never be bothered to learn a new topic, so I often find myself wondering if I can get my computer to learn and do it for me!

This article is about training learning systems (using neural networks) to trade cryptocurrencies for me, because I’m too lazy to read the thick financial trading tomes available in my library. There will be a few posts in this series as I progress my understanding and source code, so bare with me.

Cryptocurrencies right now

Before we start to think about things like neural networks or reinforcement learning, it’s important to review the domain first and get an intuition for the domain and the kind of problems that are trying to be solved. If I miss something, or supply any contentious material, leave a comment and I’ll be happy to edit it in!

A Motivation For / Explanation Of Blockchain

People have traditionally relied on a central authority when it comes to transacting money (or value) around a society. Intermediaries such as governments and banks have helped to ensure trust into the transactions that society has built upon. A potential issue with having a central authority managing all the money is that it can sometimes go terribly wrong.

Piles of banknotes during the German mark’s hyperinflation — in part caused by massive borrowing by the German emperor and parliament to pay for the first world war. There were other options — France imposed its first income tax. By November 1923, the US dollar was worth 4,210,500,000,000 German marks.

Let us use an analogy to look into a problem regarding transactions of value. If I were to give you an car, you would then own that car, and I would now have to walk home. In the physical realm, the transaction took place and the car left my possession in it’s entirety. If now, I took a photo of this car and saved it to my computer, I would end up with a digital representation of this car as a bunch of pixel colours as a file. I’m of the generous sort, and have given you my digital car picture . Can you ensure that like before in the physical realm, you are the sole proprietary owner of the picture?

Blockchain tech may do more for piracy prevention than this advert ever did!

Of course it’s entirely possible I was so generous and selfless that I sent this picture of a car out to thousands of people as a spam email before giving it to you. I just like to share. The issue that is hopefully arising here is that the digital exchange is not the same as the physical one. Digital files are incredibly easy to reproduce, creating something called the double spending problem. The double spending problem is where we spend the same unit of value more than once, and until blockchain has been a major disadvantage in the economy of digital assets.

You smile, knowingly, and exclaim ‘Aha! But it is simple — we can just build a digital ledger — this way everyone can track who owns which automobiles’.

A early ledger for keeping track of transactions.

The issue now arises that I have ended up in control of ledger (someone needs to have control over it) and I’m sick of being the nice guy. I decide to assign everybody's cars to myself. There is also the issue that as I am the owner of the ledger, if anyone wants to make genuine transactions they need to go through me, as an intermediary.

The solution is to riot, remove me from power and distribute the ledger digitally amongst everybody's computers. If I now try to steal anyones car, my ledger will not match every one elses, and I will denied the illegal transaction. In other words, the Blockchain is a distributed ledger or decentralised database that continuously updates records of who owns what. Rather than a single person owning the database, the databases are replicated across the network and synchronised over the internet. Members in the network with large amounts of compute capability take all of the past 10 minutes transactions and compete to validate them, and the winner receives a reward of cryptocurrency (generally.)

This public ledger is open source, which means there are no nasty surprises coded deep into the software (read: we can trust it more). The openness, decentralised and cryptographic nature of the blockchain makes it very hard to hack, unlike monolithic banking software which is regularly making headlines as hackers make off with millions of stolen currency. Note how our digital car photo now behaves like the physical thing; when I make an exchange it is clear who now unambiguously owns it and as a bonus, we don’t need any third party verification (such as my corrupt self) to ensure I didn’t send extra copies for myself! It is this public ledger, known as the blockchain, that cryptocurrencies such as Bitcoin are built on top of, and the focus of this article.

What The Hell Are Cryptocurrencies?

Cryptocurrency is simply a digital asset using the aforementioned technologies. According to wikipedia as of the 11th of July 2017 there are over 900 different cryptocurrencies and growing. The main behemoth is Bitcoin, of course. Bitcoin is the world's first decentralised digital currency, created by an unknown person, alias or group of people called Satoshi Nakamoto. If you are not aware of this story, check it out — one of the craziest things is that as of September 2017, Nakamoto owns roughly one million bitcoins, at a value of $6 billion USD.

Historic footage of secretive Nakamoto with (his?) bitcoins that (he?) can’t spend

But there are many alternatives to Bitcoin, which are often grouped under the term altcoins. Many altcoins attempt to target any perceived limitations of bitcoin, or try to reimagine some component of technolgy for some advantage. Litecoin for example, increases the speed a new block is generated. Etherium is aiming to build a blockchain platform to issue and sell initial coin offerings and there are loads more. This is a good link to read up on the various types of altcoins.

How Can I Trade Cryptocurrencies?

Let’s just get one thing straight — if you don’t have the money to lose, you shouldn’t invest in bitcoin. I’m a student (studying at the University of London) and not from a wealthy background. I’ll be simulated trading with pretend coins, before moving to a very small amount of money that I can afford to lose. I’m also going to gloss over manual trading, as the focus here is on creating artificial learning systems to trade for us!

So where can we trade the currencies algorithmically? There are hundreds of exchanges with different coins that you can trade, with varying volumes of currency being traded. Each exchange has it’s own API that you can access, which usually sends you JSON data for you to unpack and interpret in your own way. Regardless, you can pick any exchange that you like and access their API — the good ones should document how to do so on their website. Alternatively, you could use something like ccxt. This is a multi language (Python, PHP and Javascript) library that allows for easy, unified access to over ninety exchanges at the time of writing. As we’ll be writing lots of code in Python for our machine learning, ccxt is a great way to get started quickly. You can install it by following the instructions at the github!

So how do we use ccxt? As a hello world for algorithmic trading, let’s say we want to get some data from the Poloniex exchange. In ccxt, every exchange offers ‘markets’ within itself — the set of markets differs from exchange to exchange. This potentially opens up possibilities of cross market arbitrage.

But what is a market I hear you ask? A market within this context is a pair of cryptocurrencies. Ultimately, our goal is to buy (and sell) cryptocurrencies, and to do this we must exchange one for the other. For example: The market might be exchanging Etherium for Bitcoin. The symbol (and key for the python dictionary object) for this market would be ‘ETC/BTC’. The symbol ‘ETC’, stands for Etherium, and is the base currency. The symbol ‘BTC’ stands for Bitcoin, and is the quoted currency. Buying this pair or market would mean we buy the base currency with the quoted currency. Inversely, selling this pair would gain us the quoted currency for the base currency.

It is important to note that one of the driving reasons that neural networks are so popular today is our increasing ability to create large streams of data. That, and hardware acceleration (thanks CS:GO players,) and a minor appreciation for incrementally better algorithms, means that we can do amazing things with deep learning. As are not at the trading stage yet, we must look at collecting data, which is an imperative task. Incredibly, we can access lots of market data in just four lines of code, including the Python ccxt module import!

Getting real cryptocurrency values in just four lines of Python!

There is a lot of data here, and to interpret it better, I can strongly recommend checking out the ccxt manual, which at the time of writing is being worked very hard on! The manual also notes exactly how you can use the API to actually make trades, which we’ll go over in a later article.

Applying Deep Learning To Trading And The Portfolio Management Problem

There are many approaches of melding machine learning and trading together. A typical approach is to try to predict future price movements or trends — supplying a neural network with historic prices can be trained to output a predicted vector of prices for the future. These approaches are fairly easy to implement; it is supervised learning and a matter of collating the data and training a neural architecture to perform regression on. However, in practice it doesn’t work so well.

To validate my opinion on this matter, I did some basic regression using a multi-layer perceptron network and a recurrent neural network (long short term memory) on a financial dataset obtained from Kaggle, and have uploaded the notebook experiment.

The whole point of this notebook is to demonstrate that it is non-trivial to just throw a neural network at price prediction and assume we can just get rich quick. I also don’t suggest that I have been in anyway exhaustive — research shows that turning this problem into a classification problem can improve results, by simply asking the model to perform binary classification on whether a stock will jump by a margin based on historical data. There are an abundance of other models and methods, so please don’t let me put you off your fantastic get rich quick schemes (if you do have a good idea— leave a comment and let me know what it is!)

Out of the many applications of deep learning to the financial markets I choose to focus on one in particular: The Portfolio Management Problem. This is the repetitive process of reallocating funds into a set of discrete financial products; aiming to maximise the total return whilst minimising the risk. There are a few issues with price prediction — it is difficult to get accurate models (and therefore high performance) and price predictions are not market actions, so this means extra logic must convert the price prediction into an action, meaning a non-end-to-end machine learning solution, which is not as adaptable or as trainable. It is for example, difficult for a price prediction neural network to consider the transaction cost of market actions, which is important in real trading, especially when there is a high volume of trading. In the next post, we’ll go into implementing a deep reinforcement learning system to tackle the portfolio management problem.

Getting More Cryptocurrency Data and Visualising It

So before jumping into deep reinforcement learning I deemed it worth further exploring the available data, as well as hacking together very basic data persistence method via an sqlite3 database. Firstly lets check out a simple example that prints out the market order book at the limit defined by whatever exchange that we choose.

There is clearly more to the API than this, and we can look at a further example that gets more data and plots it in interactive graphs. A quick bit of housekeeping; you’ll need to install a few extra Python packages; Dash which essentially allows you to plot graphs in the browser using just Python (making use of great libraries such as D3.js, React and Flask.)

Below we can see more of the Poloniex exchange being graphed. This updates around every second, and the every market or coin pair offered by the exchange can be visualised by selected the desired pair via the dropdown menu.

You can plot the above information in your browser by running the below code. It will also begin to save the data (for use in training deep learning systems) in a database. There may be other forms of data which would be valuable for training, so have some thought for what data could inform the relationships you are trying teach your function approximators.

If you do run the below code, ensure that there is a folder called databases in the same directory as the code. You could easily add a line of Python to ensure that the folder exists if you want to. Although this is a reasonable start, this code could easily be improved or extended via better table structure and perhaps migrating to postgres, as well as accessing more of the API.

Concluding Remarks

Over this post I’ve demonstrated some of the ideas behind cryptocurrencies, and how to programmatically access and visualise real-time data using various Python modules. I have also introduced the notion of applying deep learning techniques to trading. It is clear that just attempting to predict the prices of financial products is a hard task, and there are other ways of trading, such as tackling the portfolio management problem using deep reinforcement learning, which I’ll be writing about in the next article.

Thanks for reading, it’s been super fun to write and I’m looking forward to the next one. Feel to send public abuse to me on twitter or leave a comment here and leave this post a clap if you enjoyed it— and I look forward to hearing from you!

--

--

Leon Fedden

Wizard nerd summoning TensorFlow, C++ and Python black magic from the void. https://leonfedden.co.uk/