// Downloaded From https://www.WiseStockTrader.com
/////////////////////////////// 
// CCI Panel for Amibroker Dr. Bob
// Codded/Added by Dennis, Kris, Wring
// Last Update: 11/25/2007
/////////////////////////////// 
// Go to www.tradershaven.net to learn everything about this system.
// You must be a registered user to see the images and downloads.
/////////////////////////////// 
// Setup Axes and Grid section (right click on chart panel, click on Parameters):
// Scaling: Custom , Min=-250  Max=250
// Show Date Axis = Yes , Show Middle Lines = No
/////////////////////////////// 
// To activate the timer properly, make sure the following is set:
// click on Tools==>Preferences==>Intraday....
// make sure "Allign minute bars to market hours" is checked...
// make sure "Start time of interval" is checked...
// make sure "Override: Weekly/monthly bars use day of last trade" is checked.
/////////////////////////////// 
// Tic/PIP values: YM=1.0, ER2=0.10, NQ=0.25, EUR/USD=.0001, USD/JPY=0.01, Stocks=0.01
// Rangebar Settings :
// ER2 1.50 
// YM 25  
// ES 3 
// NQ 3.75 
// DAX 5  
// ZG 1.5  
/////////////////////////////// 
// Discalimer: For educational purposes only. Trade at your own risk.
///////////////////////////////

// Timer

TTMperiod = 6; 
Low_ma = EMA(L, TTMperiod); 
High_ma = EMA(H, TTMperiod); 
Low_third = (High_ma - Low_ma) / 3 + Low_ma; 
High_third = 2 * (High_ma - Low_ma) / 3 + Low_ma; 
tempnum = Now( 4 ) - TimeNum(); 
TimeRem = Interval() - ((int(tempnum[BarCount - 1] / 100) * 60) + (tempnum[BarCount - 1] - int(tempnum[BarCount - 1] / 100) * 100)); 
if (TimeRem[BarCount - 1] < 0) TimeRem = 0; 
MinuteVar = int(TimeRem / 60); 
SecondsVar = int(frac(TimeRem / 60) * 60); 
if (TimeRem[BarCount - 1] > 60) 
{ 
TitleTimeRem = EncodeColor(colorWhite) + MinuteVar + ":" + WriteIf(SecondsVar > 9, "", "0") + SecondsVar; 
} 
else if (TimeRem[BarCount - 1] > 20) 
{ 
TitleTimeRem =  EncodeColor(colorYellow) + MinuteVar + ":" + WriteIf(SecondsVar > 9, "", "0") + SecondsVar; 
} 
else 
{ 
TitleTimeRem = EncodeColor(colorRed) + MinuteVar + ":" + WriteIf(SecondsVar > 9, "", "0") + SecondsVar; 
}  

// Background colors

SetChartBkColor(ParamColor("Panel color ",colorPaleBlue)); 

// CCI Colors

zcolor=ParamColor("WCCI color",colorBlack);
z6color=ParamColor("TCCI color",colorBrown);
patterncolor=ParamColor("Pattern trace color",colorWhite);

// Tic/PIP value

TicMult= Param("Tic multiplier(ER2=10,YM=1,ES=4,FOREX=1)",1,0,1000000);
TicDiv= Param("Tic or PIP value(ER2=0.1,YM=1,FOREX=1)",1,0,1000000);

// Spread

spread= Param("Spread (included in stop)",0,0,1000000); 

// Stop value

stopval= Param("Stop above/below entry bar",2,0,1000000); 

// CCI periods

zperiod=Param("WCCI Period",14,0,100);
z = CCI(zperiod); 
z6period=Param("TCCI Period",6,0,100);
z6 = CCI(z6period); 
z50period=Param("Long Term CCI Period",50,0,1000);
z50 = CCI(z50period); 

// Rangebar interval

rbint= Param("Rangebar interval:(YM=25.0,AB=1.5,NQ=3.75,ES=3.0)",1.0,0.25,1000000);

// Rangebar counter

