//+------------------------------------------------------------------+
//|                                               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   = 1;
extern color   BoxColor    = Thistle;
extern bool    Check_Hi_Lo = true;
extern color   Arrow_Color = Yellow;

double
   Box_Space   = 2;
   
int
   D_Factor = 1,
   Box_ID = 0,
   Arrow_ID = 0;
   
bool 
   Hi_Flag = false,
   Lo_Flag = false;  
//+------------------------------------------------------------------+
//| 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()
{
int 
   Counted_bars=IndicatorCounted(),
   i=Bars-Counted_bars-2;
   if(Counted_bars == 1){
      Hi_Flag = false;
      Lo_Flag = false; 
   }
   while(i>=0){
      if(MathAbs(High[i+1] - High[i+2]) <= Tolerance){
         if(Close[i+1] < Low[i+2]){ 
            Box_ID++;
            Highlight_Bars(Box_ID,MathMax(High[i+1],High[i+2]), MathMax(High[i+1],High[i+2])+Box_Space, Time[i+1],Time[i+2]);
            if(Counted_bars > Bars - 3)Alert("Double Top on last 2 bars");
         }
      }
      if(MathAbs(Low[i+1] - Low[i+2]) <= Tolerance){
         if(Close[i+1] > High[i+2]){
            Box_ID++;
            Highlight_Bars(Box_ID,MathMin(Low[i+1],Low[i+2])-Box_Space, MathMin(Low[i+1],Low[i+2]), Time[i+1],Time[i+2]);
            if(Counted_bars > Bars - 3)Alert("Double Bottom on last 2 bars");
         }
      } 
      i--;
   }
   if (Check_Hi_Lo){
      if (!Hi_Flag){
         if(Bid > High[1]){
            Display_Marker(High[0] + Box_Space, SYMBOL_ARROWUP);
            Alert("Previous High has just been exceeded");
            Hi_Flag = true;
         }
      }
      if (!Lo_Flag){
         if(Ask < Low[1]){
            Display_Marker(Low[0] - Box_Space, SYMBOL_ARROWDOWN);
            Alert("Previous Low has just been exceeded");
            Lo_Flag = true;
         }
      }
   }
   return(0);
}
//+------------------------------------------------------------------+
void Highlight_Bars(int ID_Num, double Base_Price, double Top_Price,datetime T_1, datetime T_2)
{
   string BoxID = StringConcatenate("BoxID",ID_Num);
   ObjectCreate(BoxID, OBJ_RECTANGLE ,0,0,0,0);
   ObjectSet(BoxID, OBJPROP_TIME1 ,T_1);
   ObjectSet(BoxID, OBJPROP_TIME2 , T_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);
}
//+------------------------------------------------------------------+
void Display_Marker(double Marker_Price, int Arrow)
{
   string Marker_Name = StringConcatenate("Arrow",Arrow_ID);
   ObjectCreate(Marker_Name, OBJ_ARROW ,0,Time[1],Marker_Price);
   ObjectSet(Marker_Name, OBJPROP_ARROWCODE, Arrow);
   ObjectSet(Marker_Name, OBJPROP_COLOR,Arrow_Color);
   Arrow_ID++;
}

