//+------------------------------------------------------------------+
//|                                             AdvancedFractals.mq4 |
//|                               Copyright © 2012, Gehtsoft USA LLC |
//|                                            http://fxcodebase.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2012, Gehtsoft USA LLC"
#property link      "http://fxcodebase.com"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Blue

extern int Frames=2;

double ExtUpFractalsBuffer[];
double ExtDownFractalsBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicator buffers mapping  
    SetIndexBuffer(0,ExtUpFractalsBuffer);
    SetIndexBuffer(1,ExtDownFractalsBuffer);   
//---- drawing settings
    SetIndexStyle(0,DRAW_ARROW,0,4);
    SetIndexArrow(0,119);
    SetIndexStyle(1,DRAW_ARROW,0,4);
    SetIndexArrow(1,119);
//----
    SetIndexEmptyValue(0,0.0);
    SetIndexEmptyValue(1,0.0);
//---- name for DataWindow
    SetIndexLabel(0,"Fractal Up");
    SetIndexLabel(1,"Fractal Down");
//---- initialization done   
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {

   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    i,nCountedBars;
   bool   bFound;
   double dCurrent;
   nCountedBars=IndicatorCounted();
//---- last counted bar will be recounted    
   if(nCountedBars<=2)
      i=Bars-nCountedBars-3;
   if(nCountedBars>2)
     {
      nCountedBars--;
      i=Bars-nCountedBars-1;
     }

   while(i>=Frames+1)
     {
      bool FrUp=true;
      bool FrDn=true;
      int ii;
      for (ii=1;ii<=Frames;ii++)
      {
       if (High[i+ii]>=High[i] || High[i-ii]>=High[i]) FrUp=false;
       if (Low[i+ii]<=Low[i] || Low[i-ii]<=Low[i]) FrDn=false;
      }
     
      //----Fractals up
      if (FrUp)
        {
         ExtUpFractalsBuffer[i]=High[i];
        }
      //----Fractals down
      if (FrDn)
        {
         ExtDownFractalsBuffer[i]=Low[i];
        }
      i--;
     }

   return(0);
  }

