here is my crack at it. it plots bullish divergence with $ and bearish
divergence with #
pl let me know if you find any issues with it as i am trying to test it.
Pl note. it looks ahead by 1 day to determine the turning points.
_SECTION_BEGIN( "CCI_Divergence" );
array = CCI();
Color = colorBlue;
Plot( array, "", colorDefault, styleLine | styleThick | styleNoLabel);
Plot( array, "", Color, styleHistogram );
_SECTION_END();
Change = MA( abs( 100 * ( array - Ref( array, -1 ) ) / array ), 14 );
/*
adjz = Param( "Zig", 10, 0, 15.0, .1 );
adjc = Param( "Back", 0, 0, BarCount, 1 );
CAdj = Ref( array, -adjc );
Graph1 = CAdj;
Zigzag = Zig( array, LastValue( Change ) );*/
//Plot( Zigzag, "Zigzag", colorBlack, styleNoTitle | styleLine );
//Peakshape=IIf(Peak(array, LastValue(Change),1),shapeStar,shapeNone);
//Peakvalue=Peak(array, LastValue(Change),1);
//PlotShapes(Peakshape,colorRed);
firstVisibleBar = Status( "firstvisiblebar" );
lastVisibleBar = Status( "lastvisiblebar" );
CCITurningdown = array > Ref( array, -1 ) AND array > Ref( array, 1 );
CCITurningup = array < Ref( array, -1 ) AND array < Ref( array, 1 );
CCIpeak = IIf( array > 0 AND CCITurningdown, shapeStar, shapeNone );
CCItrough = IIf( array < 0 AND CCITurningup, shapeSquare, shapeNone );
//PlotShapes(CCIpeak,colorRed);
//PlotShapes(CCItrough,colorRed);
Look4Shorts = EMA( C, 9 ) < EMA( C, 20 );
Look4Longs = EMA( C, 9 ) > EMA( C, 20 );
CCIBearishdivergence = Look4Shorts AND ( array > ValueWhen( CCItrough, array, 1
) ) AND ( C < ValueWhen( CCItrough, C, 1 ) );
CCIBullishishdivergence = Look4Longs AND ( array < ValueWhen( CCIpeak, array, 1
) ) AND ( C > ValueWhen( CCIpeak, C, 1 ) );
//PlotShapes(IIf(ValueWhen(CCItrough,C,1)>C AND
ValueWhen(CCItrough,CCItrough,1)<array,shapeCircle,shapeNone),colorOrange);
/*
CCI Divergence Occurs when the c < valuewhen(ccirough) and cci >last ccitrough.
*/
_TRACE( NumToStr( ValueWhen( CCItrough, array, 1 ) ) );
for ( b = firstVisibleBar;( ( b < lastVisibleBar ) AND ( b < BarCount ) ) ;b++ )
{
if ( CCIBearishdivergence[b] == 1 )
{
PlotText( "#", b, array[b], colorRed );
}
if ( CCIBullishishdivergence[b] == 1 )
{
PlotText( "$", b, array[b], colorGreen );
}
//_TRACE("valie of peak" + NumToStr(Peakvalue[b]));
}