//+------------------------------------------------------------------+
//|                                              DoubleCCI_Alert.mq4 |
//|                                                           oromek |
//|                                                oromeks@gmail.com |
//+------------------------------------------------------------------+
#property copyright "oromek"
#property link      "oromeks@gmail.com"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Magenta
#property indicator_width1 2
#property indicator_color2 Magenta
#property indicator_width2 2

extern double  AlertLevel=0;
extern bool    AlertON=True;
extern bool    EmailON=true;

double CCI_1, CCI_2;
double UpBuffer[], DownBuffer[];
int currBars, counter, i;
double Range, AvgRange;
string CCIFilneName="Trend CCI";
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   SetIndexStyle(0, DRAW_ARROW, EMPTY);
   SetIndexArrow(0, 116);
   SetIndexBuffer(0, UpBuffer);
   SetIndexStyle(1, DRAW_ARROW, EMPTY);
   SetIndexArrow(1, 116);
   SetIndexBuffer(1, DownBuffer);
//---- indicators
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
   
   int limit=Bars-counted_bars;
   if(counted_bars>0) limit++;
   
   for(int i=1; i<limit; i++)
   {
      CCI_1=iCustom(NULL,0,CCIFilneName,6,i);
      CCI_2=iCustom(NULL,0,CCIFilneName,6,i+1);
      
      counter=i;
      Range=0;
      AvgRange=0;
      for (counter=i ;counter<=i+15;counter++)
      {
         AvgRange=AvgRange+MathAbs(High[counter]-Low[counter]);
      }
      Range=AvgRange/10;
      
      if (CCI_2<AlertLevel && CCI_1>AlertLevel) UpBuffer[i]=Low[i]-Range;
      if (CCI_2>AlertLevel && CCI_1<AlertLevel) DownBuffer[i]=High[i]+Range;
   }
   
   if (currBars!=Bars)
   {
      CCI_1=iCustom(NULL,0,CCIFilneName,6,1);
      CCI_2=iCustom(NULL,0,CCIFilneName,6,2);
      
      if (CCI_2<AlertLevel && CCI_1>AlertLevel)
      {
         if (AlertON) Alert(Symbol(),"TREND:CCI crossed ",AlertLevel," level UP");
         if (EmailON) SendMail(Symbol()+"TREND: CCI crossed UP",Symbol()+": CCI crossed "+AlertLevel+" level UP");
      }
      
      if (CCI_2>AlertLevel && CCI_1<AlertLevel)
      {
         if (AlertON) Alert(Symbol(),"TREND:CCI crossed ",AlertLevel," level DOWN");
         if (EmailON) SendMail(Symbol()+"TREND: CCI crossed DOWN",Symbol()+": CCI crossed "+AlertLevel+" level DOWN");
      }
      
      currBars=Bars;
   }
   
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+