rbcounter= round(((rbint-(H-L))) * ticmult);
rbcounterpercent= round((rbcounter/(rbint * ticmult))*100);

// Timer/counter title

timercode= Param("Timer:(minutes=1,rangebar=2)",1,1,2);
timetitle= WriteIf(timercode==1,TitleTimeRem, EncodeColor(colorWhite) + "Countdown  " + rbcounter + "  (" + rbcounterpercent + "%)"); 

// Plot grids

PlotTheGrids = ParamToggle("Plot grids","No|Yes",0); 
if (PlotTheGrids ==1) 
{ 
PlotGrid(0); 
PlotGrid(-100); 
PlotGrid(100);
PlotGrid(-200); 
PlotGrid(200); 
} 

// EMA 34

EMA34 = EMA(C,34); 

// Color the bars for Woodies Trend Following 

Plusbars = BarsSince(z < 0); 
Minusbars = BarsSince(z > 0); 
TrendBarCount = 6; 
Color[0] = colorDefault; 
Trend[0] = 0; 
for( i = 1; i < BarCount; i++ ) 
{ 
if (Plusbars[i] >= TrendBarCount) 
{ 
Trend[i] = 1; 
} 
else if (Minusbars[i] >= TrendBarCount) 
{ 
Trend[i] = -1; 
} 
else 
{ 
Trend[i] = Trend[i - 1]; 
} 

if (Trend[i] == 1) 
{ 
if (Minusbars[i] == TrendBarCount - 1) 
{ 
Color[i] = colorYellow; 
} 
else if (z[i] < 0) 
{ 
Color[i] = colorGrey40; 
} 
else 
{ 
Color[i] = colorBlue; 
} 
} 
else if (Trend[i] == -1) 
{ 
if (Plusbars[i] == TrendBarCount - 1) 
{ 
Color[i] = colorYellow; 
} 
else if (z[i] >= 0) 
{ 
Color[i] = colorGrey40; 
} 
else 
{ 
Color[i] = colorRed; 
} 

} 
else 
{ 
Color[i] = colorDefault; 
} 
} 

// Plot the 100s 

Plot(100,"", colorLightGrey , styleLine | styleThick | styleNoLabel); 
Plot(-100,"", colorLightGrey , styleLine | styleThick | styleNoLabel); 

// Plotthe 200s

Plot(200,"", colorLightGrey, styleLine | styleThick | styleNoLabel); 
Plot(-200,"", colorLightGrey, styleLine | styleThick | styleNoLabel);

// zero line 25lsma 

Plot(0,"", colorLightGrey , styleLine | styleThick | styleNoLabel); 

// Price Panel

Lastpricetitlehi= WriteIf(H>Ref(H,-1),EncodeColor(colorBrightGreen) + Ref(H,-1) + "  " + H , EncodeColor(colorWhite)+ Ref(H,-1) + "  " + H);
Lastpricetitlelo= WriteIf(L<Ref(L,-1),EncodeColor(colorRed) + Ref(L,-1) + "  " + L , EncodeColor(colorWhite) + Ref(L,-1) + "  " + L);

// Pattern codes

z50limitlong= LLV(z50,BarsSince(z>0))>0;
z50limitshort= HHV(z50,BarsSince(z<0))<0;

// 5014 Long 

CCIhook_long5014= LLV(Ref(z,-1),BarsSince(Ref(z,-1)<0)); 
Long5014= z50limitlong AND ((Ref(z,-1)<-75 AND Z>-75 AND CCIhook_long5014<-75 AND CCIhook_long5014>=-100) OR (Ref(z,-1)<-100 AND Z>-100 AND CCIhook_long5014<-100)) AND Z50>0;

// 5014 Short 

CCIhook_short5014=HHV(Ref(z,-1),BarsSince(Ref(z,-1)>0)); 
Short5014= z50limitshort AND ((Ref(z,-1)>75 AND Z<75 AND CCIhook_short5014>75 AND CCIhook_short5014<=100) OR (Ref(z,-1)>100 AND Z<100 AND CCIhook_short5014>100)) AND Z50<0;

