//+------------------------------------------------------------------+
//|                                                      BoxFibo.mq4 |
//|                                              modificado por   egf|
//|                                              e_g_flores@yahoo.com|
//!                        sobre idea de DRJF y programadores previos|
//+------------------------------------------------------------------+

#property indicator_chart_window

extern string BoxStartTime    = "04:00";    // time for start of Box
extern string BoxEndTime      = "07:15";    // time for end of Box
extern string SR_StartTime    = "00:00";    // time for start of S/R line
extern string SR_EndTime      = "07:00";    // time for end of S/R line
extern int    NumDays      = 3;
extern color  BoxColor     = DodgerBlue;
extern bool   MuestraFibo = false;
extern color  FiboColor    = Gold;
extern int    LineWidth     = 3;
extern color  TopLineColor = Aqua;
extern color  BottomLineColor = Magenta;

bool BoxDrawOK = false;
bool SR_DrawOK = false;

//+------------------------------------------------------------------+
int init()  {
//+------------------------------------------------------------------+
  del_obj();
  BoxDrawOK = false;
  SR_DrawOK = false;
  return(0);
}

//+------------------------------------------------------------------+
int deinit()  {
//+------------------------------------------------------------------+
  del_obj();
  return(0);
}


//+------------------------------------------------------------------+
void start() 
{
//+------------------------------------------------------------------+


//-------------- dibuja Box con Fibo si está habilitada la opcion ---------------------------------------+

  datetime dt1 = StrToTime(TimeToStr(TimeCurrent(),TIME_DATE) + " " + BoxStartTime + ":00");
  datetime dt2 = StrToTime(TimeToStr(TimeCurrent(),TIME_DATE) + " " + BoxEndTime + ":00");
  int      dys = 0, NumBars = (dt2-dt1)/60/Period();
  
  if(TimeCurrent()<=dt2 || !BoxDrawOK)
  {
   int   BarDay = 1440/Period();
  
   for (int i=0; i< NumDays * BarDay /*Bars-NumBars */; i++)  
    {
     dt1 = StrToTime(TimeToStr(Time[i],TIME_DATE) + " "  + BoxStartTime + ":00");
     dt2 = StrToTime(TimeToStr(Time[i],TIME_DATE) + " "  + BoxEndTime + ":00");
     
     int ib1 = iBarShift(NULL,0,dt1);
     int ib2 = iBarShift(NULL,0,dt2);
     if (dt2 != Time[i])  continue;
     double vHigh = 0, vLow = 999;
     for (int j=ib1; j>ib2; j--)  {
       vHigh = MathMax(vHigh,High[j]);
       vLow  = MathMin(vLow,Low[j]);
     }
     string objname = "BF-Box-" + i;
     ObjectCreate(objname,OBJ_RECTANGLE,0,dt1,vHigh,dt2-60,vLow);
     ObjectSet(objname,OBJPROP_COLOR,BoxColor);
     if (MuestraFibo == true)
     {
     objname = "BF-Fibo-" + i;
     ObjectCreate(objname,OBJ_FIBO,0,dt2-7200,vHigh,dt2,vLow);
     ObjectSet(objname,OBJPROP_RAY,false);
     ObjectSet(objname,OBJPROP_COLOR,BoxColor);
     ObjectSet(objname,OBJPROP_LEVELCOLOR,FiboColor);
     ObjectSet(objname,OBJPROP_FIBOLEVELS,15);
     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.236);
     ObjectSet(objname,OBJPROP_FIRSTLEVEL+4,-0.382);
     ObjectSet(objname,OBJPROP_FIRSTLEVEL+5,-0.618);
     ObjectSet(objname,OBJPROP_FIRSTLEVEL+6,-1.0);
     ObjectSet(objname,OBJPROP_FIRSTLEVEL+7,1.236);
     ObjectSet(objname,OBJPROP_FIRSTLEVEL+8,1.382);
     ObjectSet(objname,OBJPROP_FIRSTLEVEL+9,1.618);
     ObjectSet(objname,OBJPROP_FIRSTLEVEL+10,2.0);
     ObjectSet(objname,OBJPROP_FIRSTLEVEL+11,-1.382);
     ObjectSet(objname,OBJPROP_FIRSTLEVEL+12,2.382);
     ObjectSet(objname,OBJPROP_FIRSTLEVEL+13,-1.618);
     ObjectSet(objname,OBJPROP_FIRSTLEVEL+14,2.618);
     ObjectSetFiboDescription(objname,0,"Box Edge = %$");     
     ObjectSetFiboDescription(objname,1,"Initial Stop = %$");     
     ObjectSetFiboDescription(objname,2,"Box Edge = %$");     
     ObjectSetFiboDescription(objname,3,"Entry = %$");     
     ObjectSetFiboDescription(objname,4,"TP2 Stop = %$");     
     ObjectSetFiboDescription(objname,5,"TP1 and TP3 Stop = %$");     
     ObjectSetFiboDescription(objname,6,"TP2 = %$");     
     ObjectSetFiboDescription(objname,7,"Entry = %$");     
     ObjectSetFiboDescription(objname,8,"TP2 Stop = %$");     
     ObjectSetFiboDescription(objname,9,"TP1 and TP3 Stop = %$");     
     ObjectSetFiboDescription(objname,10,"TP2 = %$");     
     ObjectSetFiboDescription(objname,11,"TP3 = %$");     
     ObjectSetFiboDescription(objname,12,"TP3 = %$");     
     ObjectSetFiboDescription(objname,13,"TP4 = %$");     
     ObjectSetFiboDescription(objname,14,"TP4 = %$");     
     //dys++; if (dys >= NumDays)   break;
    }

  }
  BoxDrawOK = true;
 }                                                             // fin del if(TimeCurrent()<=dt2 || !DrawOK)
