Savitzky-Golay Filtered Chande Momentum Oscillator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © profitprotrading
//@version=5
indicator(title="Savitzky-Golay Filtered Chande Momentum Oscillator", shorttitle="SGCMO", format=format.price, precision=0)

src1 = input(close, "Source")

//Savitzky-Golay Filtering credit to © anieri
sgfilter5(x) =>
    (x[4] * -3 + x[3] * 12 + x[2] * 17 + x[1] * 12 + x[0] * -3) / 35 
sgfilter7(x) =>
    (x[6] * -2 + x[5] * 3 + x[4] * 6 + x[3] * 7 + x[2] * 6 + x[1] * 3 + x[0] * -2) / 21
sgfilter9(x) =>
    (x[8] * -21 + x[7] * 14 + x[6] * 39 + x[5] * 54 + x[4] * 59 + x[3] * 54 + x[2] * 39 + x[1] * 14 + x[0] * -21) / 231
sgfilter15(x) =>
    (x[14] * -78 + x[13] * -13 + x[12] * 42 + x[11] * 87 + x[10] * 122 + x[9] * 147 + x[8] * 162 + x[7] * 167 + x[6] * 162 + x[5] * 147 + x[4] * 122 + x[3] * 87 + x[2] * 42 + x[1] * -13 + x[0] * -78) / 1105
sgfilter25(x) =>
    (x[24] * -253 + x[23] * -138 + x[22] * -33 + x[21] * 62 + x[20] * 147 + x[19] * 222 + x[18] * 287 + x[17] * 343 + x[16] * 387 + x[15] * 422 + x[14] * 447 + x[13] * 462 + x[12] * 467 + x[11] * 462 + x[10] * 447 + x[9] * 422 + x[8] * 387 + x[7] * 343 + x[6] * 287 + x[5] * 222 + x[4] * 147 + x[3] * 62 + x[2] * -33 + x[1] * -138 + x[0] * -253) / 5175
//
 
//Chande Momentum Oscillator
length = input.int(9, minval=1, title = "Oscillator Length")
smooth = input(8, "Oscillator Smoothing")
momm = ta.change(sgfilter5(src1))
f1(m) => m >= 0.0 ? m : 0.0
f2(m) => m >= 0.0 ? 0.0 : -m
m1 = f1(momm)
m2 = f2(momm)
sm1 = math.sum(m1, length)
sm2 = math.sum(m2, length)
percent(nom, div) => 100 * nom / div
chandeMO = percent(sm1-sm2, sm1+sm2)
chandeMO:= ta.ema(chandeMO, smooth)


//Plots
ob = plot(80, display = display.none)
ob1 = plot(50, display = display.none)
ob2 = plot(35, display = display.none)
os = plot(-80, display = display.none)
os1 = plot(-50, display = display.none)
os2 = plot(-35, display = display.none)
fill(ob, ob1, color = color.rgb(21, 0, 255, 92))
fill(os, os1, color = color.rgb(0, 208, 255, 92))
fill(ob2, ob, color =color.rgb(21, 0, 255, 95))
fill(os, os2, color =color.rgb(0, 208, 255, 95))

col = chandeMO > chandeMO[1]? color.white: color.rgb(255, 64, 0)
col2 = chandeMO > chandeMO[1] and chandeMO < -50? color.rgb(0, 215, 14, 75): na
col3 = chandeMO < chandeMO[1] and chandeMO > 50? color.rgb(215, 0, 0, 75): na
plot(chandeMO, "Chande MO", color= col, linewidth = 3)
bgcolor(col2, offset = -1)
bgcolor(col3, offset = -1)