//+------------------------------------------------------------------+
//|                                                        NMAZZ.mq4 |
//+------------------------------------------------------------------+

#property indicator_chart_window
#property indicator_color1 Red
#property indicator_buffers 2
#property indicator_color2 Gold
#include <stdlib.mqh>
//---- indicator parameters
extern double depth = 20;
extern double deviation = 5;
extern double backstep = 3;
extern double nBars = 50;
extern double per = 21;
extern double ucci = 200;
extern double mwpr = -15;
extern double bwpr = -85;
//----
int shift = 0;
double zzold = 0;
double fzz = 0;
double zz = 0;
double ccione = 0;
double ccinul = 0;
double sig = 0;
double wprone = 0;
double wprnul = 0;
bool ft = true;
double pr = 4;
double null = 0.0001;
//---- indicator buffers
double Buffer1[];
double Buffer2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   IndicatorBuffers(2);
//--- indicator lines
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID);
   SetIndexBuffer(0,Buffer1);
   SetIndexStyle(1,DRAW_ARROW,STYLE_SOLID);
   SetIndexBuffer(1,Buffer2);
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
{
 if(ft)
  {
   if(Point>0.0002) {pr=2;null=0.01;} 
   ft=false;
  } 
 for(shift=nBars;shift>=0;shift--)
  {
   if(zz!=0 && zz!=zzold) zzold=zz;
   ccione=iCCI(NULL,0,per,PRICE_CLOSE,shift+2);ccinul=iCCI(NULL,0,per,PRICE_CLOSE,shift+1);
   wprone=iWPR(NULL,0,per,shift+2);wprnul=iWPR(NULL,0,per,shift+1);
   zz=iCustom(NULL,0,"ZigZagS",depth,deviation,backstep,nBars,0,shift);
   if(zz!=0) fzz=zz;
   if(zz!=0 && (((ccione<-ucci || ccinul<-ucci) && (wprone<bwpr || wprnul<bwpr)) || ((ccione>ucci || ccinul>ucci) &&
                 (wprone>mwpr || wprnul>mwpr)))) sig=1; else sig=null;
   if(sig==1) Buffer2[shift]=zz; else Buffer2[shift]=sig;
   if(zz==0) fzz=zzold;
   if(zz!=0)
    {
     zz=NormalizeDouble(zz,pr);
     ObjectSetText("ZZ_txt","ZZ="+zz,8,"Arial",White);
     if(zz>Close[0]) 
      {
       if (ObjectFind("ZZ_txt")!=-1) ObjectMove("ZZ_txt",0,Time[0],zz+5*Point);
        else
         {
          ObjectCreate("ZZ_txt",OBJ_TEXT,0,Time[0],zz+5*Point,0,0,0,0);
          ObjectSet("ZZ_txt",OBJPROP_COLOR,White);
         }
      }
     if(zz<Close[0])
      {
       if (ObjectFind("ZZ_txt")!=-1) ObjectMove("ZZ_txt",0,Time[0],zz-5*Point);
        else
         {
          ObjectCreate("ZZ_txt",OBJ_TEXT,0,Time[0],zz-5*Point,0,0,0,0);
          ObjectSet("ZZ_txt",OBJPROP_COLOR,White);
         }
      }
    } 
  fzz=NormalizeDouble(fzz,pr);
  Buffer1[shift]=fzz;
 } 
return(0);
}
//+------------------------------------------------------------------+