Backtesting DeFi Indicators

William Hill
4 min readMar 22, 2023

Create an ETH/USD Trading Bot using AI, ADX, and Open Source software

My previous post demonstrates gathering Crypto pricing data with Alpaca’s Python SDK. Since the final goal is to create my own AI Trading Bot, I need to take this pricing data and find signals that provide quality indications on the movement of the prices. TA-Lib is a well known, open source, industry standard for computing these signals. Unfortunately it is written in C/C++. Luckily there is a nice Python wrapper for TA-Lib. To use the TA-Lib Python wrapper, the TA-LIB native libraries must first be installed. The pypi.org page has instructions for Mac, Linux, and Windows. https://pypi.org/project/TA-Lib/

Keep it Simple Silly -> ADX

After little investigation, Average Directional Index (ADX) has shown promise on a few volatile Crypto assets like Ethereum. Prior to optimizing and scaling TA-Lib on other Cryptos, I need to learn the mechanics and create a simple pipeline for a single ADX signal and token. I will start with ADX and ETH/USD. In another post, I will scale and test more signals, signal parameters, and tokens using AutoML. But first I want to demonstrate a simple signal that is somewhat informative. ADX takes the high, low, and close pricing values as inputs and returns a single value known as the strength of the indicator. More details can be found here. To generate buy and sell signals, ADX needs a Direction Indictor. TA-Lib has Plus Directional Indicator (PLUS_DI) and Minus Directional Indicator (MINUS_DI). Details on how to use these are below.

Crypto Data

Here is a quick and dirty function to grab Crypto pricing data from Alpaca as I explained in this post. The following function returns the data as pandas DataFrame.

Python to grab Crypto Data from Alpaca — carbon.now.sh

ADX + DI

Once we have our pandas DataFrame we can easily calculate ADX and DI as shown:

Calculate ADX & DI carbon.now.sh

We should now see some values in our DataFrame:

pandas DataFrame with ETH/USD pricing data and ADX + DI values

And here are some plots of the closing price and signal values over time.

import pandas as pd

pd.options.plotting.backend = "plotly"

display(df.tail(100)["close"].plot())
display(df.tail(100)[["adx", "plus_di", "minus_di"]].plot())
Plot of ETH/USD Close and ADX+DIs values

So now what?

Now we need to make use of this data. We need to backtest and eventually make trades. But first we need to generate buy and sell signals. There are a few methods of doing this but we will start simple. A threshold value for ADX will trigger our code to generate a buy or sell signal. A higher PLUS_DI will trigger a buy and a higher MINUS_DI triggers a sell signal. The only room for interpretation in this method is what value to set the ADX threshold at. We will start with 40 and optimize for ROI later. Here is the code.

adx_threshold = 45

df['buy'] = (df['adx'] >= adx_threshold) & (df["plus_di"] > df["minus_di"])
df['sell'] = (df['adx'] >= adx_threshold) & (df["plus_di"] < df["minus_di"])
buy” signals

Here is a plot with ADX, +/- DI. The dark er purple area indicates values above 40. The green area is the calculated ADX over time and the blue and red lines indicate positive and negative direction, respectively.

ADX with DI, made with Plotly

Show Me the Trades

And here is a plot of the closing price with buy/sell signals using an ADX threshold of 40. The green lines indicat a profit and the red lines indicate a loss. I normalized the gain between 1 and 6 and used that as the thickness of the red and green trade lines.

Show Me the Gains

This plot show the P&L of trades and ends with an overall ROI of 11.56%.

An ROI of 11.56% sounds good but needs to be compared to simply buying and holding. The ROI on buying and holding is less than 1% in this case. Here is a plot of both strategies. Red is buying and holding, blue letting ADX buy and sell using a threshold of 40.

Deep Dive

In my next post I will do a bit of visual inspection to look for room for improvement. The two improvements I am interested in is maximizing ROI and minimizing negative trades. In my next post I will used the plot below to hunt for better signal~trade correlations and also look for missed opportunities that can be captured with a bit of parameter tuning.

(1) ETH/USD Closing Price with ADX-Based Trades (2) ROI of trades (3) ADX and DI

--

--

William Hill

AI Engineer, Google: Finance, Crypto, ML, Coding, Fantasy Football