Tradingview: Crypto Mean Reversion Strategy

This post contains a simple but complete mean reversion strategy that has been backtested with various cryptocurrencies on the 1-hour time frame. Having said that, there is no reason why this strategy cannot be tweaked to work on other timeframes and assets. The principles remain the same.

So what is mean reversion?

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.

The Strategy

Our strategy will attempt to place trades when a fast exponential moving average has moved a large distance from a slower moving average. In this scenario, the slow-moving average is employed to act as the mean (average price). As such, if recent price action moves very far from it, the theory is that price will eventually revert to the mean. A fast exponential moving average shall be used instead of the closing price to take a little bit of noise out of the price action.

Finally, to try and ensure that price is starting to move back towards the mean, a check final check is performed on the exponential moving average. We shall check to see the fast moving average has been rising (or falling for the short side) for a given number bars before entering. This should avoid instances where price steamrolls through the entry price and continues to head further and further away from the mean.

Special Note

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. A couple of options have been incorporated to help avoid wipeouts such as a stop loss and switches to disable trades in certain directions. Disabling trades in one direction should allow you to switch the strategy to “buy the dips” or “sell the tops”.  The key challenge for you will be to correctly identify when the market regime changes and setup accordingly!

The Code

How to use the code

As noted above, there are two main ways to use this code.  A brief commentary on each method is provided below:

Mean reversion

Mean reversion is configured out of the box. However, that does not mean that there is no work to do. First of all, the percentage deviation from the mean inputs needs to be tweaked according to the characteristics of the instrument you are trading. The second thing you need to do is decide what type of a stoploss (if any) is right for your risk tolerance. When running in mean reversion mode, I have generally prefer a wide stop loss. This is because I often see price can continue to move against the trade for some time before reverting to the mean. As such, I use the stop-loss as a failsafe against a catastrophic event.

Buy the dips / sell the tops

For those of you who are looking to ride a trend. Disabling trading in a single direction works really well in a heavily trending market. It allows you to get discounted entries whilst staying on the right side of the trend. When using this strategy, one must be on the lookout for a regime change.

Don’t forget to use the date inputs

Setting up for all market regimes is tough.  Therefore, when testing use the date inputs to isolate specific regimes. This will give you an idea of how to setup for that particular regime moving forward.

Don’t overfit the moving averages

Finally, when setting up, it can be tempting to tweak the moving averages to eek every last bit of profit from the strategy. Try to avoid this temptation. All you are doing is lining up the MA’s with the peaks and troughs for one set of historical data. Aka “Curve Fitting”.

The moving averages should be employed to measure the distance from the mean. As such you should tweak them in a way that provides the best measurement. For example, adjust them if you want the SMA to represent more of the historical average or tweak the EMA to further reduce the noise.

Build on it

This code is just a starting point. Take a look at the trades and test out ideas that can improve the returns. Again, be careful not to try and turn every trade into a winner. That will lead to overfitting. Be sure to test the improvements on a range of different coins and time frames.

Some Results

Bitcoin out of the box: 20 trades, 75% Strike rate with 42% profit

Crypto Mean Reversion: Bitcoin

Ethereum buy the dip: 22 trades, 86% Strike rate with over 200% profit

Buy the Dip: Ethereum

Monero Out of the box: 28 trades, 67% Strike rate with 75% profit

Crypto Mean Reversion: Monero