// KCCI - Adaptive CCI by Karthimarar
// Details at website
www.tatechnics.in
_SECTION_BEGIN("Dominant Period");
SetBarsRequired( 1000, 1000);
prc = ( High + Low ) / 2;
Cyclepart = 0.7 ;
Smooth[0] = Detrender[0] = I1[0] = Q1[0] = jI[0] = jQ[0] = I2[0] = Q2[0] = Re[0] = Im[0] = Period[0] = SmoothPeriod[0] = 0;
for ( i = 6; i < BarCount; i++ )
{
Smooth
= ( 4 * prc + 3 * prc[i-1] + 2 * prc[i-2] + prc[i-3] ) / 10;
AmpCorr = 0.075 * Period[i-1] + 0.54;
Detrender = ( 0.0962 * Smooth + 0.5769 * Smooth[i-2] - 0.5769 * Smooth[i-4] - 0.0962 * Smooth[i-6] ) * AmpCorr;
Q1 = ( 0.0962 * Detrender + 0.5769 * Detrender[i-2] - 0.5769 * Detrender[i-4] - 0.0962 * Detrender[i-6] ) * AmpCorr;
I1 = Detrender[i-3];
jI = ( 0.0962 * I1 + 0.5769 * I1[i-2] - 0.5769 * I1[i-4] - 0.0962 * I1[i-6] ) * AmpCorr;
jQ = ( 0.0962 * Q1 + 0.5769 * Q1[i-2] - 0.5769 * Q1[i-4] - 0.0962 * Q1[i-6] ) * AmpCorr;
I2 = I1 - jQ;
Q2 = Q1 + jI;
I2 = 0.2 * I2 + 0.8 * I2[i-1];
Q2 = 0.2 * Q2 + 0.8 * Q2[i-1];
Re = I2 * I2[i-1] + Q2 * Q2[i-1];
Im = I2 * Q2[i-1] - Q2 * I2[i-1];
Re = 0.2 * Re + 0.8 * Re[i-1];
Im = 0.2 * Im + 0.8 * Im[i-1];
if ( Im != 0 AND Re != 0 )
Period = 360 / atan( Im / Re );
if ( Period > 1.5 * Period[i-1] ) Period = 1.5 * Period[i-1];
if ( Period < 0.67 * Period[i-1] ) Period = 0.67 * Period[i-1];
if ( Period < 6 )
Period = 6;
if ( Period > 50 )
Period = 50;
Period = 0.2 * Period + 0.8 * Period[i-1];
SmoothPeriod = 0.33 * Period + 0.67 * SmoothPeriod[i-1];
}
_SECTION_END();
_SECTION_BEGIN("Variable period CCI");
function MeanDev( array, mean, range )
{
result = 0;
for( i = LastValue( range ) ; i < BarCount; i++ )
{
result[ i ] = 0;
// the mean is not 'moving' over the range (outside the loop)
tm = mean[ i ];
for( j = 0; j < range[ i ] AND ( i - j ) >= 0 AND ( i - j ) < BarCount; j++ )
{
result[ i ] = result[ i ] + abs( array[ i - j ] - tm );
}
result[ i ] = result[ i ]/range[ i ];
}
return result;
}
function VarCCI( array, period )
{
SMATP = MA(array,period );//1,2
MD = MeanDev( array, SMATP, period );
KCCI = (Avg - SMATP) / (0.015 * MD);
return KCCI;
}
sp=int(SmoothPeriod*Cyclepart);
lcol = IIf( VarCCI(Avg, Sp) > Ref( VarCCI(Avg, Sp), -1 ), IIf( VarCCI(Avg, Sp) > 0, 27, 27 ), IIf( VarCCI(Avg, Sp) > 0, 32,32 ) );// 27 43 11 32
//mcol =IIf(VarCCI(Avg, Sp)>0,colorGreen,colorRed);
Plot(VarCCI(Avg, Sp),"KCCI",lcol,2|styleThick);
Plot(VarCCI(Avg, Sp),"",lcol,1|styleThick);