// 50140 Long 

CCIhook_long50140= LLV(Ref(z,-1),BarsSince(Ref(z,-1)<0)); 
Long50140= z50limitlong AND z50>0 AND z>0 AND CCIhook_long50140<-75;

// 50140 Short 

CCIhook_short50140=HHV(Ref(z,-1),BarsSince(Ref(z,-1)>0)); 
Short50140= z50limitshort AND z50<0 AND z<0 AND CCIhook_short50140>75;

// Slingshot Long

z6pivotlong= LLV(z6,BarsSince(Ref(z6,-1)>0))<=-100 AND z6>0 AND Ref(z6,-1)<0;
zpivotlong= LLV(z,BarsSince(Ref(z,-1)>-100)) AND LLV(z,BarsSince(z>-100))<50 AND z>Ref(z,-1) AND z>0;
slingshotlong= z6pivotlong AND zpivotlong AND trend==1 AND z50>0 AND z<120;

// Slingshot Short

z6pivotshort= HHV(z6,BarsSince(Ref(z6,-1)<0))>=100 AND z6<0 AND Ref(z6,-1)>0;
zpivotshort= HHV(z,BarsSince(Ref(z,-1)<100))<100 AND HHV(z,BarsSince(Ref(z,-1)<100))>-50 AND z<Ref(z,-1) AND z<0;
slingshotshort= z6pivotshort AND zpivotshort AND trend==-1 AND z50<0 AND z>-120;

// Signal Title 

PatCode1=ParamToggle("Plot 5014 (1)","No|Yes",1); 
PatCode2=ParamToggle("Plot 50140 (2)","No|Yes",1); 
PatCode3=ParamToggle("Plot Slingshot (3)","No|Yes",1); 
Signaltitle= 
WriteIf(PatCode1==1 AND Long5014 AND NOT Long50140,EncodeColor(colorRed) + "\n" + "*** ALERT -- 5014 ***" + "\n",
WriteIf(PatCode1==1 AND Short5014 AND NOT short50140,EncodeColor(colorRed) + "\n" +  "*** ALERT -- 5014 ***" + "\n", 
WriteIf(PatCode2==1 AND Long50140,EncodeColor(colorRed) + "\n" +  "*** ALERT -- 50140 ***" + "\n",  
WriteIf(PatCode2==1 AND Short50140,EncodeColor(colorRed) + "\n" +  "*** ALERT -- 50140 ***" + "\n",
WriteIf(PatCode3==1 AND slingshotlong,EncodeColor(colorRed) + "\n" +  "*** ALERT -- SLINGSHOT ***" + "\n",
WriteIf(PatCode3==1 AND slingshotshort,EncodeColor(colorRed) + "\n" +  "*** ALERT -- SLINGSHOT ***" + "\n",""))))));

// Pattern signal codes

PlotShapes(IIf(PatCode1==1 AND Long5014 AND NOT Long50140,shapeDigit1,shapeNone),colorBlue,0,0,-40); 
PlotShapes(IIf(PatCode1==1 AND Short5014 AND NOT short50140,shapeDigit1+ shapePositionAbove,shapeNone),colorRed,0,0,-40);
PlotShapes(IIf(PatCode2==1 AND Long50140,shapeDigit2,shapeNone),colorBlue,0,0,-25); 
PlotShapes(IIf(PatCode2==1 AND Short50140,shapeDigit2+ shapePositionAbove,shapeNone),colorRed,0,0,-25) ;
PlotShapes(IIf(PatCode3==1 AND slingshotlong,shapeDigit3,shapeNone),colorBlue,0,0,-10); 
PlotShapes(IIf(PatCode3==1 AND slingshotshort ,shapeDigit3+ shapePositionAbove,shapeNone),colorRed,0,0,-10) ;

// CCI Exit
HookExitLong= z<=Ref(z,-1);
HookExitShort= z>=Ref(z,-1);

