//+------------------------------------------------------------------+
//|                                                    CCI_Cross.mq4 |
//|                                      Copyright © 2006, Eli Hayun |
//|                                          http://www.elihayun.com |
//|                                       Built for ValeoFX          |
//+------------------------------------------------------------------+
//mod2008fxtsd    ki
#property copyright "Copyright © 2006, Eli Hayun"
#property link      "http://www.elihayun.com"

#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color1 BlueViolet//28-CCi
#property indicator_color2 BlueViolet
#property indicator_color3 Red//15-CCi
#property indicator_color4 Red
#property indicator_color5 Cyan//5-CCi
#property indicator_color6 Cyan
#property indicator_color7 Yellow
#property indicator_color8 Goldenrod



#property indicator_width1 4
#property indicator_width2 4
#property indicator_width3 2
#property indicator_width4 2
#property indicator_width5 1
#property indicator_width6 1
#property indicator_width7 0
#property indicator_width8 1

//---- input parameters

extern int       FastCCIperiod=5;
extern int       FastCCIprice=5;
extern bool FastCCI_on  =true;
extern int       SlowCCIperiod=15;
extern int       SlowCCIprice=5;
extern bool SlowCCI_on  =true;
extern int       VerySlowCCIperiod=28;
extern int       VerySlowCCIprice=5;
extern bool VerySlowCCI_on =true;

extern bool    AlertsOn  =true;
extern string  AlertSoundFile="wait.wav";

extern double  SignalGap =0.8;
extern int     TimeFrame =0;


extern string   note_Price = "0C 1O 2H 3L 4Md 5Tp 6WghC: Md(HL/2)4,Tp(HLC/3)5,Wgh(HLCC/4)6";
extern string TimeFrames = "M1;5,15,30,60H1;240H4;1440D1;10080W1;43200MN;0 currentTF";

