Moving averages and volatility bands work well together. Many strategies attempt to combine them to harness their combined power. This article discusses one of those strategies.
For a detailed and thorough collection of contrarian trading strategies, you can check out my book. The book features a huge number of classic and modern techniques as it dwelves into the realm of technical analysis with different trading strategies. The book comes with its own GitHub.
Contrarian Trading Strategies in Python
Amazon.com: Contrarian Trading Strategies in Python: 9798434008075: Kaabar, Sofien: Booksamzn.to
Moving Averages and Bollinger Bands
Moving averages help understand the current trend. They are used as regime detectors. A moving average is calculated as the sum of the values divided by their quantity. Bollinger bands are a type of volatility bands that track the price using moving averages and standard deviation (a proxy for volatility).
Bollinger bands accompany the price by forming dynamic barriers with the aim to trap it inside (using insights from descriptive statistics). Take a look at the following Figure. Notice how the market remains generally within the bands.
Moving averages on the other hand do not envelop price but simply show the dominant trend as can be seen in the next chart.
Coding the Strategy in TradingView
The strategy uses the following conditions:
A long signal is generated whenever the market shapes a low lower than the current moving average but closes above it. At the same time, the market should do the same thing to the lower Bollinger band.
A short signal is generated whenever the market shapes a high higher than the current moving average but closes below it. At the same time, the market should do the same thing to the upper Bollinger band.
The code of the strategy to give the signals is as follows:
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Sofien-Kaabar
//@version=5
indicator("The Light Touch Strategy", overlay = true)
lookback = input(defval = 200, title = 'Lookback Long-Term MA')
ma = ta.sma(close, lookback)
lower_bollinger_band = ta.sma(close, 20) - (2 * ta.stdev(close, 20))
upper_bollinger_band = ta.sma(close, 20) + (2 * ta.stdev(close, 20))
buy = low < ma and close > ma and low < lower_bollinger_band and close > lower_bollinger_band
sell = high > ma and close < ma and high > upper_bollinger_band and close < upper_bollinger_band
plotshape(buy, style = shape.triangleup, color = color.green, location = location.belowbar, size = size.small)
plotshape(sell, style = shape.triangledown, color = color.red, location = location.abovebar, size = size.small)
The following Figure shows multiple signals from the strategy. The advantage of the strategy is that it forces you to be on the side of the trend as implied by the 200-period moving average.
If you want to see how to create all sorts of algorithms yourself, feel free to check out Lumiwealth. From algorithmic trading to blockchain and machine learning, they have hands-on detailed courses that I highly recommend.
Learn Algorithmic Trading with Python Lumiwealth
Learn how to create your own trading algorithms for stocks, options, crypto and more from the experts at Lumiwealth. Click to learn more
Summary
To sum up, what I am trying to do is to simply contribute to the world of objective technical analysis which is promoting more transparent techniques and strategies that need to be back-tested before being implemented. This way, technical analysis will get rid of the bad reputation of being subjective and scientifically unfounded.
I recommend you always follow the the below steps whenever you come across a trading technique or strategy:
Have a critical mindset and get rid of any emotions.
Back-test it using real life simulation and conditions.
If you find potential, try optimizing it and running a forward test.
Always include transaction costs and any slippage simulation in your tests.
Always include risk management and position sizing in your tests.
Finally, even after making sure of the above, stay careful and monitor the strategy because market dynamics may shift and make the strategy unprofitable.