#Indicator

#PARAM "Length", 20

Dim fPrice as Single
Dim fSMA as single
Dim fSlope as Single
Dim fSmoothSlope as Single
Dim fITrend as Single

Dim fAlpha as Single
Dim fHP as Single
Dim fSmoothHP as Single
Dim fPI as Single

Dim fModel as Single

fPrice = (H+L)/2
fSMA = SMA(fPrice, Length)
fSlope = fPrice - fPrice[Length - 1]
fSmoothSlope = fSlope + 2 * fSlope[1] + 2 * fSLope[2] + fSlope[3]
fSmoothSlope = fSmoothSlope/6
fITrend = fSMA + 0.5 * fSmoothSlope

fPI = 3.1415926
fAlpha = (1 - Sin(2 * fPI/Length))/Cos(2 * fPI/Length)
fHP = fPrice - fPrice[1]
fHP = 0.5 * (1 + fAlpha)* fHP + fAlpha * fHP[1]

fSmoothHP = fHP + 2 * fHP[1] + 2 * fHP[2] + fHP[3]
fSmoothHP = fSmoothHP/6

fModel = fITrend + fSmoothHP

PlotPrice( "Model", fModel, Red )
PlotPrice( "Instant Trendline", fITrend, Blue)
PlotHist( "Cyclic Component", fSmoothHP, 0, Yellow )


Return 0     ' Return the value calculated by the indicator