Building an Algorithmic Trading Strategy with Enigma Catalyst

Walking you through the Enigma project and using Catalyst to build an automated trading strategy.

Coinmonks
Published in
6 min readMay 29, 2018

--

Introduction to Enigma

Blockchains, in their current form, don’t handle privacy well. The data that is stored on a blockchain is available for everyone to inspect — a side-effect of radical transparency. Although this is still better than the current data model (whose problems I summed up below), some data is just not suitable to put on display.

Problems with the current model:

  • No Privacy ~ theft (hacks), selling to other parties etc.
  • Lost Natural Income ~ your data is a natural resource
  • Sensitive Product Problem ~ some data is extremely personal
  • Aggregated Power ~ large companies own basically all of our data

Machine Learning, and especially Deep Learning, has given us insights on our data that are invaluable. With our rapid increase in computational power, together with the enormous growth of data, there is no better time to train these models. Deep Learning is on its way to reshape almost every industry out there:

Industries disrupted by Deep Learning [Source: Insight AI whitepaper]

The need for a new way of storing our personal data, that addresses the 4 main problems I mentioned above, on which Machine Learning models can still be trained, is huge. This is where Enigma comes in.

Enigma is a decentralized computation platform with guaranteed privacy. Data can be stored both on the public blockchain (non-sensitive data like ENG token transactions) or on the private Enigma network (sensitive data like medical logs). The private Enigma network architecture, which is not a blockchain, is that of a Distributed Hash Table, which is also a huge part of the InterPlanetary File System (IPFS). Read about that here. You own your data, so in essence, if a large company wants to train a model (like a targeted advertising model), you would sell your data to that company in order for them to use it. Computation on sensitive data is still possible, due Enigma’s secure multi-party computation. If you want to know more about Enigma, read their whitepaper.

Catalyst

Inspired by the rapid growth and proliferation of crypto-assets, we propose Catalyst — the first investment platform that enables developers to build, test, and execute micro crypto-funds.

This extract is from the Catalyst whitepaper, and it pretty much sums up what they aim to do. Catalyst is the first Application on the Enigma protocol, since the crypto-data that powers the platform comes from the Enigma decentralized data marketplace.

It aims to be the one-stop shop for quantitative traders to test their strategies in the crypto-asset domain. It also aims to be a platform on which people can buy strategies from Catalyst developers. The Python Software Development Kit is a Zipline based engine in which strategies can be back-tested or live-traded.

Building the Strategy

The first step will be to install Catalyst on your system. Follow the guidelines their documentation provides here.

What we’ll build is a simple strategy that utilizes the RSI or Relative Strength Index. This is a momentum indicator that signals the strength of price movements. This will be the logic:

  • If we are not in a position and the RSI is oversold (≤ 30), go long
  • If we are in a long position and the RSI reaches 60, close long
  • If we are not in a position and the RSI is overbought (≥ 70), go short
  • If we are in a short position and the RSI reaches 40, close short

These random exit-position numbers (40 for short, 60 for long) are chosen arbitrarily. It’s to make sure that the strategy can actually exit a position once the RSI has pulled back. One thing you may try is to optimize these values for maximal profit. Not like that will be a whole lot.

This is what the complete strategy looks like in Python, and we’ll break it down part by part:

I suggest just copying lines 1 through 12. This will basically be any strategy’s skeleton (along with initialize, handle_data and analyze) and I won’t go deeper into these.

If I run this algorithm with the catalyst.run_algorithm function (providing the right parameters), this is the output. First graph being our portfolio value, second graph price, with the buys and sells displayed as arrows. The third graph plots the RSI, and the last graph shows our percentage gain (Blue) compared to just holding the asset (Orange). Courtesy of our analyze function.

Now let’s break it down. Every Catalyst algorithm requires these 2 functions to be defined:

  • initialize(context)
  • handle_data(context, data)

Let’s start with initialize . When you run an algorithm, Catalyst calls the initialize function and passes in context as a parameter. This parameter can be used to store variables that need to be used in different functions through different iterations. In line 3, we make a variable asset that’s stored in context . To get a currency pair from the Catalyst library you need to use the symbol function and pass in the relevant data. context.i is a variable that will keep track of iterations, while context.base_price will store the initial price of the asset.

After the algorithm has been initialized, the handle_data function is called once every minute or day, depending on what time-frame is specified in run_algorithm . In our case, that’s minute. The same context variable is passed in, together with an object called data , which is updated every iteration containing the current trading bar with open, high, low, close, and volume for the asset we’ve defined.

This function is where all the fun really is. We’ll start by defining the periods for the RSI, which is 14 in most cases. Next, we’ll increment our iterations context.i by 1. Remember, this function is called once every minute, no loop needed. Once we have enough data to actually compute the RSI, meaning 14 data points, we can move on to line 9.

Here we’ll make use of the data object, calling history on it. Depending on how we define the parameters, this will return a number of data points. With frequency , we define how many time-frames (in this case, minutes), one bar consists of. Here this means we’ll be calculating the RSI for the 30 minute chart.

The next step is to compute the RSI. This is how it’s done in Python. If you want to know more about how the RSI is calculated read this. On line 27 we call current price the data object. This way we can access it through a simple variable that gets updated every minute. On line 29 we use that context.base_price variable to get the price on the first candle to use as a reference. Line 32 calculates the price change, which we can use as a benchmark later. In line 34 we use a function called record . This creates a performance perf DataFrame which we can later call in the analyze function for performance analysis. On line 39 through 45 we check statuses; we don’t trade until an order is filled, and we update the status if we can’t trade right now. Next, on line 47, the position amount is stored in a variable stemming from the context.portfolio object.

At the final part of handle_data the strategy logic is determined. The algorithm is now ready.

The final function, analyze , creates several plots on which we can see the performance of our strategy. I won’t go further into this.

The algorithm is now ready for backtesting. Since we made the run_algorithm call at the end of the script we can run it from the command line using python <filename.py> . Make sure you activated the Catalyst environment first with source activate catalyst in the command line and ingesting exchange data with catalyst ingest-exchange -x bitfinex -i btc_usd -f minute . The latter will keep a record of bitfinex price data on your system.

You should now see your performance charts pop up and see your profits or losses! If you want to go deeper into Catalyst and are considering live-trading once you have a good algorithm read their extensive documentation here.

Thanks for reading and consider following for more stories like these.

--

--