//+------------------------------------------------------------------+
//|                                         Gilben Cleancut Cube.mq4 |
//|                                                           Gilben |
//|                                                 Metatrader.co.il |
//+------------------------------------------------------------------+
#property copyright "Gilben"
#property link      "http://www.acegazette.com/en/community/jomsocial/gilben/profile.html"



#property indicator_chart_window

extern string  exclude_string       = "#";
extern int     exclude_LTF_from     = 240;
extern color   font_color           = Silver;
extern bool    show_discription     = true;
extern bool    chenge_zone_colors   = true;
extern color   fresh_zone_color     = DimGray;
extern color   touched_zone_color   = C'35,35,35';
extern color   swapped_zone_color   = C'10,0,34';
extern bool    show_copyrights      = true;

string prefix = "cccube_";
datetime now = 0;
int total_objects;
int init()
{
   total_objects = ObjectsTotal();
   exclude_LTF_from--;
   redraw_rectangles();
   if(show_copyrights)  write("cw1", "Cleancut Cube v1.03", 8, LightSeaGreen, 10, 10);

   
   return(0);
}

int start()
{
   int    counted_bars=IndicatorCounted();
   string dbg = ObjectName(0);
   //Comment(ObjectsTotal() + "  "+ObjectDescription(dbg)+"   "+dbg);
   
   
   if(now != Time[1])
   {
      now = Time[1];
      redraw_rectangles();
   }

   return(0);
  }
  
