Porting a backtrader strategy
Abstract
In this notebook we'll see how to port a strategy from Backtrader to Vectorbt. We'll also see how to compare and troubleshoot the two versions
Variables Setup
We'll setup some parameters here. Remember that you have to use a symbol available from the broker you're going to download data from (Binance in this case). As instance Binance provides a BTC/USDT symbol but doesn't provide an BTC/USD
Download Data
You can either download data using the binance client...
...or using CCXT (here we're using both for demonstration purposes but you can just pick the one you prefer)
Let's keep only the columns we're interested into
And make sure the timeframe is the one we specified
Let's plot the price graph!
Backtrader Strategy
We start by backtasting a simple RSI strategy which buys when < 35 and sell when > 70. We'll use backtrader as starting point.
The StrategyBase will deal with most of the routine most common logic. The real trading decisions will be taken in the BasicRSI class.
Remember to add the strategy to cerebro
Run the backtesting
Define some convenience functions for printing a report. Let's also print the actual report and plot a graph
Let's create a vectorbt portfolio using the entries and exits from backtrader...
...and compare the commissions.
We can see they are extremely close (zero dot, followed by 40 other zeros and finally some meaningful digits). This is mostly because of rounding errors for example due to the order calculations are performed but is mostly negligible for our purposes. In fact if we print the portfolio report from both vectorbt and backtrader and we can see the result is almost identical
Let's plot a graph with the entries and exits
Pure vectorbt strategy
We'll try now to execute the same strategy but using only vectorbt. Let's start by creating an RSI indicator. It'll be of course on a rolling window, meaning it will represent the value which the RSI would have been at that point in time (as opposed of being calculated considering the current timestamp or the last one as you would normally do in a realtime scenario)
We plot entries and exists as before...
...and print the Final Portfolio Value
Something's wrong! Why do we get a different result when using backtrader and vectorbt? Let's start by comparing the entries and exits ('^' means XOR logical operator: It returns true when the two inputs are different)
Definitely some values are different and why backtrader did not exit with an RSI greater than 70?
vectorbt strategy with backtrader indicators
Let's dig deeper: We can compare the vectorbt and backtrader RSI indicator and see if there's any difference. We start by importing them as a dataframe
And plot the difference results
We can clearly see there's a ± ~2 difference in value overall. The initial spike is due to backtrader not having enough price info at the beginning, which are instead available to vectorbt as we trimmed the data after generating the series of points. We can print the signals both separately and overlapped
No appreciable difference though. So, how can we achieve the same results? We could try to feed backtrader's RSI signal to vectorbt's strategy
and print the difference between entries and exits
Nice! no difference in entries and exits events.
We can print them on a binary Y axis as well
So now when we go and print the Final Portfolio Value...
...we can indeed see the results are matching! Let's also plot the position trading windows
Conclusions
If we now create a portfolio from a simple holding strategy
and plot the portfolio value on the same graph
We can see the portfolio generated with the vectorbt + backtrader RSI signal exactly overlaps with the portfolio we generated from pure backtrader strategy. The pure vectorbt portfolio is slightly off though as we find out. This should remind you that tiny differences in the ways signal algorithms are implemented, can even generate different entries and exits events in your strategy!
Bonus debugging snippets
here are some snippets which might come in handy when debugging or troubleshooting strategies