//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
double ExtMapBuffer5[];
double ExtMapBuffer6[];
double ExtMapBuffer7[];
double ExtMapBuffer8[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   TimeFrame= MathMax(TimeFrame,Period());

   if (FastCCI_on)
   {
   SetIndexStyle(5,DRAW_ARROW);
   SetIndexArrow(5,170);//217 233
   SetIndexBuffer(5,ExtMapBuffer1);
   SetIndexEmptyValue(5,0.0);

   SetIndexStyle(4,DRAW_ARROW);  
   SetIndexArrow(4,170);//218 234
   SetIndexBuffer(4,ExtMapBuffer2);
   SetIndexEmptyValue(4,0.0);

   SetIndexLabel(5,"CCI ("+FastCCIperiod+") M"+TimeFrame+" cross0 UP");
   SetIndexLabel(4,"CCI ("+FastCCIperiod+") M"+TimeFrame+" cross0 DN");
   }
   

   if (SlowCCI_on)
   {   
   SetIndexStyle(3,DRAW_ARROW);
   SetIndexArrow(3,233);
   SetIndexBuffer(3,ExtMapBuffer3);
   SetIndexEmptyValue(3,0.0);

   SetIndexStyle(2,DRAW_ARROW);
   SetIndexArrow(2,234);
   SetIndexBuffer(2,ExtMapBuffer4);
   SetIndexEmptyValue(2,0.0);

   SetIndexLabel(3,"CCI ("+SlowCCIperiod+") M"+TimeFrame+" cross0 UP");
   SetIndexLabel(2,"CCI ("+SlowCCIperiod+") M"+TimeFrame+" cross0 DN");
   }
   

   if (VerySlowCCI_on)
   {      
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexArrow(1,241);
   SetIndexShift(1,-TimeFrame/Period());
   SetIndexBuffer(1,ExtMapBuffer5);
   SetIndexEmptyValue(1,0.0);

   SetIndexStyle(0,DRAW_ARROW);
   SetIndexArrow(0,242);
   SetIndexShift(0,-TimeFrame/Period());
   SetIndexBuffer(0,ExtMapBuffer6);
   SetIndexEmptyValue(0,0.0);

   SetIndexLabel(1,"CCI ("+VerySlowCCIperiod+") M"+TimeFrame+" cross0 UP");
   SetIndexLabel(0,"CCI ("+VerySlowCCIperiod+") M"+TimeFrame+" cross0 DN");
   }


   SetIndexStyle(6,DRAW_ARROW);
   SetIndexArrow(6,226);//167158159178115
   SetIndexBuffer(6,ExtMapBuffer7);
   SetIndexEmptyValue(6,0.0);
   SetIndexLabel(6,"CCI("+FastCCIperiod+")0cross WarningSignal");
//
   SetIndexStyle(7,DRAW_ARROW);
   SetIndexArrow(7,234);//167159178170167
   SetIndexBuffer(7,ExtMapBuffer8);
   SetIndexEmptyValue(7,0.0);
   SetIndexLabel(7,"CCI("+SlowCCIperiod+")0cross WarningSignal");



   IndicatorShortName("CCI x3 Cross0 ["+TimeFrame+"]");  


   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   
   int    shift,y=0,limit,counted_bars=IndicatorCounted();
   
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;

   datetime TimeArray[];
   ArrayCopySeries(TimeArray,MODE_TIME,Symbol(),TimeFrame); 

   limit = Bars-counted_bars;
   limit = MathMax (limit,TimeFrame/Period());
   
   for(shift=0,y=0;shift<limit;shift++)
   {
   if (Time[shift]<TimeArray[y]) y++; 

 double high  = iHigh(NULL, TimeFrame, y);
 double high1 = iHigh(NULL, TimeFrame, y+1);

 double low   = iLow (NULL, TimeFrame, y);
 double low1  = iLow (NULL, TimeFrame, y+1);

 double signalgap = MathCeil(iATR(NULL,TimeFrame,10,y)/Point)*SignalGap;   
 
 
 
   if (FastCCI_on)
      {
      
      ExtMapBuffer1[shift] = 0.0; ExtMapBuffer2[shift] = 0.0; ExtMapBuffer7[shift] = 0.0;

      double fCCI_0 = iCCI(NULL, TimeFrame, FastCCIperiod, FastCCIprice, y);
      double fCCI_1 = iCCI(NULL, TimeFrame, FastCCIperiod, FastCCIprice, y+1);
 //   double fCCI_2 = iCCI(NULL, 0, FastCCI, PRICE_TYPICAL, shift+2);
     
      if (fCCI_0 > -100 && fCCI_1<-100 ){
         ExtMapBuffer1[shift] = low  - signalgap * Point; 
         ExtMapBuffer7[0]=ExtMapBuffer1[0];    SetIndexArrow(6,225);ExtMapBuffer7[1]=0.0;}

      if (fCCI_0 < 100 && fCCI_1>100 ){
         ExtMapBuffer2[shift] = high + signalgap *  Point;
         ExtMapBuffer7[0]=ExtMapBuffer2[0];   SetIndexArrow(6,226); }

      }

   if (SlowCCI_on)
      {
      ExtMapBuffer3[shift] = 0.0; ExtMapBuffer4[shift] = 0.0;  ExtMapBuffer8[shift] = 0.0;
         
      double sCCI_0 = iCCI(NULL, TimeFrame, SlowCCIperiod, SlowCCIprice, y);
      double sCCI_1 = iCCI(NULL, TimeFrame, SlowCCIperiod, SlowCCIprice, y+1);
//    double sCCI_2 = iCCI(NULL, TimeFrame, SlowCCIperiod, SlowCCIprice, y+2);
      
      if (sCCI_0>-100 && sCCI_1 < -100 ){
         ExtMapBuffer3[shift] = low  - 1.2 *signalgap * Point;  
         ExtMapBuffer8[0]= ExtMapBuffer3[0];SetIndexArrow(7,233);}
      
      if (sCCI_0<100 && sCCI_1 > 100 ){
         ExtMapBuffer4[shift]= high + 1.2 *signalgap * Point; 
         ExtMapBuffer8[0]=  ExtMapBuffer4[0];   SetIndexArrow(7,234);}
     
      }
      
   if (VerySlowCCI_on)
      {         
      ExtMapBuffer5[shift] = 0.0; ExtMapBuffer6[shift] = 0.0;

//    double vsCCI_0 = iCCI(NULL,TimeFrame, VerySlowCCIperiod, PRICE_OPEN, y);
      double vsCCI_1 = iCCI(NULL,TimeFrame, VerySlowCCIperiod, VerySlowCCIprice, y+1);
      double vsCCI_2 = iCCI(NULL,TimeFrame, VerySlowCCIperiod, VerySlowCCIprice, y+2);
      
      if (vsCCI_1>-100 && vsCCI_2<-100)
      ExtMapBuffer5[shift] = low1 - 1.5*signalgap * Point; 

      if (vsCCI_1<100&& vsCCI_2>100)
      ExtMapBuffer6[shift] = high1 + 1.5*signalgap * Point; 
      }
  
  
   }

   string sComment = "";
   
 if(AlertsOn) 
   {
   if (FastCCI_on)
      {
   if (ExtMapBuffer1[0] != 0)
      sComment = sComment + " CCI ("+FastCCIperiod+") M"+TimeFrame+" UP ";
      
   if (ExtMapBuffer2[0] != 0)
      sComment = sComment + " CCI ("+FastCCIperiod+") M"+TimeFrame+" Down ";
      }

   if (SlowCCI_on)
      {
   if (ExtMapBuffer3[0] != 0)
      sComment = sComment + " CCI ("+SlowCCIperiod+") M"+TimeFrame+" UP ";
      
   if (ExtMapBuffer4[0] != 0)
      sComment = sComment + " CCI ("+SlowCCIperiod+") M"+TimeFrame+" Down ";
      }
      
   if (VerySlowCCI_on) 
      {     
   if (ExtMapBuffer5[0] != 0)
      sComment = sComment + " CCI3 ("+VerySlowCCIperiod+") M"+TimeFrame+" UP ";
      
   if (ExtMapBuffer6[0] != 0)
      sComment = sComment + " CCI3 ("+VerySlowCCIperiod+") M"+TimeFrame+"] Down ";
      }
   }
 

   if ((sComment != "") && (!IsTesting()))
   {
      if (NewBar())
      {
         PlaySound(AlertSoundFile);//"expert.wav"wait.wav;
         Print(Symbol(), " ", sComment);
      }
      Comment(TimeToStr(CurTime()), " ", sComment);
   }
   if ((sComment == "") && (!IsTesting()))
   {
      Comment("");
   }

   return(0);
}
//+------------------------------------------------------------------+

bool NewBar()
{
   static datetime dt = 0;
   if (dt != Time[0])
   {
      dt = Time[0];
      return(true);
   }
   return(false);
}