//+------------------------------------------------------------------+
//|                                                 Bemac's ICCE.mq4 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, Bemac v1.2"

#property indicator_chart_window
extern string note7="UseBoxforRange=0,InputRange=1";
extern int rangeMethod=0;
extern double     topOfRange =1.3280;
extern double     bottomOfRange=1.3260;


extern string UniqueID        = "Asian Session"; 
extern string periodA_begin   = "01:00";     // Day0  Sydney Open
extern string periodA_end     = "03:00";     // Day0  Frankfurt Open
extern int    nextDayA        = 0;           // Set to zero if periodA_begin and periodA_end are on the same day.
                                             // Set to one if periodA_end is on the next day.
extern string periodB_end     = "03:00";     // Day0  New York Open
extern int    nextDayB        = 0;           // Set to zero if periodA_begin and periodB_end are on the same day.
                                             // Set to one if periodB_end is on the next day.
extern color  rectAB_color1      = PowderBlue; 
extern color  rectAB_color2      = LightCoral;
extern bool   rectAB_background  = true;   



extern double     TopPercent = 52.78;
extern double     BottomPercent = 47.22;
extern bool       TextRightShift = true;
extern int        TextOffset = 25;
extern int        FontSize = 10;       

extern color      RangeColor= Yellow;
double     Fib1      = 0.00;
extern double     Fib2      = 12.5;
extern double     Fib3      = 25;
extern double     Fib4      = 37.5;
extern double     Fib5      = 50;
extern double     Fib6      = 62.5;
extern double     Fib7      = 75;
extern double     Fib8      = 87.5;
double     Fib9      = 100;
extern double     Fib10     = 112.5;
extern double     Fib11     = -12.5;
extern color FibColor= Green;
extern bool   OpenClose          = false; 
extern bool connectfib=true;
 

double            pf,range,unit,pd,piptotal,fp1,fp2,fp3,fp4,fp5,fp6,fp7,fp8,fp9,fp10,fp11,mult=1,
                  TextAboveLine,TopOfRange,BottomOfRange;
string            filler = "";


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
      if ( Digits == 3 || Digits == 5 )
         mult = 10;
      TextAboveLine = NormalizeDouble(iATR(NULL,0,100,0)*0.50,Digits);   
      
      if ( TextRightShift )
         {
            filler = "                                  ";
            TextOffset = 0;
         }    

      
      pf    = TopPercent-BottomPercent;
      range = TopOfRange-BottomOfRange;
      unit  = range/pf;
      
//TopOfRange and BottomOfRange===============
      
      
      
      
   
      
      datetime dtTradeDate=TimeCurrent();
        if(OpenClose) string sRectABname = " BoxOC  ";
         else sRectABname = " BoxHL  ";
      DrawObjects(dtTradeDate, UniqueID , periodA_begin, periodA_end, periodB_end, rectAB_color1,rectAB_color2, 0, 1, rectAB_background,nextDayA,nextDayB,OpenClose,false);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
       
      DeleteObj();
      Comment("");

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  { 
      
      
      Sleep(2000); 
      
      if(rangeMethod==1)
      {  
      TopOfRange    =  topOfRange;
       BottomOfRange = bottomOfRange ;
       
      }
      else if (rangeMethod==0)
      {
      TopOfRange    = ObjectGet(UniqueID ,OBJPROP_PRICE1);
      BottomOfRange = ObjectGet(UniqueID ,OBJPROP_PRICE2); 
      }
      else 
      {
       TopOfRange    =  topOfRange;
       BottomOfRange = bottomOfRange ;  
       }
     if ( TopOfRange == 0 || BottomOfRange == 0 )
         {
        // Comment("hi",TopOfRange,TopOfRange);
           Comment ( "Bemac ICCE v1.2 ENTER A TOP RANGE AND A BOTTOM RANGE!" );
            return(0);
         } 
     

      if ( TopOfRange <= BottomOfRange )
         {
            Comment ("Bemac ICCE v1.2 TOP OF RANGE MUST BE GREATER THAN BOTTOM OF RANGE. TRY AGAIN!");
            DeleteObj(); 
            return(0);
         }
      
     
      pf    = TopPercent-BottomPercent;
      range = TopOfRange-BottomOfRange;
      unit  = range/pf;
      
//TopOfRange and BottomOfRange===============
     
        
//Fib1=======================================      
      pd       = MathAbs(TopPercent-Fib1);
      piptotal = unit*pd;
      if ( Fib1 < TopPercent )
         fp1 = TopOfRange-piptotal;  
      else 
         fp1 = TopOfRange+piptotal;
       
                  

//Fib9=======================================      
      pd       = MathAbs(TopPercent-Fib9);
      piptotal = unit*pd;
      if ( Fib9 < TopPercent )
         fp9 = TopOfRange-piptotal;  
      else 
         fp9 = TopOfRange+piptotal;
      
                   

      

  if (connectfib)
  {
  DrawFib(UniqueID, fp1,fp9);
  }
   return(0);      
  }
