
#property copyright "© 2014 Sconcha"
#property link      "2007 RickD"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Yellow
#property indicator_width1  1
#property indicator_width2  1

extern int FrPeriod = 4;
extern int MaxBars = 200;
double treshold = 0.0002;

double upper_fr[];
double lower_fr[];

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

void init() {
  SetIndexBuffer(0, upper_fr);
  SetIndexBuffer(1, lower_fr);
  
  SetIndexEmptyValue(0, 0);
  SetIndexEmptyValue(1, 0);
  
  SetIndexStyle(0, DRAW_ARROW);
  SetIndexArrow(0, 234);

  SetIndexStyle(1, DRAW_ARROW);
  SetIndexArrow(1, 233);  
  
   ObjectCreate("DBL",OBJ_LABEL,0,0,0,0,0);
   ObjectSet("DBL",OBJPROP_XDISTANCE,0);
   ObjectSet("DBL",OBJPROP_YDISTANCE,52);
   ObjectSetText("DBL","Fractal & DBLH Detector v1.0",9,"Tahoma",Gold);
   
   if(Symbol() == "EURJPY" || Symbol() == "GBPJPY" || Symbol() == "USDJPY"){
     
      treshold = treshold*100;   
         
   }
   
   if(Symbol() == "XAUUSD" || Symbol() == "GOLD"){
   
    treshold = 0.05;  
    
   
   }
   
   if(Symbol() == "XAGUSD" || Symbol() == "SILVER"){
   
    treshold = 0.002;  
   
   }

   if(Symbol() == "CORNUSD"){
   
    treshold = 0.002;  
   
   }
   
   if(Symbol() == "#CZ4" || Symbol() == "#WZ4"){
   
    treshold = 0.2;  
   
   }
   
   if(Symbol() == "WHEATUSD"){
   
    
   
    treshold = 0.002;  
   
   }

   if(Symbol() == "SUGARUSD"){
   
    
   
    treshold = 0.0001;  
   
   }
   
   if(Symbol() == "WTICOUSD"){
   
    
   
    treshold = 0.02;  
   
   }
      
         
}

void start() 
{
  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 (i=0+FrPeriod; i <= limit+FrPeriod; i++) 
  {
  
    upper_fr[i] = 0;
    lower_fr[i] = 0;
  
    if(is_upper_fr(i, FrPeriod)){
      upper_fr[i] = High[i]+dy;
    
    }
  
    
    if(is_lower_fr(i, FrPeriod)){ 
      lower_fr[i] = Low[i]-dy;
    }
  
    if(isDBH(i))
      upper_fr[i] = High[i]+dy;
      
    if(isDBL(i))
      lower_fr[i] = Low[i]-dy;
  
    
  }
}

   bool isDBH(int bar){

         if(
          // barra actual menos a barra anterior
            ((High[bar] - High[bar+1] < treshold) && (High[bar] - High[bar+1] > treshold*(-1)))
            &&  Close[bar] < Close[bar+1]
         ){
               return true;
         }
         return false;

   }

   bool isDBL(int bar){

         if(
          // barra actual menos a barra anterior
            ((Low[bar] - Low[bar+1] < treshold) && (Low[bar] - Low[bar+1] > treshold*(-1)))
            &&  Close[bar] > Close[bar+1]
         ){
         
            return true;
         }
         return false;


   }

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);
}