// 100 Line Cross Exit
Cross100long= Ref(z,-1)>100 AND z<100;
Cross100short= Ref(z,-1)<-100 AND z>-100;

// Mplay Exit 

MplayExitLong= z<Ref(z,-1) AND Ref(z,-1)<Ref(z,-2) AND C<O; 
MplayExitShort= z>Ref(z,-1) AND Ref(z,-1)>Ref(z,-2) AND C>O; 

// Heikin-Ashi Exit

HaClose =EMA((O+H+L+C)/4,3); 
HaOpen = AMA( Ref( HaClose, -1 ), 0.5 ); 
HaHigh = Max( H, Max( HaClose, HaOpen ) ); 
HaLow = Min( L, Min( HaClose, HaOpen ) );
HExitConLong=  HaLow < HaOpen;
HExitConShort=  HaHigh > HaOpen;

// Exit code
ExitCode= Param("Exits:Hook=1,100x=2,Mplay=3,H-Ashi=4",1,1,4);
ExitCodeTitleLong=
IIf(ExitCode==1,HookExitLong,IIf(Exitcode==3,MplayExitLong,IIf(Exitcode==4,HExitConLong,IIf(Exitcode==2,Cross100long,0))));
ExitCodeTitleShort=IIf(ExitCode==1,HookExitShort,IIf(Exitcode==3,MplayExitShort,IIf(Exitcode==4,HExitConShort,IIf(Exitcode==2,Cross100short,0))));
AllPatterns =(Long50140 AND patcode2==1) OR (Long5014 AND patcode1==1) OR (slingshotlong AND patcode3==1)  OR (short50140 AND patcode2==1) OR (short5014 AND patcode1==1) OR (slingshotshort AND patcode3==1) ; 
BuyPattern = (Long50140 AND patcode2==1) OR (Long5014 AND patcode1==1) OR (slingshotlong AND patcode3==1);;
SellPattern= ExitCodeTitleLong;
ShortPattern = (short50140 AND patcode2==1) OR (short5014 AND patcode1==1) OR (slingshotshort AND patcode3==1);
CoverPattern = ExitCodeTitleShort;

//Plot Sell/Cover Arrows 

Sell1=ExRem(SellPattern,BuyPattern); 
Cover1=ExRem(CoverPattern,ShortPattern); 
SellCode=ParamToggle("Plot sell/cover arrows ","No|Yes",0); 
PlotShapes(IIf(Sell1 AND SellCode==1,shapeHollowDownArrow,shapeNone),colorBlue,0,200,10); 
PlotShapes(IIf(Cover1 AND SellCode==1,shapeHollowDownArrow,shapeNone),colorRed,0,200,10); 

// Calculate pattern hilites

// Long

AsellL= BuyPattern;
BsellL= Sell1;
bs_AsellL= BarsSince(AsellL);
bs_BsellL= BarsSince(BsellL);
bars_sellL= IIf(bs_ASellL<= bs_BsellL,bs_AsellL,0);

//Short
AsellS= ShortPattern;
BsellS= Cover1;
bs_AsellS= BarsSince(AsellS);
bs_BsellS= BarsSince(BsellS);
bars_sellS= IIf(bs_ASellS<= bs_BsellS,bs_AsellS,0);
hilitecode=ParamToggle("Hi-lite patterns","No|Yes",0);

// CCI Line 

CCIcolor= IIf(((bars_sellL OR bars_sellS) OR AllPAtterns) AND hilitecode==1,patterncolor,zcolor);
Plot(round(z),"WCCI", CCIcolor , styleLine | styleThick);

// Turbo CCI 

Plot(round(z6),"TCCI", z6color, styleLine); 

// CCI Histogram 

Plot(z,"",Color,styleHistogram | styleThick | styleNoLabel); 

// BackTest Long

