Stochastic Slow and OBV Percent Oscillator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Bhangerang

//@version=5
indicator("Stochastic Slow & OBV")

// Setting Inputs
crypto_mode = input.bool(false)
sto1 = input.int(12, "%K Length", minval=1)
sto2 = input.int(5, "%K Smoothing", minval=1)
sto3 = input.int(5, "%D Smoothing", minval=1)
sto4 = input.int(12, "OBV Stochastic Slow %K Length", minval=1)
sto5 = input.int(5, "OBV Stochastic Slow %K Smoothing", minval=1)
obv_period = input.int(380, "OBV Period", minval=1)
obv_signal = input.int(5, "OBV Signal Line Period", minval=1)
obv = ta.obv


// Functions to Use
StochSlow(period1, period2) =>

    // Declare variables
    float numerator = 0
    float denominator = 0
    
    
    // Loop
    for i = 0 to (period2 - 1)
        numerator += close[i] - ta.lowest(low, period1)[i]
        denominator += ta.highest(high, period1)[i] - ta.lowest(low, period1)[i]
        
        
    // Return
    numerator/denominator
    

OBV_Stochastic_Slow(period1, period2) =>

    // Declare variables
    float numerator = 0
    float denominator = 0
    
    
    // Loop
    for i = 0 to (period2 - 1)
        numerator += obv[i] - ta.lowest(obv, period1)[i]
        denominator += ta.highest(obv, period1)[i] - ta.lowest(obv, period1)[i]
        
        
    // Return
    numerator/denominator


// Values
OBV = ((obv - ta.lowest(obv, obv_period))/(ta.highest(obv, obv_period) - ta.lowest(obv, obv_period))) * 100
OBV_slow = OBV_Stochastic_Slow(sto4, sto5)*100
toggled_obv = crypto_mode ? OBV_slow : OBV
signal = ta.sma(toggled_obv, obv_signal)
k = StochSlow(sto1, sto2)*100
d = ta.ema(k, sto3)


// Plot
plot(k, "%K", color=color.orange)
plot(d, "%D", color=color.blue)
h0 = hline(80, "Upper Band")
h1 = hline(50, "Middle Band", color=color.gray)
h2 = hline(20, "Lower Band")
plot(toggled_obv, "OBV", color=color.purple)
plot(signal, "Signal", color=color.red)


// Plot dots
plot(d, "Momentum Dot", (k-d) > 0 ? color.lime : ((k-d) < 0 ? color.red : color.gray), style=plot.style_circles, linewidth=2)
plot(signal, "OBV Dot", (toggled_obv-signal) > 0 ? color.white : ((toggled_obv-signal) < 0 ? color.gray : color.black), style=plot.style_circles, linewidth=2)
