//+------------------------------------------------------------------+
//|                                                 Bemac's ICCE.mq4 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, Bemac v1.2"

#property indicator_chart_window
extern string UniqueIDBox        = "AS";
extern string UniqueIDFib        = "ASFib";  
extern string note7="UseBoxforRange=0,InputRange=1";
extern int rangeMethod=0;
extern double     topOfRange =1.3280;
extern double     bottomOfRange=1.3260;

extern string BoxBegin    = "23:00";   //
extern string BoxEnd      = "01:00";   // 
extern int    nextDayEnd        = 1; 
extern color  BoxColor    = MistyRose; //
extern int    NumberOfDays = 1; 
extern  bool DontMoveBox =true;

extern string note1="Set to 0 if boxbegin and boxend on the same day.";
extern string note2="Set to 1 if boxend is on the next day.";
         
                                            

extern string note3="Set to 0 for Bemac defaults, 1 for Eigths";
extern int     useBemacorEights = 0;
extern double     eTopPercent = 52.78;
extern double     eBottomPercent = 47.22;
    
extern double     bTopPercent = 52.8;
extern double     bBottomPercent = 47.2;
    

double            eFib1      = 0.00;
extern double     eFib2      = 12.5;
extern double     eFib3      = 25;
extern double     eFib4      = 37.5;
extern double     eFib5      = 50;
extern double     eFib6      = 62.5;
extern double     eFib7      = 75;
extern double     eFib8      = 87.5;
double            eFib9      = 100;
extern double     eFib10     = 112.5;
extern double     eFib11     = -12.5;

double            bFib1      = 0.00;
extern double     bFib2      = 14.6;
extern double     bFib3      = 23.6;
extern double     bFib4      = 38.2;
extern double     bFib5      = 50;
extern double     bFib6      = 61.8;
extern double     bFib7      = 76.4;
extern double     bFib8      = 85.4;
double            bFib9      = 100;
extern double     bFib10     = 161.6;
extern double     bFib11     = -12.5;


extern color FibColor= Green;
 
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;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators

 DeleteObjects();

      if ( Digits == 3 || Digits == 5 )
         mult = 10;
      

     
//TopOfRange and BottomOfRange===============
      
        datetime dt=CurTime();
      int i;
      if (rangeMethod==0)
      {
         for ( i=0; i<NumberOfDays; i++) 
         {
         CreateObjects(UniqueIDBox+i, BoxColor);
   
         }
         if (DontMoveBox==true)
         {
           bool ckhighest=ObjectGet(UniqueIDBox+0, OBJPROP_PRICE1); 
             Comment("b", ckhighest,BoxBegin,BoxEnd);
              if (ckhighest>0)
              {
              BoxBegin= ObjectGet(UniqueIDBox+0, OBJPROP_TIME1);
              BoxEnd = ObjectGet(UniqueIDBox+0, OBJPROP_TIME2);
              Comment("b1", ckhighest,BoxBegin,BoxEnd);
   
               DrawObjects(dt, UniqueIDBox+i, BoxBegin, BoxEnd);
              }
              else
              {
               Comment("b3",ckhighest, BoxBegin,BoxEnd);
                 for ( i=0; i<NumberOfDays; i++) 
                 {
                 DrawObjects(dt, UniqueIDBox+i, BoxBegin, BoxEnd);
  
                dt=decDateTradeDay(dt);
                while (TimeDayOfWeek(dt)>5) dt=decDateTradeDay(dt);
                }
            }
       }
         if (DontMoveBox==false)
         {
              for ( i=0; i<NumberOfDays; i++) 
              {
              DrawObjects(dt, UniqueIDBox+i, BoxBegin, BoxEnd);
  
             dt=decDateTradeDay(dt);
             while (TimeDayOfWeek(dt)>5) dt=decDateTradeDay(dt);
             }
          }
   }
      
   if ( rangeMethod==1)
   {
   DeleteBox();
   }
   
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
       
      DeleteObjects();
    //  DeleteBox();
      Comment("");

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  { 
      
       
      Sleep(2000); 
      
      if(rangeMethod==1)//userange
      {  
       TopOfRange    =  topOfRange;
       BottomOfRange = bottomOfRange ;
       
      }
      else if (rangeMethod==0)//usebox
      {
      TopOfRange    = ObjectGet(UniqueIDBox+0 ,OBJPROP_PRICE1);
      BottomOfRange = ObjectGet(UniqueIDBox+0 ,OBJPROP_PRICE2); 
      }
      else //default use range
      {
       TopOfRange    =  topOfRange;
       BottomOfRange = bottomOfRange ;  
       }
     if ( TopOfRange == 0 || BottomOfRange == 0 )
         {
       Comment ("Please input a range or select a box");
            return(0);
         } 
     

      if ( TopOfRange <= BottomOfRange )
         {
           
           DeleteObjects(); 
            return(0);
         }
     
     if ( useBemacorEights == 0 )
      {
      pf    = eTopPercent-eBottomPercent;
      range = TopOfRange-BottomOfRange;
      unit  = range/pf;
      }
      else if  ( useBemacorEights == 1 )
      
      {
      pf    = bTopPercent-bBottomPercent;
      range = TopOfRange-BottomOfRange;
      unit  = range/pf;
      }
      
//TopOfRange and BottomOfRange===============
     
        
//Fib1=======================================      
      pd       = MathAbs(eTopPercent-eFib1);
      piptotal = unit*pd;
      if ( eFib1 < eTopPercent )
         fp1 = TopOfRange-piptotal;  
      else 
         fp1 = TopOfRange+piptotal;
       
                  

//Fib9=======================================      
      pd       = MathAbs(eTopPercent-eFib9);
      piptotal = unit*pd;
      if ( eFib9 < eTopPercent )
         fp9 = TopOfRange-piptotal;  
      else 
         fp9 = TopOfRange+piptotal;
      
                   

      

  if (connectfib)
  {
      if ( useBemacorEights == 0 )
      {
      DrawBFib(UniqueIDBox+0, fp1,fp9);
      }
      else if ( useBemacorEights == 1 )
      {
       DrawEFib(UniqueIDBox+0, fp1,fp9);
      }
      else
      {
      DrawBFib(UniqueIDBox+0, fp1,fp9);
      }
  }
   return(0);      
  }