LastPatLong= BuyPattern;
LastPatBarLong= BarsSince(LastPatLong);
DDL=IIf((((LLV(L,LastPatBarLong)-Ref(C,-LastPatBarLong))))/ticdiv>=0,0,(((LLV(L,LastPatBarLong)-Ref(C,-LastPatBarLong))))/ticdiv);
BTExitLong= (((C-Ref(C,-LastPatBarLong))))/ticdiv; 
PeakLong=IIf((((HHV(H,LastPatBarLong)-Ref(C,-LastPatBarLong))))/ticdiv<=0,0,(((HHV(H,LastPatBarLong)-Ref(C,-LastPatBarLong))))/ticdiv);
StopLong1= Ref(C,-LastPatBarLong) - (Ref(L,-LastPatBarLong) - (stopval*ticdiv) -  (spread*ticdiv));
stoplong= (((StopLong1)))/ticdiv;
BackTestLongTitle= WriteIf(Sell1,EncodeColor(colorBlue) + "\n" +
"Stop:   " + round(stoplong) + "\n" +
"DD:     " + round(DDL) + "\n" + 
"Exit:      " + round(BTExitLong)+ "\n" +
"Peak:   " +  round(PeakLong) +"\n",EncodeColor(colorBlue) + "\n" +
"Stop:   " + round(stoplong) + "\n" +
"DD:     " + round(DDL) + "\n" + 
"Exit:      " + round(BTExitLong)+ "\n" +
"Peak:   " +  round(PeakLong) +"\n"); 

// BackTest Short

LastPatShort= ShortPattern;
LastPatBarShort= BarsSince(LastPatShort);
DDS= IIf((((Ref(C,-LastPatBarShort)-HHV(H,LastPatBarShort))))/ticdiv>=0,0,(((Ref(C,-LastPatBarShort)-HHV(H,LastPatBarShort))))/ticdiv);
BTExitShort= (((Ref(C,-LastPatBarShort)-C)))/ticdiv; 
PeakShort= IIf((((Ref(C,-LastPatBarShort)-LLV(L,LastPatBarShort))))/ticdiv<=0,0,(((Ref(C,-LastPatBarShort)-LLV(L,LastPatBarShort))))/ticdiv);
StopShort1= (Ref(H,-LastPatBarShort) + (Stopval*ticdiv) + (spread*ticdiv)) - Ref(C,-LastPatBarShort);
StopShort= (((StopShort1)))/ticdiv;
BackTestShortTitle= WriteIf(Cover1,EncodeColor(colorRed) + "\n" +
"Stop:   " + round(StopShort) + "\n" +
"DD:     " + round(DDS) + "\n" + 
"Exit:      " + round(BTExitShort)+ "\n" +
"Peak:   " +  round(PeakShort) +"\n",EncodeColor(colorRed)+ "\n" + 
"Stop:   " + round(StopShort) + "\n" +
"DD:     " + round(DDS) + "\n" + 
"Exit:      " + round(BTExitShort)+ "\n" + 
"Peak:   " +  round(PeakShort) +"\n");

// Backtest title

BackTestCode= ParamToggle("Display Stop,DD,Peak,Exit stats","No|Yes",0); 
BackTestTitle= WriteIf((bars_sellL>0 OR Sell1) AND BackTestCode==1,BackTestLongTitle ,WriteIf((bars_sellS>0 OR Cover1 )AND BackTestCode==1,BackTestShortTitle ,""));

// Stop in

Longbar= L+rbint;
Shortbar= H-rbint;

// Stop in title

stopincode= ParamToggle("Display stop-in for range bar mode","No|Yes",0);
sstoptitle=WriteIf(stopincode==1, EncodeColor(colorBlue) + "\n" + "Stop In Long:  " + Longbar + EncodeColor(colorRed) + "\n" + "Stop In Short:  " + Shortbar ,"");

// Title

Title = "\n" + "" + EncodeColor(colorWhite) + "14 CCI" + "\n" + EncodeColor(colorWhite) + Date() + "\n" + "\n" +
timetitle + "\n" + "\n" + Lastpricetitlehi + "\n" + EncodeColor(colorWhite) +  " " + Close + "\n" + 
Lastpricetitlelo + "\n" +  BackTestTitle + Signaltitle + sstoptitle;