Trend Oscillator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © faytterro

//@version=5
indicator("Trend Oscillator", timeframe = "", timeframe_gaps = true)
srctcr=input(hlc3,title="source")
lentcr=input.int(25,title="lenght", maxval=1000)
cr(x, y) =>
    z = 0.0
    w = 0.0
    for i = 0 to y-1
        z:=z + x[i]*(-math.cos((22/7)*2*(i)/(y+1))*(y+1)/4+(y+1)/4)
        w:= w + (-math.cos((22/7)*2*(i)/(y+1))*(y+1)/4+(y+1)/4)
    z/w
cr= cr(srctcr,2*lentcr-1) 
mom = ta.change(cr)
ac = ta.change(mom)
x = (mom>0 and ac>0)? 2 : 
 (mom>0 and ac<0)? 1 : 
 (mom<0 and ac<0)? -2 : 
 (mom<0 and ac>0)? -1 : na
y = ta.cum(x)
plot(y, color = x>1? color.rgb(0, 255, 0) : x<-1? color.rgb(255, 0, 0) :
 x>0? color.rgb(155, 255, 0) : color.rgb(255, 155, 0), linewidth = 2)
plot(ta.sma(y,100), trackprice = true, display = display.none, color = color.rgb(255, 255, 255))
dip = x[1]==-1 and x==2
tepe = x[1]==1 and x==-2
bgcolor(color= dip? color.green : tepe? color.red : na, display = display.none)
alertcondition(dip, "Trend Oscillator swing low alert")
alertcondition(tepe, "Trend Oscillator swing high alert")