//+------------------------------------------------------------------+
//|                                                  FractalMark.mq4 |
//|                                                    OnTheMark     |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Blue

extern    int Bars_On_Side = 1;

double FractalsUp[];
double FractalsDown[];


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_ARROW);
   SetIndexArrow(0,217);
   SetIndexBuffer(0,FractalsUp);
   SetIndexEmptyValue(0,0.0);
   SetIndexStyle (1,DRAW_ARROW);
   SetIndexArrow (1,218);
   SetIndexBuffer(1,FractalsDown);
   SetIndexEmptyValue(1,0.0);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int pos;
   int i;
   bool found;
   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--;
   pos = Bars - counted_bars - Bars_On_Side;
   
   
   while (pos > Bars_On_Side)
     {
     // assume ok
     found = true;
     for (i=Bars_On_Side; i>0; i--)
       {
       // look for problems
       if (High[pos+i] >= High[pos] || High[pos] <= High[pos-i]) found = false;
       }
     if (found) FractalsUp[pos] = High[pos];  
     
     // assume ok
     found = true;
     for (i=Bars_On_Side; i>0; i--)
       {
       // look for problems
       if (Low[pos+i] <= Low[pos] || Low[pos] >= Low[pos-i]) found = false;
       }
       
     if (found) FractalsDown[pos] = Low[pos];  
     pos--;
     }
   
   return(0);
  }
//+------------------------------------------------------------------+