//+------------------------------------------------------------------+

void CreateObjects(string no, color cl) {
  ObjectCreate(no, OBJ_RECTANGLE, 0, 0,0, 0,0);
  ObjectSet(no, OBJPROP_STYLE, STYLE_SOLID);
  ObjectSet(no, OBJPROP_COLOR, cl);
  ObjectSet(no, OBJPROP_BACK, True);
}

void DeleteObjects()
   {
     
     
   ObjectDelete(UniqueIDFib); 
  
 
      
      return(0);
   }   
void DeleteBox()
{

  for (int i=0; i<NumberOfDays; i++) {
    ObjectDelete(UniqueIDBox+i);
    
   }

}

void DrawObjects(datetime dt, string no, string tb, string te) {
  datetime t1, t2;
  double   p1, p2;
  int      b1, b2;
  
   datetime dtTradeDate=TimeCurrent();

  
  

  t1=StrToTime(TimeToStr(dt, TIME_DATE)+" "+tb);
  t2=StrToTime(TimeToStr(dt, TIME_DATE)+" "+te);
 // if(nextDayEnd == 1) t2 = t2 + 86400;
if(nextDayEnd == 1) t1 = t1 - 86400;
  if(nextDayEnd == 1 && TimeDayOfWeek(dtTradeDate) == 1) t1 = t1 - (2 * 86400);
  
  b1=iBarShift(NULL, 0, t1);
  b2=iBarShift(NULL, 0, t2);
  p1=High[Highest(NULL, 0, MODE_HIGH, b1-b2, b2)];
  p2=Low [Lowest (NULL, 0, MODE_LOW , b1-b2, b2)];
  ObjectSet(no, OBJPROP_TIME1 , t1);
  ObjectSet(no, OBJPROP_PRICE1, p1);
  ObjectSet(no, OBJPROP_TIME2 , t2);
  ObjectSet(no, OBJPROP_PRICE2, p2);
}
 
   
 
 
datetime decDateTradeDay (datetime dt) {
  int ty=TimeYear(dt);
  int tm=TimeMonth(dt);
  int td=TimeDay(dt);
  int th=TimeHour(dt);
  int ti=TimeMinute(dt);

  td--;
  if (td==0) {
    tm--;
    if (tm==0) {
      ty--;
      tm=12;
    }
    if (tm==1 || tm==3 || tm==5 || tm==7 || tm==8 || tm==10 || tm==12) td=31;
    if (tm==2) if (MathMod(ty, 4)==0) td=29; else td=28;
    if (tm==4 || tm==6 || tm==9 || tm==11) td=30;
  }
  return(StrToTime(ty+"."+tm+"."+td+" "+th+":"+ti));
} 
void DrawEFib(string sObjName,  double bottomofFib, double topofFib)
{  
//draw fibs
static double oldtopofFib=0;
            if (oldtopofFib!=topofFib)
            {
            ObjectDelete(UniqueIDFib); 
            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(UniqueIDFib, OBJ_FIBO, 0, T2, topofFib, T1,bottomofFib);
           
  
   double  efib2,efib3,efib4,efib5,efib6,efib7,efib8,efib9,efib10,efib11,etopPercent,ebottomPercent;
   efib2=eFib2/100;
   efib3=eFib3/100;
   efib4=eFib4/100;
   efib5=eFib5/100;
   efib6=eFib6/100;
   efib7=eFib7/100;
   efib8=eFib8/100;
   efib9=eFib9/100;
   efib10=eFib10/100;
   efib11=eFib11/100;
   etopPercent=eTopPercent/100;
   ebottomPercent=eBottomPercent/100;
  

            string fiboobjname = UniqueIDFib;
            ObjectSet(fiboobjname, OBJPROP_FIBOLEVELS, 12);
            
            ObjectSet(fiboobjname, OBJPROP_FIRSTLEVEL, eFib1);
               ObjectSetFiboDescription(fiboobjname,0,DoubleToStr(eFib1,Digits)+"    %$");
               
             
                ObjectSet(fiboobjname, OBJPROP_FIRSTLEVEL+1, efib2);
               ObjectSetFiboDescription(fiboobjname,1,DoubleToStr(eFib2,Digits)+"    %$");
   
              
                ObjectSet(fiboobjname, OBJPROP_FIRSTLEVEL+2, efib3);
              ObjectSetFiboDescription(fiboobjname,2,DoubleToStr(eFib3,Digits)+"    %$");
               
             
              ObjectSet(fiboobjname, OBJPROP_FIRSTLEVEL+3, efib4);
               ObjectSetFiboDescription(fiboobjname,3,DoubleToStr(eFib4,Digits)+"    %$");
   
              ObjectSet(fiboobjname, OBJPROP_FIRSTLEVEL+4, ebottomPercent);
              ObjectSetFiboDescription(fiboobjname,4,DoubleToStr(ebottomPercent,Digits)+"    %$");
   
             
               ObjectSet(fiboobjname, OBJPROP_FIRSTLEVEL+5, efib5);
              ObjectSetFiboDescription(fiboobjname,5,DoubleToStr(eFib5,Digits)+"    %$");
   
             ObjectSet(fiboobjname, OBJPROP_FIRSTLEVEL+6, etopPercent);
               ObjectSetFiboDescription(fiboobjname,6,DoubleToStr(etopPercent,Digits)+"    %$");
   
             
                ObjectSet(fiboobjname, OBJPROP_FIRSTLEVEL+7, efib6);
               ObjectSetFiboDescription(fiboobjname,7,DoubleToStr(eFib6,Digits)+"    %$");
             
   
             
               ObjectSet(fiboobjname, OBJPROP_FIRSTLEVEL+8, efib7);
           ObjectSetFiboDescription(fiboobjname,8,DoubleToStr(eFib7,Digits)+"    %$");
   
             
              ObjectSet(fiboobjname, OBJPROP_FIRSTLEVEL+9, efib8);
           ObjectSetFiboDescription(fiboobjname,9,DoubleToStr(eFib8,Digits)+"    %$");
             
           
          ObjectSet(fiboobjname, OBJPROP_FIRSTLEVEL+10, efib9);
           ObjectSetFiboDescription(fiboobjname,10,DoubleToStr(eFib9,Digits)+"    %$");
           
           
   
             
            
                ObjectSet(fiboobjname, OBJPROP_FIRSTLEVEL+11, efib10);
           ObjectSetFiboDescription(fiboobjname,11,DoubleToStr(eFib10,Digits)+"    %$");
   
    
   
               ObjectSet( fiboobjname, OBJPROP_LEVELCOLOR, FibColor) ;
               ObjectsRedraw();
               oldtopofFib=topofFib;
               }
   
   return(0); 
   } 
   
