//+------------------------------------------------------------------+
//|                                        Custom Moving Average.mq4 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/"

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Red
#property indicator_color2 Blue
//---- indicator parameters
extern string TimeFrame="H4";
extern int    ExtDepth=12;
extern int    ExtDeviation=5;
extern int    ExtBackstep= 3;
extern int    ArrowChar  = 174; 
extern bool   ShowZigZag = true;

//---- indicator buffers
double ExtMapBuffer[];
double ExtMapBuffer2[];
double dnLevel[];
double upLevel[];

//
//
//
//
//

int    timeFrame;
string indicatorFileName;
bool   calculateValue;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   IndicatorBuffers(4);
   SetIndexBuffer(0,dnLevel); SetIndexStyle(0,DRAW_ARROW); SetIndexArrow(0,ArrowChar); 
   SetIndexBuffer(1,upLevel); SetIndexStyle(1,DRAW_ARROW); SetIndexArrow(1,ArrowChar);
   SetIndexBuffer(2,ExtMapBuffer); if (ShowZigZag) SetIndexStyle(2,DRAW_SECTION); else SetIndexStyle(2,DRAW_NONE);    SetIndexEmptyValue(2,0.0);
   SetIndexBuffer(3,ExtMapBuffer2);

      indicatorFileName = WindowExpertName();
      calculateValue    = (TimeFrame=="calculateValue"); if (calculateValue) return(0);
      timeFrame         = stringToTimeFrame(TimeFrame);
            SetIndexShift(0,timeFrame/Period());
            SetIndexShift(1,timeFrame/Period());

//---- indicator short name
   IndicatorShortName(timeFrameToString(timeFrame)+" ZigZag levels("+ExtDepth+","+ExtDeviation+","+ExtBackstep+")");
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
{
   if (calculateValue || timeFrame==Period())
   {
   int    shift, back,lasthighpos,lastlowpos;
   double val,res;
   double curlow,curhigh,lasthigh,lastlow;

   for(shift=Bars-ExtDepth; shift>=0; shift--)
     {
      val=Low[Lowest(NULL,0,MODE_LOW,ExtDepth,shift)];
      if(val==lastlow) val=0.0;
      else 
        { 
         lastlow=val; 
         if((Low[shift]-val)>(ExtDeviation*Point)) val=0.0;
         else
           {
            for(back=1; back<=ExtBackstep; back++)
              {
               res=ExtMapBuffer[shift+back];
               if((res!=0)&&(res>val)) ExtMapBuffer[shift+back]=0.0; 
              }
           }
        } 
      ExtMapBuffer[shift]=val;
      //--- high
      val=High[Highest(NULL,0,MODE_HIGH,ExtDepth,shift)];
      if(val==lasthigh) val=0.0;
      else 
        {
         lasthigh=val;
         if((val-High[shift])>(ExtDeviation*Point)) val=0.0;
         else
           {
            for(back=1; back<=ExtBackstep; back++)
              {
               res=ExtMapBuffer2[shift+back];
               if((res!=0)&&(res<val)) ExtMapBuffer2[shift+back]=0.0; 
              } 
           }
        }
      ExtMapBuffer2[shift]=val;
     }

   // final cutting 
   lasthigh=-1; lasthighpos=-1;
   lastlow=-1;  lastlowpos=-1;

   for(shift=Bars-ExtDepth; shift>=0; shift--)
     {
      curlow=ExtMapBuffer[shift];
      curhigh=ExtMapBuffer2[shift];
      if((curlow==0)&&(curhigh==0)) continue;
      //---
      if(curhigh!=0)
        {
         if(lasthigh>0) 
           {
            if(lasthigh<curhigh) ExtMapBuffer2[lasthighpos]=0;
            else ExtMapBuffer2[shift]=0;
           }
         //---
         if(lasthigh<curhigh || lasthigh<0)
           {
            lasthigh=curhigh;
            lasthighpos=shift;
           }
         lastlow=-1;
        }
      //----
      if(curlow!=0)
        {
         if(lastlow>0)
           {
            if(lastlow>curlow) ExtMapBuffer[lastlowpos]=0;
            else ExtMapBuffer[shift]=0;
           }
         //---
         if((curlow<lastlow)||(lastlow<0))
           {
            lastlow=curlow;
            lastlowpos=shift;
           } 
         lasthigh=-1;
        }
     }
  
   for(shift=Bars-1; shift>=0; shift--)
   {
     if(shift>=Bars-ExtDepth) { ExtMapBuffer[shift]=0.0; continue; }
     dnLevel[shift] = dnLevel[shift+1];
     upLevel[shift] = upLevel[shift+1];
     res            = ExtMapBuffer2[shift];
     if(res!=0.0)  ExtMapBuffer[shift]=res;
         if (Low[shift] ==ExtMapBuffer[shift]) { dnLevel[shift] = ExtMapBuffer[shift]; upLevel[shift] = EMPTY_VALUE; }
         if (High[shift]==ExtMapBuffer[shift]) { upLevel[shift] = ExtMapBuffer[shift]; dnLevel[shift] = EMPTY_VALUE; }
   }
   return(0);
   }
   for(shift=Bars-1; shift>=0; shift--)
   {
      int y = iBarShift(NULL,timeFrame,Time[shift]);
      dnLevel[shift] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",ExtDepth,ExtDeviation,ExtBackstep,0,y);
      upLevel[shift] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",ExtDepth,ExtDeviation,ExtBackstep,1,y);
   }   
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

//
//
//
//
//

int stringToTimeFrame(string tfs)
{
   StringToUpper(tfs);
   for (int i=ArraySize(iTfTable)-1; i>=0; i--)
         if (tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(MathMax(iTfTable[i],Period()));
                                                      return(Period());
}
string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}