How We Have Beaten the Crypto Market Using Machine Learning

Original article can be found here (source): Artificial Intelligence on Medium

We lived in 5-star hotels, worked on trading bots and gambled in local casinos for a break. It was an intense and surreal experience that completely changed my career. Upon leaving, I was confident that I’d take on beating the market myself, but years have passed, and I haven’t got into it.

The reason why I never got into it is that while that experience was inspiring, it was also quite demotivating. The only person I knew who managed to beat the market was clearly out of my league, both intellectually and psychologically. Overall, he had a brilliant mind and an outstanding ability to handle stress. I had severe doubts about whether I was good enough.

Later on, I started building a company, and while hiring software developers and data scientists, I always had this potential project in mind. At some point, we seemingly got the right people, but it was too hard to justify the risk. Imagine that you’re a senior data scientist at Google making half a million a year. What are the chances you can quit your job and beat the market significantly enough to make it worth it? I would not take this risk. Similarly, we were doing some generously compensated client work and the idea of getting our top talent busy with an at least year-long experiment that would most likely fail… was simply scary.

What changed my mind was one of our projects, a liquidity provider. It got more and more clients among crypto derivative exchanges and was being used under increasingly competitive requirements. We hit the limits of our ability to keep the profit above zero — as our old approach was about not doing anything stupid rather than doing something particularly smart. We decided to move the goalpost from not losing money to beating the market as a way to push ourselves to the next level. Even if we failed (which was highly probable), the insights we would get during this process would most likely improve our liquidity provider.

We started building a team. We were lucky enough to have access to a mature trading infrastructure so that we could focus exclusively on data science. Finding data scientists with experience at the level required for trading outside of large financial hubs is next to impossible. Building your expertise from scratch is the only realistic option. It was clear that it would take us a while, and we would make several embarrassing rookie mistakes on the way (spoiler alert: we did), but overall, it went smoothly. If I had to do it again, I would prioritize raw talent, passion and commitment over the experience of working in a hedge fund.

The core of the team were our two most experienced data scientists in time series (predictions) and clustering (market regimes) and a data engineer who helped with data processing, backtesting and tooling. Also, I was responsible for research and management. Overall, we put together a pretty well-balanced team, and it was time to get our hands dirty.

It was intense. We treated this project as a kind of intellectual war. We did not discuss or plan it; it just naturally worked out this way. It felt like this attitude was the only way to overcome the enormous gap that was separating us from the people who can consistently beat the market. We worked at full speed; the drive was going through the roof. It was R&D in its pure form: exhausting in the most exciting way possible.

The first two months were fun: we were killing it, breakthrough after breakthrough, it seemed like nothing could stop us. But then we hit a plateau, and over the next six weeks we made next to zero progress, and there was a growing feeling of discouragement. Psychologically it was a very tough period. Can we do this? Aren’t we wasting our time? We were so close to calling it a day.

There were several reasons behind that plateau. First, the initial progress was mostly driven by the publicly available information, and we didn’t notice how fast we got into the territory where you’re mostly on your own. Second, the further you go, the more complex and time-consuming ideas/experiments get, which limits your ability to try out different things. Third, due to overconfidence caused by the initial progress, we spread our resources too thin by working on too many things.

It was clear we needed to shake things up, so we added more structure, switched from creative chaos to a more mature and sustainable process with more granular tasks that gave us doping in the form of regular progress. Even if sometimes it was a little artificial, the motivation coming out of it made it worth it.

And we got lucky. I recall at least three occasions when we got a breakthrough out of nowhere. For example, at some point, we struggled with detecting trends under specific market conditions. Among other things, we were playing with an idea that was mentioned between the lines in a hardly relevant white paper. It turned out to be a game-changer. We haven’t seen that idea anywhere else, so the chances of finding it were slim to none. We would probably have eventually discovered it ourselves, but it would have taken longer, and we may have needed to cancel the project by that time. When margins are that thin, luck becomes crucial.

After about five months of hard work, we saw the first consistently profitable version. It rapidly got to the first $100k of profit and then gracefully went through the market crash (which market makers are particularly sensitive to). It felt like we reached the first milestone, so I wanted to sit down and put all our thoughts together. Below, I will cover some specific topics in more detail.

Market making

On the surface, market making is a “get rich fast” strategy. Let’s take crypto derivative exchanges as an example, where you get a 0.025% rebate (negative commission) for each trade. Using the most naive market making strategy, you can make two to three thousand trades a day, which is about 50–70% a day. Sounds good, right? But don’t hurry to buy a Lamborghini: market making is a place where dreams die, thanks to adverse selection (when your buy orders get executed while the price is going down and the other way around) and inventory risk (when the price jumps against your position).

Let’s take a look at the graph that shows us the position, price and profit for a naive market-making strategy in action. First, we make trades and end up with a long position, but then the price drops, and so does our profit. Consequently, our position turns short and the price jumps up, so we lose to better-informed players again. Rinse and repeat.

There are dozens of recent white-papers mostly derived from the classic Avellaneda and Stoikov (2008) which put together most of the moving parts of market-making under a solid framework. But none of them are even remotely profitable, mostly because they either ignore market predictions or do something naive about them. We had to figure out this part by ourselves, and that was the root cause of our plateau.

Initially, we believed the only path to profitability was the ability to predict the market for relatively long periods. This way in the example above we would be able to predict whether the market considerably drops (so we need to take a small loss by resetting the position with a taker order) or it’s a small fluctuation (and we could wait until the market comes back to make a profitable counter-trade).

