[KVA]K Stochastic Indicator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Kamvia

//@version=5
indicator(title='[KVA]K Stochastic Indicator', shorttitle='KStochastic', overlay=false)

// Input parameters
length = input(14, 'Length')
smoothK = input(1, 'Smooth K')
smoothD = input(3, 'Smooth D')
smoothF = input(21, 'Smooth F')

_src = input.source(close,title="Source")


sma_signal = input.string(title="Stochastic MA Type", defval="EMA", options=["SMA","WMA", "EMA","DEMA"])
// Calculate %K
lowestLow = ta.lowest(_src, length)
highestHigh = ta.highest(_src, length)
k = 100 * (_src - lowestLow) / (highestHigh - lowestLow)

dema(src, length)=>
    e1 = ta.ema(src, length)
    e2 = ta.ema(e1, length)
    ret = 2 * e1 - e2
    ret
ma(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "DEMA" => dema(source, length)
        "EMA" => ta.ema(source, length)
       
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)
// Smooth %K with simple moving average
sk = ma(k, smoothK,sma_signal)

// Smooth %K with simple moving average to calculate %D
sd =ma(sk, smoothD,sma_signal)
sf = ma(sk, smoothF,sma_signal)
// Plotting
plot(sk, color=color.new(color.blue, 0), title='%K')
plot(sd, color=color.new(color.red, 0), title='%D')
plot(sf, color=color.new(color.black, 0), title='%F')
h0 = hline(80, "Upper Band", color=#787B86)
hline(50, "Middle Band", color=color.new(#787B86, 50))
h1 = hline(20, "Lower Band", color=#787B86)
fill(h0, h1, color=color.rgb(33, 150, 243, 90), title="Background")


stRD =  sk ==100 
stGD =  sk == 0 
stOBOS = true
plot(stOBOS ?  stRD ? sk : na:na, color=color.red, style=plot.style_circles, linewidth=3)
plot(stOBOS ?  stGD ? sk : na:na, color=color.green, style=plot.style_circles, linewidth=3)
