Validating a Cryptocurrency Trading Strategy Using the Gatsiva API

Written by gatsiva | Published 2018/08/30
Tech Story Tags: bitcoin | trading | cryptocurrency | data-science

TLDRvia the TL;DR App

A use-case and walkthrough of how we used the Gatsiva API to evaluate a proposed trading strategy for 0x (ZRX).

At Gatsiva, we’ve been applying machine learning to markets for over 15 years with equities, forex, and most recently, cryptocurrencies. We provide an API that helps cryptocurrency traders become more confident in their trades by leveraging technical analysis to generate buy or sell signals.

In this article we show how the Gatsiva API was used by one of our collaborators to gain more confidence in an approach to trading 0x (ZRX).

The Proposed Strategy

A collaborator on our platform recently floated the question: Can the slope of the MACD Histogram predict shifts in price of cryptocurrencies? Would this provide advance insight for price declines, price increases, or both? In particular, would this strategy work well for the coin in question — 0x (ZRX) priced in Bitcoin (BTC)?

Illustration of MACD Histogram Slope Crossover Strategy

The first step in the analysis was to design the condition in the Gatsiva’s natural language for condition definition that would identify these signals accurately. Utilizing the condition language we helped this collaborator specify the condition they were describing to define the strategy. The pair of rules were easily defined as follows:

Rule 1 (Short): macd histogram(12,26,9) > 0 and slope of macd histogram(12,26,9) over 1 period crosses below 0

Rule 2 (Long): macd histogram(12,26,9) < 0 and slope of macd histogram(12,26,9) over 1 period crosses above 0

The second step was to analyze the return profile of the conditions. Using our API, we can easily bring in the data on those returns and generate the following visualizations; one for each condition we’ve defined.

Rule 1 (SHORT) Percentile Return Profile

Rule 2 (LONG) Percentile Return Profile

These visualizations show the distribution of observed returns after the conditions we defined above have occurred in the past. These returns are shown over a number of time periods (days, in this case). That’s a mouth full. Basically the visualizations show, for the conditions above, when they occurred in the past, what happened to the price next after 1 day? 7 days? 21 days? In the graph above, we can see that the long-side condition has been true 19 times in the past, and in general, the price of 0x (ZRX) priced in Bitcoin (BTC) has generally declined, at least up to a month later.

On the other hand, we can see that when the short-side condition occurs, based on 18 previous instances, the price of 0x (ZRX) priced in Bitcoin (BTC) has generally increased. Again, at least up to about three weeks later.

This is an interesting outcome for our collaborator’s trading idea. They condition wanted to analyze on the long side predicted a falling price, and the condition they wanted to analyze on the short side predicted a rising price. The conditions were well paired, but the actual outcome was exactly the reverse of what our collaborator assumed. Running this analysis was valuable insight into a proposed trading strategy that may have resulted in losses

For a more detailed explanation of these confidence band charts and how to create them, take a look at our prior articles: Trading Cryptocurrency with Confidence Bands and Gatsiva + Python: Visualizing Bitcoin Corrections. To jump right into code, you can also check out our open source Python Notebooks.

Usually MACD histogram being positive and sloping downward is a sign of overbought scenarios. Likewise, the opposite being a sign of an oversold scenario. But in this case, the return profile indicates precisely the opposite for 0x (ZRX) priced in Bitcoin (BTC).

When the MACD Histogram was positive and the slope shifted downward (Rule 1), returns were often higher in the next few days indicating this would be a better LONG rule than a SHORT rule. Similarly, when MACD Histogram was negative and the slope shifted upward, returns continued to be negative — indicating that this was a better SHORT rule than a LONG rule.

This is the first superpower of the Gatsiva API — the ability to show real returns after an event. This can help support or quickly debunk trading ideas, especially those that have classically worked in other markets like the stock market.

Simulating Trades

Next was the trade simulation. At Gatsiva, we’ve built an internal trade simulator that allows us to use 5-minute interval data from Binance to simulate trades from signals which can be defined in the Gatsiva condition language.

Since our main data source is Cryptocompare, using a separate data source to simulate trades is necessary to minimize data snooping bias as well as a way to get a more realistic trade entry experience by using more granular time slices.

