//+------------------------------------------------------------------+
//|                                                      BoxFibo.mq4 |
//+------------------------------------------------------------------+

#property indicator_chart_window

extern string StartTime    = "05:00";    // time for start of price establishment window
extern string EndTime      = "08:00";    // time for end of price establishment window
extern int    NumDays      = 1;
extern color  BoxColor     = LightBlue;
extern color  FiboColor    = DimGray;
extern color  LevelColor   = DimGray;
extern int    BarsBack     = 1000;
extern int    FibLength    = 8; //Fib swing will be drawn from left box to 2 bars after that  

int ObjCnt;
//+------------------------------------------------------------------+
int init()  {
//+------------------------------------------------------------------+
  del_obj();
  
  return(0);
}

//+------------------------------------------------------------------+
int deinit()  {
//+------------------------------------------------------------------+
  del_obj();
  return(0);
}


//+------------------------------------------------------------------+
void start()  {
//+------------------------------------------------------------------+
 int i, limit, counted_bars=IndicatorCounted();
 limit = MathMin(BarsBack,Bars-counted_bars-1);
  //datetime dt1 = StrToTime(TimeToStr(TimeCurrent(),TIME_DATE) + " " + StartTime + ":00");
  //datetime dt2 = StrToTime(TimeToStr(TimeCurrent(),TIME_DATE) + " " + EndTime + ":00");
  //int      dys = 0, NumBars = (dt2-dt1)/60/Period();
  
  
  for (i=limit; i>=0; i--)//< NumDays * 96 /*Bars-NumBars */; i++)  
   {   
    datetime thistime = Time[i];  
    datetime dt2 = StrToTime(TimeToStr(Time[i],TIME_DATE) + " "  + EndTime);
    datetime dt1 = StrToTime(TimeToStr(Time[i],TIME_DATE) + " "  + StartTime);    
    if(thistime < dt2) continue;
    if(TimeDayOfWeek(dt1)==0) continue; //TimeDayOfWeek of Sunday = 0, Monday = 1, etc.

    string objname = "BF-Box-"+dt2;
    if(ObjectFind(objname)!=-1) continue;

    int ib1 = iBarShift(NULL,0,dt1);
    int ib2 = iBarShift(NULL,0,dt2);
    int thigh = iHighest(NULL,0,MODE_HIGH,(ib1-ib2+1),ib2);
    int tlow = iLowest(NULL,0,MODE_LOW,(ib1-ib2+1),ib2);
    double vHigh = High[thigh];
    double vLow  = Low[tlow];
    double pt1, pt2;
    if(tlow > thigh) {
      pt1 = vLow;
      pt2 = vHigh;
    }
    else {
      pt1 = vHigh;
      pt2 = vLow;
    }
    ObjectCreate(objname,OBJ_RECTANGLE,0,dt1,pt1,dt2,pt2);
    ObjectSet(objname,OBJPROP_COLOR,BoxColor);
    objname = "BF-Fibo-" + dt2;
    ObjectCreate(objname,OBJ_FIBO,0,dt1,pt1,Time[ib1-FibLength],pt2);    
    ObjectSet(objname,OBJPROP_RAY,false);
    ObjectSet(objname,OBJPROP_COLOR,DimGray);
    ObjectSet(objname,OBJPROP_LEVELCOLOR,DimGray);
    ObjectSet(objname,OBJPROP_FIBOLEVELS,13);
    ObjectSet(objname,OBJPROP_LEVELSTYLE,STYLE_SOLID);
    ObjectSet(objname,OBJPROP_FIRSTLEVEL,0.0);
    ObjectSet(objname,OBJPROP_FIRSTLEVEL+1,0.5);
    ObjectSet(objname,OBJPROP_FIRSTLEVEL+2,1.0);
    ObjectSet(objname,OBJPROP_FIRSTLEVEL+3,-0.382);
    ObjectSet(objname,OBJPROP_FIRSTLEVEL+4,-0.618);
    ObjectSet(objname,OBJPROP_FIRSTLEVEL+5,-1.0);
    ObjectSet(objname,OBJPROP_FIRSTLEVEL+6,1.382);
    ObjectSet(objname,OBJPROP_FIRSTLEVEL+7,1.618);
    ObjectSet(objname,OBJPROP_FIRSTLEVEL+8,2.0);
    ObjectSet(objname,OBJPROP_FIRSTLEVEL+9,-1.618);
    ObjectSet(objname,OBJPROP_FIRSTLEVEL+10,2.618);
    ObjectSet(objname,OBJPROP_FIRSTLEVEL+11,-3.236);
    ObjectSet(objname,OBJPROP_FIRSTLEVEL+12,4.236);
    ObjectSetFiboDescription(objname,0,"Level 2 Stop = %$");     
    ObjectSetFiboDescription(objname,1,"Level 1 Stop = %$");     
    ObjectSetFiboDescription(objname,2,"Level 2 Stop = %$");     
    ObjectSetFiboDescription(objname,3,"Level 3 Stop/Entry = %$");          
    ObjectSetFiboDescription(objname,4,"Level 1 = %$");     
    ObjectSetFiboDescription(objname,5,"Level 2 = %$");     
    ObjectSetFiboDescription(objname,6,"Level 3 Stop/Entry = %$");         
    ObjectSetFiboDescription(objname,7,"Level 1 = %$");     
    ObjectSetFiboDescription(objname,8,"Level 2 = %$");  
    ObjectSetFiboDescription(objname,9,"Level 3 = %$");     
    ObjectSetFiboDescription(objname,10,"level 3 = %$"); 
    ObjectSetFiboDescription(objname,11,"level 4 = %$");
    ObjectSetFiboDescription(objname,12,"level 4 = %$");    
    //dys++; if (dys >= NumDays)   break;
  }
  
  //WindowRedraw();
  return(0);
}

//+------------------------------------------------------------------+
void del_obj()  {
//+------------------------------------------------------------------+
  string ObjName;
  int ObjTotal = ObjectsTotal();
  for(int i = ObjTotal-1; i>=0; i--) {
    ObjName = ObjectName(i);
    if(StringFind(ObjName,"BF",0)!=-1) ObjectDelete(ObjName);
  }
  return(0);
}