#property indicator_chart_window
#property indicator_buffers 1
#property description "Automatically render boxes for liquidity gaps "
#property indicator_color1 Black

extern double Gap_Size_Minimum = 100;
extern int ExtendBars = 1000;
extern int    History = 1000;
extern color Gap_Up = Aqua;
extern color Gap_Down = Tomato;
input ENUM_LINE_STYLE Rectangle_Style = STYLE_SOLID;

double Pip;
int init()
  {
    Pip = Point;
   if(Digits==3 || Digits==5) Pip = 10*Point;
   return(0);
  }

int deinit()
  {
    string ObjName;
    for(int i = ObjectsTotal()-1; i>=0; i--)
    {
      ObjName = ObjectName(i);
      if(StringFind(ObjName,"liquidity_gaps",0)>=0)
        ObjectDelete(ObjName);
    }
    return(0);
  }

int start()
  {
   int i, limit, counted_bars=IndicatorCounted();
   limit = MathMin(History,Bars-counted_bars-1);
   string ObjName;
   for(i=limit; i>=0; i--)
   {
     if(i>Bars-2) continue;
     if(MathAbs(Open[i]-Close[i+1]) > Gap_Size_Minimum*Pip)
     {
       ObjName = "liquidity_gaps_Up_"+Time[i];
       color ObjColor;
       if(Open[i] > Close[i+1]) ObjColor = Gap_Up;
       else ObjColor = Gap_Down;
       if(ObjectFind(ObjName)<0)
       {  
         ObjectCreate(ObjName,OBJ_RECTANGLE,0,Time[i+1],Close[i+1],Time[i],Open[i]);
         ObjectSet(ObjName,OBJPROP_BACK,0);         
         ObjectSet(ObjName,OBJPROP_COLOR,ObjColor);                 
         ObjectSet(ObjName,OBJPROP_STYLE,Rectangle_Style);
       }
     }
     
     //datetime thistime = Time[i];     
     int ib = i+ExtendBars;
     while(ib>=0)
     
     {
       ObjName = "liquidity_gaps_Up_"+Time[ib];
       if(ObjectFind(ObjName)>=0)
       {
         double pr2 = ObjectGet(ObjName,OBJPROP_PRICE2);
         double pr1 = ObjectGet(ObjName,OBJPROP_PRICE2);
         ObjectSet(ObjName,OBJPROP_TIME2,Time[i]);
         ObjectSet(ObjName,OBJPROP_PRICE2,pr2);
       }
       ib--;
     }
   }
   return(0);
  }