void redraw_rectangles()
{
   for(int obj=0 ; obj < ObjectsTotal() ; obj++)
    {
          string obj_name=ObjectName(obj);
          if(ObjectType(obj_name) ==  OBJ_RECTANGLE && StringFind(ObjectName(obj),exclude_string,0) == -1)
          {
             
             double     top_line          = MathMax(ObjectGet(obj_name, OBJPROP_PRICE1), ObjectGet(obj_name, OBJPROP_PRICE2));
             double     low_line          = MathMin(ObjectGet(obj_name, OBJPROP_PRICE1), ObjectGet(obj_name, OBJPROP_PRICE2));
             datetime   rect_origin_time  = MathMin(ObjectGet(obj_name, OBJPROP_TIME1) , ObjectGet(obj_name, OBJPROP_TIME2));
             datetime   rect_ending_time  = MathMax(ObjectGet(obj_name, OBJPROP_TIME1) , ObjectGet(obj_name, OBJPROP_TIME2));
             int        origin_bar_shift  = iBarShift(Symbol(), Period(),rect_origin_time,false);
             int        ending_bar_shift  = MathMax(0,iBarShift(Symbol(), Period(),rect_ending_time,false));
             int        touch_counts = -1;
             bool       freeze_count = false;
             bool       is_swap = false;
             
             bool       is_price_above;
             if(High[ending_bar_shift] > top_line) is_price_above = true;
             if(Low[ending_bar_shift] < low_line)  is_price_above = false;
                   
             for(int rect_bar = ending_bar_shift ; rect_bar < origin_bar_shift ; rect_bar++)
             {
                  if(!is_swap && !is_price_above && High[rect_bar] > top_line) is_swap = true;
                  if(!is_swap && is_price_above  && Low[rect_bar] < low_line) is_swap = true;
                  
                  if( (High[rect_bar] > top_line && Low[rect_bar] > top_line) ||  (High[rect_bar] < low_line && Low[rect_bar] < low_line) )
                     freeze_count = false;
                  else
                  {
                     touch_counts++;
                     freeze_count = true;
                     //vline("L-"+touch_counts, touch_counts, iTime(Symbol(),Period(),rect_bar), White); 
                  }    
  
                  while(freeze_count && rect_bar < origin_bar_shift)
                  {
                     if( (High[rect_bar] > top_line && Low[rect_bar] > top_line) ||  (High[rect_bar] < low_line && Low[rect_bar] < low_line) )
                     {
                        freeze_count = false;
                     }   
                     else   
                        rect_bar++;
                  }
       
             }
             double price_for_tx = (top_line + low_line)/2;
             string touch_txt;
             color  cube_color = ObjectGet(obj_name,OBJPROP_COLOR);
             switch (touch_counts)
             {
               case 0: 
                        {
                          touch_txt = "Fresh";
                          if(chenge_zone_colors) cube_color = fresh_zone_color;
                          break;
                        }
               default: 
                        {
                           touch_txt = touch_counts +" touches";
                           if(chenge_zone_colors) cube_color = touched_zone_color;
                           break;
                        }
             
             }
             if(is_swap)
             {
                  touch_txt = "Swap";
                  if(chenge_zone_colors) cube_color = swapped_zone_color;
             }
             string clean_desc = ObjectDescription(obj_name);
             string clean_start,clean_end;
             int clear_tag_from=-1;
             bool object_is_LTF = false;
             if( StringFind(ObjectDescription(obj_name),"<LTF>",0) > -1)  
             {
               clear_tag_from = StringFind(ObjectDescription(obj_name),"<LTF>",0) ;
               clean_start = StringSubstr(ObjectDescription(obj_name), 0, clear_tag_from);
               clean_end   = StringSubstr(ObjectDescription(obj_name), clear_tag_from+5, StringLen(ObjectDescription(obj_name)));
               clean_desc =  clean_start+ clean_end;
               object_is_LTF = true;
             }
             if( StringFind(ObjectDescription(obj_name),"<HTF>",0) > -1)
             {
               clear_tag_from = StringFind(ObjectDescription(obj_name),"<HTF>",0) ;
               clean_start = StringSubstr(ObjectDescription(obj_name), 0, clear_tag_from);
               clean_end   = StringSubstr(ObjectDescription(obj_name), clear_tag_from+5, StringLen(ObjectDescription(obj_name)));
               clean_desc =  clean_start+ clean_end;
               
             }
             
             string txt1 = StringConcatenate(clean_desc,"    ",touch_txt);
             ObjectSet(obj_name,OBJPROP_TIME2,iTime(NULL,Period(),0)+Period()*60*15);
             ObjectSet(obj_name,OBJPROP_COLOR,cube_color);

             if(show_discription)
             {
               if(object_is_LTF && Period() < exclude_LTF_from )
               {
                  txt("rect_"+obj_name,txt1, 7, font_color,TimeCurrent()-Period(),price_for_tx); 
               } 
               if(!object_is_LTF)
               {
                  txt("rect_"+obj_name,txt1, 7, font_color,TimeCurrent()-Period(),price_for_tx); 
               }
             } 
               
             
             // Chnage Vieweing prefences
             string desc = ObjectDescription(obj_name);
             
             if( StringFind(desc,"<LTF>",0) < 0  &&  StringFind(desc,"<HTF>",0) < 0)
             {
                   Comment("dbg#1");
                 
                  if(exclude_LTF_from-1 < Period())
                  {
                     ObjectSetText(obj_name, ObjectDescription(obj_name)+"<HTF>",0);
                  }   
                  else
                  {
                     ObjectSetText(obj_name,ObjectDescription(obj_name)+"<LTF>",0);
                     ObjectSet(obj_name, OBJPROP_TIMEFRAMES, OBJ_PERIOD_M1 | OBJ_PERIOD_M5 | OBJ_PERIOD_M15 | OBJ_PERIOD_M30 | OBJ_PERIOD_H1);
                     
                  }    
                      
             }
             
          }  
    }
    
    
}
void vline(string name, string disc, datetime time, color col)
{
   
   name=prefix+"vline_"+name;
   if(ObjectFind(name)>-1) ObjectDelete(name);
   ObjectCreate(name,OBJ_VLINE,0,time,1);
   ObjectSet(name,OBJPROP_COLOR,col);
   ObjectSet(name,OBJPROP_BACK,true);
   ObjectSet(name,OBJPROP_STYLE,STYLE_DOT);
   ObjectSetText(name,disc,1);

}
void write(string name, string txt, int font_size, color col, int xpos, int ypos)
{
   
   name=prefix+"gcc_"+name;
   if(ObjectFind(name)==true) ObjectDelete(name);
   ObjectCreate(name,OBJ_LABEL,0,0,0);
   ObjectSetText(name,txt, font_size, "Arial", col);
   ObjectSet(name, OBJPROP_CORNER, 2);
   ObjectSet(name, OBJPROP_XDISTANCE, xpos);
   ObjectSet(name, OBJPROP_YDISTANCE, ypos);   
}

void txt(string name, string txt, int font_size, color col, datetime t, double p)
{
      name=prefix+name;
      if(ObjectFind(name)==true) ObjectDelete(name);
      ObjectCreate(name,OBJ_TEXT,0,t,p);
      ObjectSetText(name,txt, font_size, "Arial", col);
  
   
   //txt("name","string", 9, White,Time(),price);   
}
void delObj()

{
      string obj;
      for(int i=ObjectsTotal()-1; i>=0; i--) 
      {
			 obj = ObjectName(i);
		    if(StringFind(obj,prefix,0)>=0) ObjectDelete(obj);
		}    
}
int deinit()
{
   delObj();
   return(0);
}