//---------------------------- Fin del dibujo del Box           ------------------------------------------+
//---------------------------- Dibuja S/R de la sesion asiatica ------------------------------------------+

  datetime sr_dt1 = StrToTime(TimeToStr(TimeCurrent(),TIME_DATE) + " " + SR_StartTime + ":00");
  datetime sr_dt2 = StrToTime(TimeToStr(TimeCurrent(),TIME_DATE) + " " + SR_EndTime + ":00");
  //NumBars = (dt2-dt1)/60/Period();
 
//  Print("inicio SR: ", TimeToStr(sr_dt1)," Fin SR: ", TimeToStr(sr_dt2));
  if(TimeCurrent()<= sr_dt2 || !SR_DrawOK)
  {
  
   for (i=0; i< NumDays * BarDay /*Bars-NumBars */; i++)  
    {
     sr_dt1 = StrToTime(TimeToStr(Time[i],TIME_DATE) + " "  + SR_StartTime + ":00");
     sr_dt2 = StrToTime(TimeToStr(Time[i],TIME_DATE) + " "  + SR_EndTime + ":00");

     ib1 = iBarShift(NULL,0, sr_dt1);
     ib2 = iBarShift(NULL,0, sr_dt2);
     if (sr_dt2 != Time[i])  continue;
     double SR_High = 0, SR_Low = 999;
     for (j=ib1; j>ib2; j--)
      {
       SR_High = MathMax(SR_High,High[j]);
       SR_Low  = MathMin(SR_Low,Low[j]);
      }                                                        // fin del for(j...
      
                                                              // fin del for(i...
     string TopName    = "BF-SRT-" + i;
//     Print("inicio: ", TimeToStr(sr_dt1), " Fin ", TimeToStr(sr_dt2));
//     Print("Max ", SR_High, " Low ", SR_Low);
     ObjectCreate(TopName, OBJ_TREND, 0, sr_dt1, SR_High, sr_dt2,SR_High);
     ObjectSet(TopName, OBJPROP_WIDTH, LineWidth);
     ObjectSet(TopName, OBJPROP_COLOR, TopLineColor);
     ObjectSet(TopName,OBJPROP_STYLE,STYLE_SOLID);
     ObjectSet(TopName, OBJPROP_RAY, false);
     
     string BottomName = "BF-SRB-" + i;
     ObjectCreate(BottomName, OBJ_TREND, 0, sr_dt1, SR_Low, sr_dt2, SR_Low);
     ObjectSet(BottomName, OBJPROP_WIDTH, LineWidth);
     ObjectSet(BottomName, OBJPROP_COLOR, BottomLineColor);
     ObjectSet(BottomName,OBJPROP_STYLE,STYLE_SOLID);
     ObjectSet(BottomName, OBJPROP_RAY, false);
    }

   SR_DrawOK = true;
  }                                                      // fin del  if(TimeCurrent()<= sr_dt2 || !SR_DrawOK)
  
//--------------------------- Fin del dibujo de S/R de la sesion asiatica --------------------------------+  
  
  
  WindowRedraw();
  
  return(0);
}

//+------------------------------------------------------------------+
void del_obj()  {
//+------------------------------------------------------------------+
  int k=0;
  while (k<ObjectsTotal())   {
    string objname = ObjectName(k);
    if (StringSubstr(objname,0,3) == "BF-")  
      ObjectDelete(objname);
    else
      k++;
  }    
  return(0);
}


