KDJ stochastic indicator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © farzinsabbagh
// This Calculation is Based on this post https://www.learnforexc.com/what-is-the-kdj-stochastic-oscillator-and-the-calculation-method-of-the-kdj-indicator/
//@version=4
study("KDJ stochastic indicator",shorttitle="KDJ",overlay=false)
RangeLength = input(14, title="Range Length(K Length)", minval=1)
KsmoothLength = input(3, title="K Smoothing Length", minval=1, type=input.integer)
DsmoothLength = input(3, title="D Smoothing Length", minval=1, type=input.integer)
JsmoothLength = input(3, title="J Smoothing Length", minval=1, type=input.integer)

//immature stochastic index or RSV
RSV = stoch(close, high, low, RangeLength)

var K = 0.0
var D = 0.0
K := (0.666)*nz(K[1],50)+(0.334)*nz(RSV,50)
D := (0.666)*nz(D[1],50)+(0.334)*nz(K,50)
J = sma(3 * K - 2 * D,JsmoothLength)

// Colors and Plots
K_Color = color.rgb(26, 147, 111,0)
D_Color = color.rgb(136, 212, 152,0)
J_Color = color.rgb(198, 218, 191,0)
h0 = hline(80, "UpperBought",editable=false)
h1 = hline(20, "OverSold",editable=false)
h2 = hline(50, "Middle Line",editable=false)
hzero = hline(0,  "Zero Line",editable=false)
hmax = hline(100,"Max Line",editable=false)
K_Smoothed = sma(K,KsmoothLength)
D_Smoothed = sma(D,DsmoothLength)
Buy = crossover(J,0)
Sell = crossunder(J,100)
fill(h0, h1, color=color.rgb(17, 75, 95, 90), title="Background",editable=false)
fill(hzero,h1,color=color.green,editable=false)
fill(hmax,h0, color=color.red,editable=false)
plot(K_Smoothed, color = K_Color,title="K")
plot(D_Smoothed, color = D_Color,title="D")
plot(J, color = J_Color,title="J")