//+------------------------------------------------------------------+
//|                                                    CCI_Cross.mq4 |
//|                                      Copyright © 2006, Eli Hayun |
//|                                          http://www.elihayun.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Eli Hayun"
#property link      "http://www.elihayun.com"

#property indicator_chart_window
#property indicator_buffers 6
#property indicator_color1 Blue
#property indicator_color2 Red
#property indicator_color3 Magenta //Red
#property indicator_color4 Magenta //Red
#property indicator_color5 Red
#property indicator_color6 Blue
//---- input parameters
extern int       FastCCI=34;//8
extern int       SlowCCI=55;//13
extern int       VerySlowCCI=170;//55
extern bool      ShowAlert = false;
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
double ExtMapBuffer5[];
double ExtMapBuffer6[];
string ShortName ="CCI_Cross";

bool   AlertBuy1  = false;
bool   AlertSell1 = false;
bool   AlertBuy2  = false;
bool   AlertSell2 = false;
bool   AlertBuy3  = false;
bool   AlertSell3 = false;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_ARROW,STYLE_SOLID, 2);//2
   SetIndexArrow(0,233);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexEmptyValue(0,0.0);
   SetIndexStyle(1,DRAW_ARROW,STYLE_SOLID, 2);//2
   SetIndexArrow(1,234);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexEmptyValue(1,0.0);
   SetIndexStyle(2,DRAW_ARROW,STYLE_SOLID, 2);
   SetIndexArrow(2,233);
   SetIndexBuffer(2,ExtMapBuffer3);
   SetIndexEmptyValue(2,0.0);
   SetIndexStyle(3,DRAW_NONE);
   SetIndexArrow(3,234);
   SetIndexBuffer(3,ExtMapBuffer4);
   SetIndexEmptyValue(3,0.0);
   SetIndexStyle(4,DRAW_ARROW);
   SetIndexArrow(4,241);
   SetIndexBuffer(4,ExtMapBuffer5);
   SetIndexEmptyValue(4,0.0);
   SetIndexStyle(5,DRAW_ARROW);
   SetIndexArrow(5,242);
   SetIndexBuffer(5,ExtMapBuffer6);
   SetIndexEmptyValue(5,0.0);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int shift;
   for(shift=Bars-1;shift>=0;shift--)
   {	
      ExtMapBuffer1[shift] = 0; ExtMapBuffer2[shift] = 0; ExtMapBuffer3[shift] = 0;
      ExtMapBuffer4[shift] = 0; ExtMapBuffer5[shift] = 0; ExtMapBuffer6[shift] = 0;

      double fCCI_0 = iCCI(NULL, 0, FastCCI, PRICE_TYPICAL, shift);
      double fCCI_1 = iCCI(NULL, 0, FastCCI, PRICE_TYPICAL, shift+1);
      double fCCI_2 = iCCI(NULL, 0, FastCCI, PRICE_TYPICAL, shift+2);
      
      if (fCCI_0 > 0 && fCCI_1<0 && fCCI_2 < 0)
        ExtMapBuffer1[shift] = Low[shift] - 3 * Point; 
   
      if (fCCI_0 < 0 && fCCI_1>0 && fCCI_2 > 0)
        ExtMapBuffer2[shift] = High[shift] + 3 * Point; 

      double sCCI_0 = iCCI(NULL, 0, SlowCCI, PRICE_TYPICAL, shift);
      double sCCI_1 = iCCI(NULL, 0, SlowCCI, PRICE_TYPICAL, shift+1);
      double sCCI_2 = iCCI(NULL, 0, SlowCCI, PRICE_TYPICAL, shift+2);
      
      if (sCCI_0 > 0 && sCCI_1<0 && sCCI_2 < 0)
        ExtMapBuffer3[shift] = Low[shift] - 4 * Point; 
   
      if (sCCI_0 < 0 && sCCI_1>0 && sCCI_2 > 0)
        ExtMapBuffer4[shift] = High[shift] + 4 * Point; 

      double vsCCI_0 = iCCI(NULL, 0, VerySlowCCI, PRICE_TYPICAL, shift);
      double vsCCI_1 = iCCI(NULL, 0, VerySlowCCI, PRICE_TYPICAL, shift+1);
      double vsCCI_2 = iCCI(NULL, 0, VerySlowCCI, PRICE_TYPICAL, shift+2);
      
      if (vsCCI_0 > 0 && vsCCI_1<0 && vsCCI_2 < 0)
        ExtMapBuffer5[shift] = Low[shift] - 5 * Point; 
   
      if (vsCCI_0 < 0 && vsCCI_1>0 && vsCCI_2 > 0)
        ExtMapBuffer6[shift] = High[shift] + 5 * Point; 
   }

   if(ShowAlert && NewBar()) showAlert();
   return(0);
  }
//+------------------------------------------------------------------+

void showAlert()

{

   if(NewBar()){
      AlertSell1 = false; 
      AlertBuy1  = false;
      AlertSell2 = false; 
      AlertBuy2 = false;
      AlertSell3 = false; 
      AlertBuy3 = false;
   }
   
   string sComment = ShortName+" "+Symbol()+"["+getPeriod(Period())+"] "+TimeToStr(CurTime(),TIME_MINUTES)+"شع"+DoubleToStr(Close[0],Digits);
   if (ExtMapBuffer1[0] != 0 && AlertBuy1==false){
      sComment = sComment + " CCI("+FastCCI+") UP";
      PlaySound("expert.wav");
      Alert(sComment);
      AlertBuy1  = true;
      AlertSell1 = false;
   }
   if (ExtMapBuffer2[0] != 0 && AlertSell1==false){
      sComment = sComment + " CCI("+FastCCI+") Down";
      PlaySound("expert.wav");
      Alert(sComment);
      AlertBuy1  = false;
      AlertSell1 = true;
   }
   if (ExtMapBuffer3[0] != 0 && AlertBuy2==false){
      sComment = sComment + " CCI("+SlowCCI+") UP";
      PlaySound("expert.wav");
      Alert(sComment);
      AlertBuy2  = true;
      AlertSell2 = false;
   }   
   if (ExtMapBuffer4[0] != 0 && AlertSell2==false){
      sComment = sComment + " CCI("+SlowCCI+") Down";
      PlaySound("expert.wav");
      Alert(sComment);
      AlertBuy2  = false;
      AlertSell2 = true;
   }  
   if (ExtMapBuffer5[0] != 0 && AlertBuy3==false){
      sComment = sComment + " CCI("+VerySlowCCI+") UP";
      PlaySound("expert.wav");
      Alert(sComment);
      AlertBuy3  = true;
      AlertSell3 = false;
   }   
   if (ExtMapBuffer6[0] != 0 && AlertSell3==false){
      sComment = sComment + " CCI("+VerySlowCCI+") Down";
      PlaySound("expert.wav");
      Alert(sComment);
      AlertBuy3  = false;
      AlertSell3 = true;
   }
   
}


bool NewBar()
{
   static datetime dt = 0;
   //if (dt == 0) dt = Time[0];
   if (dt != Time[0])
   {
      dt = Time[0];
      return(true);
   }
   return(false);
}


string getPeriod(int period){
   string s="";
   if     (period==1)     s="M1";
   else if(period==5)     s="M5";
   else if(period==15)    s="M15";
   else if(period==30)    s="M30";
   else if(period==60)    s="H1";
   else if(period==240)   s="H4";
   else if(period==1440)  s="D1";
   else if(period==10080) s="W1";
   else if(period==43200) s="MN";
   return(s);
}

