//+------------------------------------------------------------------+
//|                                               ant-GUBreatout.mq4 |
//|                                                        avoitenko |
//|                        https://login.mql5.com/en/users/avoitenko |
//+------------------------------------------------------------------+
#property copyright  "avoitenko"
#property link       "https://login.mql5.com/en/users/avoitenko"

#property indicator_chart_window

//+------------------------------------------------------------------+
//|   Define                                                         |
//+------------------------------------------------------------------+
#define PREFIX    "myGUB"
#define FONT_NAME "Arial"

//+------------------------------------------------------------------+
//|   Extern parameters                                              |
//+------------------------------------------------------------------+
extern string  unique_number     =  "--- Unique number ---";
extern int     UNIQUE_NUMBER     =  1;
//---
extern string  time_options      =  "--- Time Setting ---";
extern int     GMT_SHIFT         =  0;
extern int     TIME_START_HOUR   =  12;
extern int     TIME_START_MINUTE =  0;
extern int     TIME_STOP_HOUR    =  16;
extern int     TIME_STOP_MINUTE  =  0;
extern int     TIME_END_HOUR     =  21;
extern int     TIME_END_MINUTE   =  0;
//---
extern string  show_options      =  "--- Display Setting ---";
extern int     DAYS_NUMBER       =  6;
extern int     PIPS_OFFSET       =  10;
extern color   COLOR_BOX         =  Brown;
extern color   COLOR_OFFSET      =  MidnightBlue;
extern color   LINE_COLOR        =  Silver;
extern color   LABEL_COLOR       =  Silver;
extern int     LABEL_SIZE        =  8;
extern bool    SHOW_LABEL        =  true;

//+------------------------------------------------------------------+
//|   Global variables                                               |
//+------------------------------------------------------------------+
double array1[][6];
int coef_point;
datetime day[];

//+------------------------------------------------------------------+
//|   init()                                                         |
//+------------------------------------------------------------------+
void init()
{
   coef_point = 1;
   if(Digits==3 || Digits==5) coef_point=10;

   ArrayResize(day,DAYS_NUMBER);
   
   if(GMT_SHIFT <-12) GMT_SHIFT = -12;
   if(GMT_SHIFT > 12) GMT_SHIFT = 12;
   
   
   if(TIME_START_HOUR   <  0) TIME_START_HOUR   = 0;
   if(TIME_START_HOUR   > 23) TIME_START_HOUR   = 23;
   if(TIME_START_MINUTE <  0) TIME_START_MINUTE = 0;
   if(TIME_START_MINUTE > 59) TIME_START_MINUTE = 59;
//---
   if(TIME_STOP_HOUR   <  0) TIME_STOP_HOUR     = 0;
   if(TIME_STOP_HOUR   > 23) TIME_STOP_HOUR     = 23;
   if(TIME_STOP_MINUTE <  0) TIME_STOP_MINUTE   = 0;
   if(TIME_STOP_MINUTE > 59) TIME_STOP_MINUTE   = 59;
//---
   if(TIME_END_HOUR   < 0 ) TIME_END_HOUR    = 0;
   if(TIME_END_HOUR   > 23) TIME_END_HOUR    = 23;
   if(TIME_END_MINUTE < 0 ) TIME_END_MINUTE  = 0;
   if(TIME_END_MINUTE > 59) TIME_END_MINUTE  = 59;
   
   start(); 
}