No matter what we tried, our results were mediocre. Here are a couple of lessons we’ve learned:

  1. When we got underwhelming results, there was a temptation to blindly brute-force the right parameters. Nine out of ten times it’s a bad idea, it’s an attempt to compensate for the lack of quality with quantity. Even if you get optimal parameters this way, they will likely get overfit to historical data. There’s a time and place for this approach, but you have to be very careful.
  2. We’ve spent too much time trying to find a silver bullet kind of machine learning approach. It’s a good idea to start with traditional methods instead of getting right into neural networks as they are unforgiving, take a lot of time to make them work well and are not guaranteed to bring better results.

We had to accept the fact that we could not solve the problem directly, so we started looking for ways around it. After multiple failed attempts, we ended up with rather precise, multi-layered market regimes on steroids. It allowed us to get a pretty good idea of what was going on in the market without making predictions, and within that narrowed context, we could use our machine learning prediction engine on the time scale it was comfortable with. This seemingly apparent sweet spot brought us the first profitable version.

It’s by far the hackiest part of our solution. On the one hand, a perfectionist inside me is not particularly happy about it. On the other hand, if we compare this kind of data science to software development, it would not be a beautiful code with solid architecture written in a high-level language. It would be an ugly, highly-optimized code in C that gave up the last bit of architecture for the sake of performance.

Market-making is very subtle, nuanced, and insidious. There’re reasons why it’s usually done by hedge funds or banks that occasionally lose a lot of money. If I could change one thing, I would start with a simpler market-taking strategy, hopefully, bring it to profitability and only after that — take on market making. There were just way too many things going on, it was overwhelming, and it negatively influenced our chances.

Sources of insights

Machine learning application in finance is an extremely secretive field, so when you’re starting from scratch, getting insights is very problematic. Here are the sources of information that helped us:

  1. Advances in Financial Machine Learning

It’s the best book to get an overview of how to use machine learning in finance. It helped us build the first remotely decent model within a reasonable timeline. Though later on, we started carefully re-evaluating and even occasionally violating some of the rules from the book, which resulted in increasing our profitability.

2. White papers

We’ve processed hundreds of white papers on the way, and they certainly are a great source of insights, but you have to be careful:

a) Most academics don’t have access to proper trading infrastructure, so their ideas usually get validated using basic backtesting, which tends to be more optimistic than the cruel reality.

b) For the sake of staying relevant and meeting quotes for grants, universities tend to force academics to publish a white paper; so, there is not necessarily a smart person doing cutting-edge research behind every article. It could be just someone trying to put as little effort as possible into not getting fired.

c) Some white papers are an academic’s attempt to get a job in a hedge fund. Often those articles are more practical, show promising results, and share way fewer details than an average scientific paper. Those white papers are worth attention because people who worked on them got the results, which make them believe they have a shot in the industry. But be careful as point a) still applies.

3. Fundamental literature

There’s a lot of useful information in the fundamental literature on time-series, statistics, and econometrics that is hidden from the public eye. It’s not literally hidden, it’s out there, but there’s just no hype around it as it’s not exciting and it’s all pretty boring stuff. But the deeper we go, the more we find ourselves leaning towards this kind of resource.

4. Medium

At first, I was skeptical of Medium, as it’s full of questionable articles (including this one 🙂 ) like the ones where people are trying to predict stock prices using generic approaches and either fail or get “great results” just to find out in the comments they overfit their model. But with time, I warmed up towards Medium as it has a great recommendation engine, and it’s refreshing to receive a new set of relevant articles every morning. Due to the sheer volume of the articles on Medium, you get exposed to a diverse set of ideas; and it complements deeper but narrower insights from the sources above.

Computational resources

The only thing that matters in regards to a data scientist’s hardware is the feedback loop. If you worked on something today and ran an experiment overnight, ideally, you need to get at least some kind of feedback the next morning so that you could find out whether you’re moving in the right direction or not.

If you need to train models all the time, cloud GPU is either too expensive or free, but not powerful enough, so mostly for R&D we stick to our hardware. For desktops we use 6-cores AMD Ryzen 3600 and Nvidia RTX 2060, and for workstations that are shared among data scientists, we prefer 32-core AMD Threadripper 3970x and Nvidia RTX 2080 TIs, though you can get quite far without a workstation.

Technically Nvidia RTX 20xx GPUs are outdated as Nvidia was rumored to release 7nm GPUs in March with an expected 30–50% increase in performance and memory. The release was delayed, but as China gets back on track, it’s probably a good idea to wait for a little if you are going to buy an expensive GPU.

P.S.

I can’t emphasize enough how grateful I am to the universe this little experiment worked out. Here are some statistics: 80% of our clients are from the US, 45% of them are from New York (the epicenter of coronavirus), 30% of our projects have already been postponed/canceled, and it will get worse.

I put my heart and soul into building and nourishing my team and firing these people would be devastating. Thanks to this project, we will not only be able to keep our most precious talent, but also, we’re even hiring more people to expand into new markets, try new strategies and, if everything goes right, build a tiny hedge fund. In about half a year I will publish an update with the lessons learned no matter if we succeed or fail.

If you’re under quarantine and are looking for something to get busy with, trying to beat the market is a lot of fun. Even if it doesn’t work out, you will get better by pushing your limits. It’s almost a philosophical/spiritual experience. If you decide to take this route, I wish you all the luck you’ll need. But most importantly — stay safe.