Distance analysis is sometimes very important to determine whether the market is about to reverse or correct. Statistically, we expect to remain in normality more often than extremes, therefore, when we do reach the extremes, we expect some form of reversion to the mean unless Euphoria is in hyperdrive and participants are excessively buying or selling following big news. In this article, we will discuss and code a simple indicator called the Disparity Index.
I have just published a new book after the success of my previous one “New Technical Indicators in Python”. It features a more complete description and addition of structured trading strategies with a GitHub page dedicated to the continuously updated code. If you feel that this interests you, feel free to visit the below link, or if you prefer to buy the PDF version, you could contact me on LinkedIn.
The Disparity Index
Created by Steve Nison, the Disparity Index is a very simple calculation that measures the distance between the price and its moving average using the following formula:
It is generally believed that the index can be used in the following ways:
Surpassing the zero line signals a bullish bias while breaking the zero line signals a bearish bias.
We can calculate a short-term moving average on the Disparity Index where its surpass of its moving average signals a bullish bias while its break signals a bearish bias.
We can form subjective boundaries such as 0.20 and -0.20 from where reversal ideas can be formed.
We will see in the next section how to code the indicator from scratch.
Creating the Disparity Index in TradingView
We want to create and plot the Disparity Index on the AUDCAD values loaded on the TradingView platform. Note that you must create an account to be able to view the charts, the good news is that it is free. Locate the Chart button on the home screen and then choose any asset you would like to calculate the indicator on. Now, on the bottom of the screen, locate Pine Editor and warm up your fingers to do some coding.
The first step is to specify the version of Pine Script. In our case it is 4.
//@version = 4
The next step is to specify the name of the indicator (Script) by using the following syntax
study("Disparity Index")
Having done the introductory part, we can proceed by defining the parameters of the index.
lookback = 13, lookback_signal = 6
Now, we are set to transform the data using a moving average. What if we use a weighted moving average? The below shows that we want to calculate in parallel a 13-period weighted moving average. Then, we we will use the formula of the index.
moving_average = wma(close, lookback)
disparity_index = ((close - moving_average) / moving_average) * 100
One last calculation remains and it is the short-term weighted moving average calculated on the Disparity Index.
moving_average_di = wma(disparity_index, lookback_signal)
And finally, we are set to plot the indicator alongside the chart. This is done using the plot() function. It takes a source and a color property as shown below. We can also use the hline() function to plot a horizontal line as a subjective barrier.
plot(disparity_index, color = color.green)
plot(moving_average_di, color = color.red)
hline(0.20, color = color.gray, linestyle = hline.style_dashed)
hline(-0.20, color = color.gray, linestyle = hline.style_dashed)
The full code can be found below.
//@version=4
study("Disparity Index")
lookback = 13
lookback_signal = 6
moving_average = wma(close, lookback)
disparity_index = ((close - moving_average) / moving_average) * 100
moving_average_di = wma(disparity_index, lookback_signal)
plot(disparity_index, color = color.green)
plot(moving_average_di, color = color.red)
hline(0.20, color = color.gray, linestyle = hline.style_dashed)
hline(-0.20, color = color.gray, linestyle = hline.style_dashed)
If you are also interested by more technical indicators and using Python to create strategies, then my best-selling book on Technical Indicators may interest you:
Conclusion
Remember to always do your back-tests. You should always believe that other people are wrong. My indicators and style of trading may work for me but maybe not for you.
I am a firm believer of not spoon-feeding. I have learnt by doing and not by copying. You should get the idea, the function, the intuition, the conditions of the strategy, and then elaborate (an even better) one yourself so that you back-test and improve it before deciding to take it live or to eliminate it. My choice of not providing specific Back-testing results should lead the reader to explore more herself the strategy and work on it more.
To sum up, are the strategies I provide realistic? Yes, but only by optimizing the environment (robust algorithm, low costs, honest broker, proper risk management, and order management). Are the strategies provided only for the sole use of trading? No, it is to stimulate brainstorming and getting more trading ideas as we are all sick of hearing about an oversold RSI as a reason to go short or a resistance being surpassed as a reason to go long. I am trying to introduce a new field called Objective Technical Analysis where we use hard data to judge our techniques rather than rely on outdated classical methods.