//+------------------------------------------------------------------+

void DeleteObj()
   {
     
     
   ObjectDelete("Fibo"); 
  // ObjectDelete(UniqueID);
   
      
      return(0);
   }    
   void DrawObjects(datetime dtTradeDate, string sObjName, string sTimeBegin, string sTimeEnd, string sTimeObjEnd, color cObjColor1, color cObjColor2, int iOffSet, int iForm, bool background, int nextDayA, int nextDayB, bool OpenClose, bool rectA_close_begin) {
  datetime dtTimeBegin, dtTimeEnd, dtTimeObjEnd;
  double   dPriceHigh,  dPriceLow, dPriceOpen, dPriceClose;
  int      iBarBegin,   iBarEnd;
  string   sObjDesc;

  dtTimeBegin = StrToTime(TimeToStr(dtTradeDate, TIME_DATE) + " " + sTimeBegin);
  dtTimeEnd = StrToTime(TimeToStr(dtTradeDate, TIME_DATE) + " " + sTimeEnd);
  dtTimeObjEnd = StrToTime(TimeToStr(dtTradeDate, TIME_DATE) + " " + sTimeObjEnd);
  
  if(nextDayA == 1) dtTimeEnd = dtTimeEnd + 86400;
  if(nextDayB == 1) dtTimeObjEnd = dtTimeObjEnd + 86400;
  if(nextDayA == 1 && TimeDayOfWeek(dtTradeDate) == 5) dtTimeEnd = dtTimeEnd + (2 * 86400);
  if(nextDayB == 1 && TimeDayOfWeek(dtTradeDate) == 5) dtTimeObjEnd = dtTimeObjEnd + (2 * 86400);
      
  iBarBegin = iBarShift(NULL, 0, dtTimeBegin)+1;                                    // added 1 to bar count to correct calculation for highest price for the period
  iBarEnd = iBarShift(NULL, 0, dtTimeEnd)+1;                                        // added 1 to bar count to correct calculation for lowest price for the period 
  
   dPriceHigh  = High[Highest(NULL, 0, MODE_HIGH, (iBarBegin)-iBarEnd, iBarEnd)];
   dPriceLow   = Low [Lowest (NULL, 0, MODE_LOW , (iBarBegin)-iBarEnd, iBarEnd)];
   dPriceOpen  = Open[iBarBegin-1];                                                 // Open/Close added to enable Open/Close rectangles
   dPriceClose = Close[iBarEnd];
   
      if(OpenClose){                                                                // Selection of extremes of Open/Close values
         dPriceHigh = MathMax(dPriceOpen, dPriceClose);
         dPriceLow  = MathMin(dPriceOpen, dPriceClose); 
         }
  
//---- High-Low Rectangle - Period A and B combined
   if(iForm==1){  
      ObjectCreate(sObjName, OBJ_RECTANGLE, 0, 0, 0, 0, 0);
      ObjectSet(sObjName, OBJPROP_TIME1 , dtTimeBegin);
      ObjectSet(sObjName, OBJPROP_TIME2 , dtTimeObjEnd);
      ObjectSet(sObjName, OBJPROP_PRICE1, dPriceHigh);  
      ObjectSet(sObjName, OBJPROP_PRICE2, dPriceLow);
      ObjectSet(sObjName, OBJPROP_STYLE, STYLE_SOLID);
      if(dPriceClose<dPriceOpen && OpenClose==true) cObjColor1 = cObjColor2;        // Color change of rectangle dependent on Open/Close positions
      ObjectSet(sObjName, OBJPROP_COLOR, cObjColor1);
      ObjectSet(sObjName, OBJPROP_BACK, background);
   }
   
 
 } 
 
