Weighted Oscillator Convergence Divergence
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © tseddik

//@version=5
indicator(title="Weighted Oscillator Convergence Divergence", shorttitle="WOCD", overlay=false, timeframe = '', timeframe_gaps = true)

// Inputs
length = input(9, title="Length")
smooth1 = input(5, title="Smoothing 1")
smooth2 = input(7, title="Smoothing 2")
maTypeInput = input.string("Weighted", title="MA Type", options=["Weighted","NonLag","Wilder"])
src = input.source(close, title= "source")
oscDiff = input(9, title="0sc Diff")

col_length = input(#2962FF, "Length  ", group="Color Settings", inline="Length")
col_smooth1 = input(#FF6D00, "Smooth1  ", group="Color Settings", inline="Smooth1")
col_smooth2 = input(#ff00d4, "Smooth2  ", group="Color Settings", inline="Smooth2")
col_grow_above = input(#00ffe5, "Above   Grow", group="Histogram", inline="Above")
col_fall_above = input(#b2dcdf, "Fall", group="Histogram", inline="Above")
col_grow_below = input(#ffcdf0, "Below Grow", group="Histogram", inline="Below")
col_fall_below = input(#ff52bd, "Fall", group="Histogram", inline="Below")

nonLagEMA(src, length) =>
    alpha = 2 / (length + 1)
    nonLag = 0.0
    nonLag := alpha * src + (1 - alpha) * ta.ema(src, length)
    nonLag

Wild_ma(_src, _malength) =>
    _wild = 0.0
    _wild := nz(_wild[1]) + (_src - nz(_wild[1])) / _malength
    _wild

ma(source, length, type) =>
    switch type
        "NonLag" => nonLagEMA(source, length)
        "Wilder" => Wild_ma(source, length)
        "Weighted" => ta.wma(source, length)

// Calculate raw oscillator value
diff = close - src[length]

smoothedOscillator = ma(diff, oscDiff, maTypeInput)

// Apply smoothing
smoothedValue1 = ta.ema(smoothedOscillator, smooth1)
smoothedValue2 = ta.sma(smoothedOscillator, smooth2)

hist = smoothedOscillator - smoothedValue1 


// Plot oscillator and smoothed values
plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below)))
plot(smoothedOscillator, color=col_length, title="WOCD Line")
plot(smoothedValue1, color=col_smooth1, title="Smoothed EMA")
plot(smoothedValue2, color=col_smooth2, title="Smoothed SMA")


// Plot zero line
hline(0, "Zero Line", color=color.gray)