The Disparity Index — Coding in TradingView
How to Code the Disparity Index in Pine Script
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.
Knowledge must be accessible to everyone. This is why, from now on, a purchase of either one of my new books “Contrarian Trading Strategies in Python” or “Trend Following Strategies in Python” comes with free PDF copies of my first three books (Therefore, purchasing one of the new books gets you 4 books in total). The two new books listed above feature a lot of advanced indicators and strategies with a GitHub page. You can use the below link to purchase one of the two books (Please specify which one and make sure to include your e-mail in the note).
Pay Kaabar using PayPal.Me
Go to paypal.me/sofienkaabar and type in the amount. Since it’s PayPal, it’s easy and secure. Don’t have a PayPal…www.paypal.com
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.
Check out my weekly market sentiment report to understand the current positioning and to estimate the future direction of several major markets through complex and simple models working side by side. Find out more about the report through this link that covers the analysis between 07/08/2022 and 14/08/2022:
Coalescence Report 7th August — 14th August 2022
THIS IS A FREE SAMPLE GIVEN TO FREE SUBSCRIBERS THIS WEEK. IF YOU WANT TO REGULARLY HAVE THIS REPORT, MAKE SURE TO…coalescence.substack.com
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
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=4study("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 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
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.