I want to chart the Commodity Channel Index in a separate pane and have it apply to a Relative Strength indicator instead of the normal Price Field. Since the built-in CCI function can only be applied to the Price Field, I have tried to code an alternate using loops.
Moderator comment: The original poster is wrong, built-in CCIa works with ANY array. All that is needed is reading the manual: see AFL Function Reference - CCI
I was able to duplicate Amibroker's function in a tedious way (the first part of the code below) by taking the absolute difference between the current period Moving Average of the specified lookback period and the sum of the differences between that and the Close price over the same time frame. However, my alternate calculation using loops to do this (so the lookback period could be easily varied) has not been successful as it does not correctly sum up the differences over the lookback period. I have tried umpteen ways to get it to work using _Trace, Explorations, searching the Forum, etc without achieving the correct result. Any help that someone could give pointing out what is wrong with my code below would be much appreciated.
Russell
///////////////////////////////////////////////////////////////////////////////////
// Calculate Raw CCI
///////////////////////////////////////////////////////////////////////////////////
cciPer = 5;
tp = Close; //(High+Low+Close)/3;
cciMA = MA( tp, cciPer );
///////////////////////////////////////////////////////////////////////////////////
// Manual calculation used to verify Amibroker calculation
///////////////////////////////////////////////////////////////////////////////////
md1 = abs( cciMA - Close ) + abs( cciMA - Ref( C, -1 ) ) + abs( cciMA - Ref( C, -2 ) ) + abs( cciMA - Ref( C, -3 ) ) + abs( cciMA - Ref( C, -4 ) );
meanDev = md1 / cciPer;
cciRaw = ( tp - cciMA ) / ( .015 * meanDev );
///////////////////////////////////////////////////////////////////////////////////
// My alternate calculation using loops
///////////////////////////////////////////////////////////////////////////////////
for( i = 0; i < BarCount; i++ )
{
z = zum = 0;
for( j = 0; j < cciPer; j++ )
{
printf( "i =: %g and j =: %g\n", i, j );
printf( "Close =: %g\n", Ref( Close, j ) );
printf( "cciMA =: %g\n", cciMA[i] );
z = abs( cciMA[i] - Ref( Close, j ) );
printf( "z =: %g\n", z );
zum = Sum( z, cciPer );
printf( "zum =: %g\n", zum );
_TRACE( "zum =: " + zum );
}
}
///////////////////////////////////////////////////////////////////////////////////
// Specify Columns for Exploration
///////////////////////////////////////////////////////////////////////////////////
Filter = 1;
{
if( Status( "actionex" ) == actionExplore )
AddColumn( tp, "tp", 1.4, colorDarkYellow, -1 );
AddColumn( cciMA, "cciMA", 1.4 );
AddColumn( C, "close", 1.4 );
AddColumn( md1, "md1", 1.4 );
AddColumn( meanDev, "meanD", 1.2 );
AddColumn( cciRaw, "CCI Raw", 1.4 );
AddColumn( z, "z", 1.4 );
AddColumn( zum, "zum", 1.4 );
}