How to create a simple backtesting strategy for your cryptocurrency trading

Backtesting means working with historical data in order to determine potential future profits of your current cryptocurrency trading strategy. This is especially important when you’re running a crypto trading algorithm to do the trading for you. 

While it’s not always indicative of future gains, it’s generally considered good practice to backtest your strategy before you YOLO your lifesavings into Dogecoin. If you’re into that kind of thing, /r/wallstreetbets would like to recruit you.

In this article, I’ll be showing you how to build a simple backtesting framework for your cryptocurrency trading bot. I know, not everyone has a crypto trading bot, that’s why you can use the one I built along with a few other devs, it’s open source and should be easy enough to set up, though there is no user interface at the moment (we’re working on that!) – Binance Volatility Trading Bot.

While you’re at it why not register your interest in my upcoming crypto trading algorithm app complete with a user interface and in depth customisation. Currently looking at releasing this at the end of this year.

Jumping into backtesting

Any backtesting algorithm has two main parts: the data and the engine. 

The Data

This is the historical data that will need to be fed into the script, in order to it to execute buy and sell orders. We’ll be using the open binance endpoints, so you won’t need an API Key or secret (those will be needed for the bot though).

So if you don’t have one or would like to use a different account for the crypto trading bot, use my referral link to get 5% off of your commission fees. Next up is generating an API Key and Secret. 

The first step is to install our dependencies. We’ll be using python-binance for the historical data, backtrader as the framework for own strategy and matplotlib to plot our results. It’s easier if you just download the code from GitHub.

You can do it the easy way:

Or the preferred way: 

We’re now ready to fetch our historical data. This is done in the binancedata.py file. The get_historical_data() function will fetch this data based on your arguments in the maintester.py

 

In maintester.py see the filename var. In this example, we’ll be getting historical data since 1st for January this year for DOTUSDT at an interval of 30 minute (klines or candles). Following the same structure you can add your own arguments.

That’s all the set-up needed for the historical data. The files will be stored on your drive in the same path as the script.

The Strategy

Bear in mind that this is a sample strategy and you should configure this to fit your own strategy. With the current set-up, we’ll be buying every time the current candle (1 minute by default) is by at least 1% higher than the close of the previous candle. The take profit is set to 10% and the stop loss is set to 5%.  Here’s a sample customisation setup:

 

The def next(self): function is where the magic happens. This is where the buying and selling logic is defined. Any substantial alterations, outside the customisation options given above, would need to replace the current code in here:

 

Lastly, we are using a simple cerebro.plot() so that we can see the performance over time.

That’s it. You now have a simple backtesting framework to decide what trading strategy to feed to your crypto trading algorithm. Happy testing!

Did you enjoy this article? Please consider subscribing to the newsletter for more awesome content.

If you appreciate my content and would like to see more, you can support me by donating here or via the Brave Browser. This is much appreciated as it helps me to keep the content free and open source. Thank you!

4 thoughts on “How to create a simple backtesting strategy for your cryptocurrency trading

Leave a Reply

Your email address will not be published. Required fields are marked *