//+------------------------------------------------------------------+
//|                Support and Resistance 2                          |
//|                Copyright © 2004/5/6/7     Barry Stander          |
//|                http://www.4Africa.net/4meta/                     |
//+------------------------------------------------------------------+
#property copyright "Support and Resistance        Copyright © 2006 Barry Stander      fx1@4africa.net"
#property link      "http://www.4Africa.net/4meta/"


string version = ""; 
string likeit = " ";
   
extern int srPeriod = 7;
extern int MaxBars = 500;

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

//---- buffers
double v1[];
double v2[];
double val1;
double val2;
int i;

double upper_fr[300];
double lower_fr[300];
  
int init()
  {
Comment( version + "\n" + likeit );

  IndicatorBuffers(2);
 
   SetIndexArrow(0, 119);
   SetIndexStyle(0,DRAW_ARROW,STYLE_DOT,1,Red);
   SetIndexDrawBegin(0,i-1);
   SetIndexBuffer(0, v1);
   SetIndexLabel(0,"Resistance");
   
   SetIndexArrow(1, 119);
   SetIndexStyle(1,DRAW_ARROW,STYLE_DOT,1,Blue);
   
   SetIndexDrawBegin(1,i-1);
   SetIndexBuffer(1, v2);
   SetIndexLabel(1,"Support");
 
   return(0);
  }

int start()
  {
  Comment( version + "\n" + likeit );

  int counted = IndicatorCounted();
  if (counted < 0) return (-1);
  if (counted > 0) counted--;
  
    int limit = MathMin(Bars-counted, MaxBars);
  
  double dy = 0;
//  for (int i=1; i <= 20; i++) 
//  {
//    dy += 0.3*(High[i]-Low[i])/20;
//  }
  
  for (int i=0+srPeriod; i <= limit+srPeriod; i++) 
  {
    upper_fr[i] = 0;
    lower_fr[i] = 0;
  
    if (is_upper_fr(i, srPeriod)) upper_fr[i] = High[i];//+dy;
    if (is_lower_fr(i, srPeriod)) lower_fr[i] = Low[i];//-dy;
  }
 
  
  i=Bars;
while(i>=0)
 {
   
   val1 = upper_fr[i];
    if (val1 > 0) 
     v1[i]=High[i];
    else
     v1[i] = v1[i+1];
  
   val2 = lower_fr[i];
    if (val2 > 0) 
     v2[i]=Low[i];
    else
     v2[i] = v2[i+1];

     i--;
 }   

 return(0);
}
 
//+------------------------------------------------------------------+
//
//       i-FractalsEx from:   http://codebase.mql4.com/1302
//       #property copyright "© 2007 RickD"
//       #property link      "www.e2e-fx.net"
//


bool is_upper_fr(int bar, int period) 
{
  for (int i=1; i<=period; i++) 
  {
    if (bar+i >= Bars || bar-i < 0) return (false);

    if (High[bar] < High[bar+i]) return (false);
    if (High[bar] < High[bar-i]) return (false);
  }
  
  return (true);
}

bool is_lower_fr(int bar, int period) 
{
  for (int i=1; i<=period; i++) 
  {
    if (bar+i >= Bars || bar-i < 0) return (false);
    
    if (Low[bar] > Low[bar+i]) return (false);
    if (Low[bar] > Low[bar-i]) return (false);
  }  
  return (true);
}