_SECTION_BEGIN("Trend Intensity Index"); SetChartOptions(0,chartShowArrows|chartShowDates); _N(Title = StrFormat("{{NAME}} – {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) )); Plot( Close, "Price", colorWhite, styleCandle ); n = Param("Periods",10,5,100,5); //Calculate Simple Moving Average (SMA) of the closing prices SMA=MA(Close,n); //Calculate Positive and Negative deviation of the closing prices from SMA calculated in step 1 Dev = Close - SMA; posDev = IIf( Dev>0, Dev, 0 ); negDev = IIf( Dev<0, abs(Dev), 0 ); //Calculate sum of Positive and Negative deviations for the period twice smaller than period used for SMA calculation. if ( n%2 == 0) m = n/2; else m = (n+1)/2; SDpos = sum(posDev, m); SDneg = sum(negDev, m); //Calculate Trend Intensity index TII = 100 * (SDpos) / (SDpos + SDneg); //Calculate second line (Signal line) as EMA applied to TII TIISignal = EMA(TII, 9); Plot( TII , "TII", colorWhite, styleLine | styleOwnScale ); Plot ( TIISignal ,"TII Signal", colorBlue, styleLine | styleOwnScale ); _SECTION_END();