//+------------------------------------------------------------------+
//|                                               Double_Trouble.mq4 |
//|                                                    Kenny Hubbard |
//|                                       http://www.compu-forex.com |
//+------------------------------------------------------------------+
#property copyright "Kenny Hubbard"
#property link      "http://www.compu-forex.com"

#property indicator_chart_window

extern double  Tolerance   = 3;
extern color   BoxColor    = Thistle;

double
   Box_Space   = 2;
   
int
   D_Factor = 1,
   Box_ID = 0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   if (Digits == 3 || Digits == 5) D_Factor = 10;
   Tolerance *= Point * D_Factor;
   Box_Space *= Point * D_Factor;
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   ObjectsDeleteAll();
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   if(New_Bar() == true){
      if(MathAbs(High[1] - High[2]) <= Tolerance){
         if(Close[1] < Low[2]){ 
            Box_ID++;
            Highlight_Bars(Box_ID,MathMax(High[1],High[2]), MathMax(High[1],High[2])+Box_Space);
            Alert("Double Top Found");
         }
      }
      if(MathAbs(Low[1] - Low[2]) <= Tolerance){
         if(Close[1] > High[2]){
            Box_ID++;
            Highlight_Bars(Box_ID,MathMin(Low[1],Low[2])-Box_Space, MathMin(Low[1],Low[2]));
            Alert("Double Bottom Found");
         }
      }
   }
   return(0);
}
//+------------------------------------------------------------------+
void Highlight_Bars(int ID_Num, double Base_Price, double Top_Price)
{
   string BoxID = StringConcatenate("BoxID",ID_Num);
   ObjectCreate(BoxID, OBJ_RECTANGLE ,0,0,0,0);
   ObjectSet(BoxID, OBJPROP_TIME1 , Time[1]);
   ObjectSet(BoxID, OBJPROP_TIME2 , Time[2]);
   ObjectSet(BoxID, OBJPROP_PRICE1, Base_Price);  
   ObjectSet(BoxID, OBJPROP_PRICE2, Top_Price);
   ObjectSet(BoxID, OBJPROP_STYLE, STYLE_SOLID);
   ObjectSet(BoxID, OBJPROP_COLOR, BoxColor);
   ObjectSet(BoxID, OBJPROP_BACK, True);
}
//+------------------------------------------------------------------+
bool New_Bar()
{
   static datetime New_Time=0;
   if(New_Time!=Time[0]){
      New_Time=Time[0];
      return(true);
   }
   return(false);
}