We assumed a starting portfolio of 10 BTC, allowing a maximum of 5 simultaneous open positions, each with 20% available capital applied to a new position. Lastly, we assumed a commission cost of 0.1%.

Our actual condition signals are delayed slightly since the source data is slightly delayed. Also, it takes a small amount of time to process the data and generate signals in our engine. These delays are built into our model and are included in our simulation to ensure a more realistic trading scenario. The following rules are also included in our simulation:

Trade Entry Rules

The exit criteria was simple: Apply a 15% trailing stop and a 20% profit target, but hold the position no longer than 72 hours. The simulation updates the trailing stop loss as positions become profitable. If the stop loss is triggered or we reach the profit target, the simulation sells at the target price. If we reach the maximum hold time, the simulation sells at the average of the open, close, and low observed during the 5 minute tick interval. These exit rules are applied to both LONG and SHORT positions.

Note: This equates to a maximum of 3% loss of total value on any 1 trade when combined with our position sizes (15% trailing stop x 20% overall position size).

Lastly, we chose the data from 2018–01–01 to 2018–08–09 (date we performed the analysis). This was to ensure some history was available from 0x before beginning technical analysis.

The Results

The overall results for 0x (ZRX) were impressive and are shown in the tables and charts below. We were able to realistically enter 78% of the trades generated from our signals and make over 14% profit after commissions. The lowest our simulated portfolio ever reached during this time period was 9.81 BTC (about -1.9% of our original starting capital).

Table of Simulation Results

Portfolio Value and Open Positions Over Time

P/L of Each Trade

For the detailed list of simulated trades and timestamps, please take a look at this gist on Github.

Criticisms

From these results, we of course can anticipate many criticisms, and some of them will be fair. To head of two of them in particular — time-frame selection and coin selection — we offer the following:

It’s possible that our timeframe selected skews the results. Note that during this simulation period of 2018–01–01 to 2018–08–09, 0x (ZRX) had an overall price increase in BTC of nearly 132%. We must recognize that these results might be just “lucky” and that our results benefit from the run up.

In order to test this, we selected another time period that most closely matches the closing price at 2018–08–09 and find that 2018–02–02 would be a good starting point to test our “luck”. Below is a comparison of selecting a relative “flat” return period also with positive results.

Comparison of Simulation Results to Flat Time Period

In the table above, we can see that even in a time period where the return was slightly negative, this strategy produced positive results.

Another fair criticism is the selection of 0x (ZRX) for this simulation instead of more mainstream coins. However, remember that before entering this strategy, we validated our assumptions on the viability of this MACD Histogram Slope Crossover signals as applied to 0x (ZRX).

That said, it’s interesting that similar results can be observed for Ethereum (ETH), Ethereum Classic (ETC), and Litecoin (LTC) over the same time period.

Comparison of Simulation Results to Other Coins

In the table above we can see that for ETH, ETC, and LTC we get positive results in a mixed environment by coin. Ethereum and Ethereum Classic were relatively flat during this time period, but the simulation outperformed in both scenarios. Litecoin lost over 43% of its value during this time period, but the MACD Histogram Crossover strategy actually made 3.28% after commissions in this environment.

Making marginal gains in a down market … we’ll take it over losing money any day.

Putting the Simulation Into Action

What can we do with this condition analysis and simulation results? These two conditions have been added to our Insight Engine and are now being constantly re-evaluated in realtime as new market data is produced. Any relevant signals based on our models are then fed to our collaborators as a signal available for discussion on our platform.

It’s important to note that this strategy will not work for all coins in all cases. This brings up the second superpower of the Gatsiva platform — the ability to continually track and update which signals work for which coins. Carefully applied and validated with statistical tools in real time, such signals can be a powerful addition to one’s trading strategy.

Interested in Tools For Your Own Strategies?

Come and visit us at gatsiva.com, take a look at the documentation to our API and get building your own strategies, and be sure to sign up for our collaboration platform. We’re always extending our API based on collaborator feedback.

Finally, if you like what you just read: Feel free to bookmark this article, clap, or leave a comment! It helps us help more people!


Published by HackerNoon on 2018/08/30