farzinsabbagh

KDJ stochastic indicator

This is a special calculation of KDJ indicator. As you may know this is based on stochastic indicator. Stochastic indicator is a method to normalize a trending time serie (here price). the calculation of stochastic itself is a built in function in pine but it is straight forward:
In sudo code:
RSV for n days=(Cn-Ln)/(Hn-Ln)×100
In which, Cn is the closing price on the nth day; Ln is the lowest price in n days; Hn is the highest price in n days.
To calculate other indices K, D and J we use this formulas:
K = (2/3) * K + (1/3) * RSV
D = (2/3) * D + (1/3) * K
J = 3 * K - 2 * D
As you can see it is a recursive calculation. It means any value of the indices are affected by it's own previous value (and I'm passionate about recursive functions!) It may concern you that in the initial calculation there is no previous value and you are right. For the initial values we use value of 50 because it is an oscillator and the mean value is always 50 so we replace the first NAs with 50 using nz() function in pine.
After doing this calculations we reach to the smoothing section. I used simple moving averages, you may replace it with other more advanced smoothing techniqes like EMA or ALMA.
After I wrote this indicator I saw that it is a good indicator for reading divergences. As you can see I showed couple of these divergences to you on the chart. Notice that I analyzed divergence between price and J (very light green) line and not to K or D. I really appreciate any suggestion on this indicator and hope to improve it. The other ones present in the public library wasn't good and they differ a lot in the calculation and also the graphics doesn't look good.
Be free to change the parameter i saw these parameter are good to daily Bitcoin chart.
Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in a publication is governed by House Rules. You can favorite it to use it on a chart.

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.

Want to use this script on a chart?