void DrawFib(string sObjName,  double bottomofFib, double topofFib)
{  
//draw fibs
static double oldtopofFib=0;
            if (oldtopofFib!=topofFib)
            {
            ObjectDelete("Fibo"); 
            datetime T1,T2;
            T1= ObjectGet(sObjName, OBJPROP_TIME1 );
            T2=ObjectGet(sObjName, OBJPROP_TIME2 );
            double lowest,highest;
            highest=ObjectGet(sObjName, OBJPROP_PRICE1); 
            lowest=ObjectGet(sObjName, OBJPROP_PRICE2);
            
                ObjectCreate("Fibo", OBJ_FIBO, 0, T2, topofFib, T1,bottomofFib);
           
  
   double  fib2,fib3,fib4,fib5,fib6,fib7,fib8,fib9,fib10,fib11,topPercent,bottomPercent;
   fib2=Fib2/100;
   fib3=Fib3/100;
   fib4=Fib4/100;
   fib5=Fib5/100;
   fib6=Fib6/100;
   fib7=Fib7/100;
   fib8=Fib8/100;
   fib9=Fib9/100;
   fib10=Fib10/100;
   fib11=Fib11/100;
   topPercent=TopPercent/100;
   bottomPercent=BottomPercent/100;
  

            string fiboobjname = "Fibo";
            ObjectSet(fiboobjname, OBJPROP_FIBOLEVELS, 12);
            
            ObjectSet(fiboobjname, OBJPROP_FIRSTLEVEL, Fib1);
               ObjectSetFiboDescription(fiboobjname,0,DoubleToStr(Fib1,Digits)+"    %$");
               
             
                ObjectSet(fiboobjname, OBJPROP_FIRSTLEVEL+1, fib2);
               ObjectSetFiboDescription(fiboobjname,1,DoubleToStr(Fib2,Digits)+"    %$");
   
              
                ObjectSet(fiboobjname, OBJPROP_FIRSTLEVEL+2, fib3);
              ObjectSetFiboDescription(fiboobjname,2,DoubleToStr(Fib3,Digits)+"    %$");
               
             
              ObjectSet(fiboobjname, OBJPROP_FIRSTLEVEL+3, fib4);
               ObjectSetFiboDescription(fiboobjname,3,DoubleToStr(Fib4,Digits)+"    %$");
   
              ObjectSet(fiboobjname, OBJPROP_FIRSTLEVEL+4, bottomPercent);
              ObjectSetFiboDescription(fiboobjname,4,DoubleToStr(bottomPercent,Digits)+"    %$");
   
             
               ObjectSet(fiboobjname, OBJPROP_FIRSTLEVEL+5, fib5);
              ObjectSetFiboDescription(fiboobjname,5,DoubleToStr(Fib5,Digits)+"    %$");
   
             ObjectSet(fiboobjname, OBJPROP_FIRSTLEVEL+6, topPercent);
               ObjectSetFiboDescription(fiboobjname,6,DoubleToStr(topPercent,Digits)+"    %$");
   
             
                ObjectSet(fiboobjname, OBJPROP_FIRSTLEVEL+7, fib6);
               ObjectSetFiboDescription(fiboobjname,7,DoubleToStr(Fib6,Digits)+"    %$");
             
   
             
               ObjectSet(fiboobjname, OBJPROP_FIRSTLEVEL+8, fib7);
           ObjectSetFiboDescription(fiboobjname,8,DoubleToStr(Fib7,Digits)+"    %$");
   
             
              ObjectSet(fiboobjname, OBJPROP_FIRSTLEVEL+9, fib8);
           ObjectSetFiboDescription(fiboobjname,9,DoubleToStr(Fib8,Digits)+"    %$");
             
           
          ObjectSet(fiboobjname, OBJPROP_FIRSTLEVEL+10, fib9);
           ObjectSetFiboDescription(fiboobjname,10,DoubleToStr(Fib9,Digits)+"    %$");
           
           
   
             
            
                ObjectSet(fiboobjname, OBJPROP_FIRSTLEVEL+11, fib10);
           ObjectSetFiboDescription(fiboobjname,11,DoubleToStr(Fib10,Digits)+"    %$");
   
    
   
               ObjectSet( "Fibo", OBJPROP_LEVELCOLOR, FibColor) ;
               ObjectsRedraw();
               oldtopofFib=topofFib;
               }
   
   return(0); 
   }   