Backtrader: Bollinger Mean Reversion Strategy

Bollinger bands by design have all the elements needed to implement a complete mean reversion strategy. The Bollinger’s middle line is a simple moving average which is suitable for representing the mean. Furthermore, the upper and lower bands represent a standard deviation above/below the median line. This is ideal for indicating when price has moved away from the mean.

Mean Reversion for beginners

Mean reversion is simply a nice way to describe something that is moving back (reverting) to an “average” price (the mean). Generally, a trader will employ a mean reversion strategy when the price of asset/stock/currency has moved quite far away from the historical average and there is a belief, it will eventually move back.

User beware!

Mean reversion strategies can be risky if you are placing trades on the wrong side of a heavily trending market. As such, this strategy should be employed with care.

The Strategy

The strategy we will implement shall attempt to capture reversion to the mean (middle line) once price moves beyond the upper or lower bands. In order to try and capture as much of the move as possible, the strategy will use stop orders to enter a position right at the moment price retouches the outer bands (only after already having moved beyond them). This does have a potential downside though. Price might move back inside the band before reversing and continuing to move away from the mean. As such, some people may prefer to wait until price closes inside band.

To exit the positions, limit orders shall be used with a limit price equal to the middle line. This will exit as soon as price has reverted to the mean.

The Code

You might notice that is frequently used to cancel all open orders. This is because, at the time of writing, Backtrader does not have a supported method to amend open orders. Another thing to be aware of is that most stores (IB, Oanda etc) do not have support for the get_orders_open()method. Therefore, this strategy can not be used live without some tweaks.

Some Results

In the interests of fairly showing the strengths and weaknesses of this strategy, one of the results will show the strategy running in an ideal environment whilst the other example was run in sub-optimal conditions. It should be pointed out that a fixed position size was used for testing. As such, the focus should be on the strike rate and dollar differences between winners and losers instead of the actual PnL value.

Ford 2016 – 2017

A good example of a ranging equity. Lots of nice positive trades. Volatility was nice during this period with the price often reverting to the mean.

Results of backtesting Ford

Amazon 2017-2018

This is a good example of a strong trending stock. In this scenario, the strategy just about broke even. If given more trades, I suspect it would have been a losing strategy over the long term. In general, the bands were quite tight during Amazons’ steady uptrend. This resulted in a large losing position where price only just broke above the band and initiated a short position. The stock then continued to rise upwards for quite a long time.

Results of backtesting Amazon

Find This Post Useful?

If this post saved you time and effort, please consider support the site! There are many ways to support us and some won’t even cost you a penny.