void DrawBFib(string sObjName,  double bottomofFib, double topofFib)
{  
//draw fibs
static double oldtopofFib=0;
            if (oldtopofFib!=topofFib)
            {
            ObjectDelete(UniqueIDFib); 
            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(UniqueIDFib, OBJ_FIBO, 0, T2, topofFib, T1,bottomofFib);
           
  
   double  bfib2,bfib3,bfib4,bfib5,bfib6,bfib7,bfib8,bfib9,bfib10,bfib11,btopPercent,bbottomPercent;
  bfib2=bFib2/100;
   bfib3=bFib3/100;
   bfib4=bFib4/100;
   bfib5=bFib5/100;
   bfib6=bFib6/100;
   bfib7=bFib7/100;
   bfib8=bFib8/100;
   bfib9=bFib9/100;
   bfib10=bFib10/100;
   bfib11=bFib11/100;
   btopPercent=bTopPercent/100;
   bbottomPercent=bBottomPercent/100;
  

            string fiboobjname = UniqueIDFib;
            ObjectSet(fiboobjname, OBJPROP_FIBOLEVELS, 12);
            
            ObjectSet(fiboobjname, OBJPROP_FIRSTLEVEL, bFib1);
               ObjectSetFiboDescription(fiboobjname,0,DoubleToStr(bFib1,Digits)+"    %$");
               
             
                ObjectSet(fiboobjname, OBJPROP_FIRSTLEVEL+1, bfib2);
               ObjectSetFiboDescription(fiboobjname,1,DoubleToStr(bFib2,Digits)+"    %$");
   
              
                ObjectSet(fiboobjname, OBJPROP_FIRSTLEVEL+2, bfib3);
              ObjectSetFiboDescription(fiboobjname,2,DoubleToStr(bFib3,Digits)+"    %$");
               
             
              ObjectSet(fiboobjname, OBJPROP_FIRSTLEVEL+3, bfib4);
               ObjectSetFiboDescription(fiboobjname,3,DoubleToStr(bFib4,Digits)+"    %$");
   
              ObjectSet(fiboobjname, OBJPROP_FIRSTLEVEL+4, bbottomPercent);
              ObjectSetFiboDescription(fiboobjname,4,DoubleToStr(bbottomPercent,Digits)+"    %$");
   
             
               ObjectSet(fiboobjname, OBJPROP_FIRSTLEVEL+5, bfib5);
              ObjectSetFiboDescription(fiboobjname,5,DoubleToStr(bFib5,Digits)+"    %$");
   
             ObjectSet(fiboobjname, OBJPROP_FIRSTLEVEL+6, btopPercent);
               ObjectSetFiboDescription(fiboobjname,6,DoubleToStr(btopPercent,Digits)+"    %$");
   
             
                ObjectSet(fiboobjname, OBJPROP_FIRSTLEVEL+7, bfib6);
               ObjectSetFiboDescription(fiboobjname,7,DoubleToStr(bFib6,Digits)+"    %$");
             
   
             
               ObjectSet(fiboobjname, OBJPROP_FIRSTLEVEL+8, bfib7);
           ObjectSetFiboDescription(fiboobjname,8,DoubleToStr(bFib7,Digits)+"    %$");
   
             
              ObjectSet(fiboobjname, OBJPROP_FIRSTLEVEL+9, bfib8);
           ObjectSetFiboDescription(fiboobjname,9,DoubleToStr(bFib8,Digits)+"    %$");
             
           
          ObjectSet(fiboobjname, OBJPROP_FIRSTLEVEL+10, bfib9);
           ObjectSetFiboDescription(fiboobjname,10,DoubleToStr(bFib9,Digits)+"    %$");
           
           
   
             
            
                ObjectSet(fiboobjname, OBJPROP_FIRSTLEVEL+11, bfib10);
           ObjectSetFiboDescription(fiboobjname,11,DoubleToStr(bFib10,Digits)+"    %$");
   
    
   
               ObjectSet( fiboobjname, OBJPROP_LEVELCOLOR, FibColor) ;
               ObjectsRedraw();
               oldtopofFib=topofFib;
               }
   
   return(0); 
   }    