//+------------------------------------------------------------------+
//|   deinit()                                                       |
//+------------------------------------------------------------------+
void deinit()
{
   //--- delete all my objects
   int total = ObjectsTotal();
   for(int i=total-1;i>=0;i--)
   {
      string obj_name = ObjectName(i);
      if(StringFind(obj_name,PREFIX + "_" + UNIQUE_NUMBER, 0) == 0) ObjectDelete(obj_name);
   }
   WindowRedraw();
}
//+------------------------------------------------------------------+
//|   start()                                                        |
//+------------------------------------------------------------------+
void start()
{
   int counted_bars = IndicatorCounted();

   if(counted_bars<0)return(0);
   
   int count=0;
   if(counted_bars==0)
   {
      count = DAYS_NUMBER;
   }

   //--- calc days 
   int days = 0;
   datetime new_day = 0;
   for(int i=0; i<Bars && !IsStopped(); i++)
   {
      datetime _day = StrToTime(TimeYear(Time[i]) + "." + TimeMonth(Time[i]) + "." + TimeDay(Time[i]) + " 00:00");
      
      if(TimeDayOfWeek(_day)==0 ||  TimeDayOfWeek(_day)==6)continue;

      if(_day != new_day)
      {
         //Print(days," ",TimeToStr(_day));
         new_day = _day;
         day[days] = _day;
         days++;
      }
      if(days >= count) break;
   }   

   //--- main cycle 
   for(i=0; i<=count; i++)
   {
      //--- расчет времени
      datetime time_start  =  BuildTime(day[i], TIME_START_HOUR,  TIME_START_MINUTE,   GMT_SHIFT);
      datetime time_stop   =  BuildTime(day[i], TIME_STOP_HOUR,   TIME_STOP_MINUTE,    GMT_SHIFT);
      datetime time_end    =  BuildTime(day[i], TIME_END_HOUR,    TIME_END_MINUTE,     GMT_SHIFT);
      
      if(time_start > time_stop)
      {
         time_stop = time_stop + PERIOD_D1*60;
         time_end  = time_end  + PERIOD_D1*60;
      }
      if(time_end < time_stop) time_end = time_stop;
      
      //---       
      if(i==0 && (Time[0] < time_start || Time[0] > time_end))continue;

      int index_start   =  iBarShift(NULL, 0, time_start);
      int index_stop    =  iBarShift(NULL, 0, time_stop);
      int index_end     =  iBarShift(NULL, 0, time_end);
      
      int bars = index_start - index_stop + 1;
      
      double max_box  = High[ArrayMaximum(High, bars, index_stop)];
      double min_box  =  Low[ArrayMinimum(Low,  bars, index_stop)];
      double max_line = max_box + PIPS_OFFSET * coef_point * Point;
      double min_line = min_box - PIPS_OFFSET * coef_point * Point;
      
      SetRectangle("box_center_" + day[i], time_start, min_box,  time_stop, max_box,  COLOR_BOX);
      SetRectangle("box_all_"    + day[i], time_start, min_line, time_stop, max_line, COLOR_OFFSET);
           SetLine("high_line_"  + day[i], time_start, time_end, max_line);
           SetLine("low_line_"   + day[i], time_start, time_end, min_line);
      
      if(SHOW_LABEL)
      {
         string text = "[H]:" + DoubleToStr(max_box, Digits) + " [L]:" + DoubleToStr(min_box, Digits) + " [R]:" + DoubleToStr((max_box - min_box)/coef_point/Point,0);
           SetText("label_"   + day[i], time_start, max_line, text);
      }
   }
}
//+------------------------------------------------------------------+
//|   SetRectangle                                                   |
//+------------------------------------------------------------------+
void SetRectangle(string sufix,datetime time1,double price1, datetime time2, double price2,color clr)
{
   string obj_name = PREFIX + "_" + UNIQUE_NUMBER + "_" + sufix;
   if(ObjectFind(obj_name)!=-1 || ObjectCreate(obj_name,OBJ_RECTANGLE,0,time1,price1,time2,price2))
   {
      ObjectSet(obj_name, OBJPROP_TIME1,  time1);
      ObjectSet(obj_name, OBJPROP_TIME2,  time2);
      ObjectSet(obj_name, OBJPROP_PRICE1, price1);
      ObjectSet(obj_name, OBJPROP_PRICE2, price2);
      ObjectSet(obj_name, OBJPROP_COLOR,  clr);
      ObjectSet(obj_name, OBJPROP_BACK,   true); 
   }
}
//+------------------------------------------------------------------+
//|   SetLine                                                        |
//+------------------------------------------------------------------+
void SetLine(string sufix, datetime time1, datetime time2, double price1)
{
   string obj_name = PREFIX + "_" + UNIQUE_NUMBER + "_" + sufix;
   if(ObjectFind(obj_name) !=-1 || ObjectCreate(obj_name,OBJ_TREND,0,time1,price1,time2,price1))
   {
      ObjectSet(obj_name, OBJPROP_TIME1,  time1);
      ObjectSet(obj_name, OBJPROP_TIME2,  time2);
      ObjectSet(obj_name, OBJPROP_PRICE1, price1);
      ObjectSet(obj_name, OBJPROP_PRICE2, price1);
      ObjectSet(obj_name, OBJPROP_RAY,    false);
      ObjectSet(obj_name, OBJPROP_BACK,   true); 
      ObjectSet(obj_name, OBJPROP_COLOR,  LINE_COLOR);
      ObjectSet(obj_name, OBJPROP_WIDTH,  2);
   }
}
//+------------------------------------------------------------------+
//|   SetText                                                        |
//+------------------------------------------------------------------+
void SetText(string sufix, datetime time1, double price1, string text)
{
   string obj_name = PREFIX + "_" + UNIQUE_NUMBER + "_" + sufix;
   if(ObjectFind(obj_name) !=-1 || ObjectCreate(obj_name,OBJ_TEXT,0,time1,price1))
   {
      ObjectSet(obj_name, OBJPROP_TIME1,  time1);
      ObjectSet(obj_name, OBJPROP_PRICE1, price1);
      ObjectSet(obj_name, OBJPROP_BACK,   true);
      ObjectSet(obj_name, OBJPROP_COLOR,  LABEL_COLOR);
      ObjectSetText(obj_name, text, LABEL_SIZE,FONT_NAME, LABEL_COLOR);
   }
}
//+------------------------------------------------------------------+
//|   BuildTime                                                      |
//+------------------------------------------------------------------+
datetime BuildTime(datetime day, int hours, int minutes, int offset)
{
   return(day + hours*PERIOD_H1*60 + minutes*PERIOD_M1*60 + offset*PERIOD_H1*60);
}
//+------------------------------------------------------------------+

