//+------------------------------------------------------------------+
//|                                              Z-Winner Signal.mq4 |
//|                                                      Dien & Nabi |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Dien & Nabi"
#property link      ""

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Lime
#property indicator_color2 Red

double CrossUp[];
double CrossDown[];
extern string MA_MODE_Values = "-----SMA=0,EMA=1,SMMA=2,LWMA=3----";
extern int MA_MODE = 0;
extern int FasterMA = 3;
extern int SlowerMA = 9;
extern int CCI_Level = 50;
extern int CCI_Period = 14;
extern bool SoundON=false;
double alertTag;
int digits;
double point;
 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0, DRAW_ARROW, EMPTY,1);
   SetIndexArrow(0, 233);
   SetIndexBuffer(0, CrossUp);
   SetIndexStyle(1, DRAW_ARROW, EMPTY,1);
   SetIndexArrow(1, 234);
   SetIndexBuffer(1, CrossDown);

   if (Digits < 4)
   {
      point = 0.01;
      digits = 2;
   }
   else
   {
      point = 0.0001;
      digits = 4;
   }
   
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start() {
   int limit, i, counter;
   double FasterMAnow, SlowerMAnow, FasterMAprevious, SlowerMAprevious, FasterMAafter, SlowerMAafter, CCIPrevious, CCINow, CCIBefore;
   double Range, AvgRange;
   int counted_bars=IndicatorCounted();
//---- check for possible errors
   if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;

   limit=Bars-counted_bars;
   
   for(i = limit; i >= 0; i--) 
   {
       
      FasterMAnow = iMA(NULL, 0, FasterMA, 0, MA_MODE, PRICE_CLOSE, i);
      FasterMAprevious = iMA(NULL, 0, FasterMA, 0, MA_MODE, PRICE_CLOSE, i+1);
      FasterMAafter = iMA(NULL, 0, FasterMA, 0, MA_MODE, PRICE_CLOSE, i-1);

      SlowerMAnow = iMA(NULL, 0, SlowerMA, 0, MA_MODE, PRICE_CLOSE, i);
      SlowerMAprevious = iMA(NULL, 0, SlowerMA, 0, MA_MODE, PRICE_CLOSE, i+1);
      SlowerMAafter = iMA(NULL, 0, SlowerMA, 0, MA_MODE, PRICE_CLOSE, i-1);
      
      CCINow = iCCI(NULL, 0, CCI_Period, PRICE_TYPICAL, i);
      CCIPrevious = iCCI(NULL,0, CCI_Period, PRICE_TYPICAL, i+1);
      CCIBefore = iCCI(NULL,0, CCI_Period, PRICE_TYPICAL, i+2);
      
      if (
            (FasterMAnow > SlowerMAnow) && (FasterMAprevious < SlowerMAprevious) && (FasterMAafter > SlowerMAafter)
            && ((CCIPrevious <= CCI_Level || CCIBefore <= CCI_Level) && CCINow >= CCI_Level)
         ) {
         CrossUp[i] = Low[i] - point*15;
      }
      else if (
            (FasterMAnow < SlowerMAnow) && (FasterMAprevious > SlowerMAprevious) && (FasterMAafter < SlowerMAafter)
            && ((CCIPrevious >= CCI_Level || CCIBefore >= CCI_Level) && CCINow <= CCI_Level)
         ) {
          CrossDown[i] = High[i] + point*15;
      }
    
      if (SoundON==true && i==1 && CrossUp[i] > CrossDown[i] && alertTag!=Time[0]){
         Alert("Z-Winner going Down on ",Symbol()," ",Period());
        alertTag = Time[0];
      }
    
      if (SoundON==true && i==1 && CrossUp[i] < CrossDown[i] && alertTag!=Time[0]){
         Alert("Z-Winner going Up on ",Symbol()," ",Period());
         alertTag = Time[0];
      } 
  }
   return(0);
}

