//+------------------------------------------------------------------+
//|                                                  FibSemiAuto.mq4 |
//|                                  Copyright © 2010, John Wustrack |
//|                                        john_wustrack@hotmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, John Wustrack"
#property link      "john_wustrack@hotmail.com"

#property indicator_chart_window

extern string Copyright    = "Copyright © 2011, John Wustrack";
extern string Copyright2   = "eMail: john_wustrack@hotmail.com";

extern int xi_Period = 10080;
extern bool xb_MoveHiLo = true;
extern bool xb_ShowPrice = true;
extern color xc_Color_Fib = Yellow;

int gi_FibLevels;
double gd_FLvl[5];
double gd_High, gd_Low;

datetime gdt_LastBar;
datetime gdt_Times[];

int gi_BarOffset = 1;
string gs_Fibo;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- 
   
   gd_FLvl[0]=0;
   gd_FLvl[1]=0.382;
   gd_FLvl[2]=0.5;
   gd_FLvl[3]=0.618;
   gd_FLvl[4]=1;
   
   gi_FibLevels=5;
   
   gs_Fibo = "!" + xi_Period + " Fibo";
      
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   ObjectDelete(gs_Fibo);   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
//----
   bool lb_Changed=false;
   static datetime ldt_Time;
   
   // Check if the bar has moved
   if (iTime(NULL,xi_Period,gi_BarOffset) != gdt_LastBar)
      {
      gdt_LastBar = iTime(NULL,xi_Period,gi_BarOffset);
      lb_Changed = true;
      }
      
   // If the move Hi/Lo is set then we need to check if there is a new hi/lo
   if (xb_MoveHiLo)
      if (Time[0] != ldt_Time)
         {
         lb_Changed = true;
         ldt_Time = Time[0];
         }
         
   if (lb_Changed)
      Calculate_Fibo();
   
       
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| calculate the fibonacci                                          |
//+------------------------------------------------------------------+
int Calculate_Fibo()
  {
//----
   int li_Id1;
   datetime ldt_Fibo;
   int barid;
   int li_High, li_Low;
      
   ObjectDelete(gs_Fibo);   

   gd_High=iHigh(NULL,xi_Period,gi_BarOffset);
   gd_Low=iLow(NULL,xi_Period,gi_BarOffset);

   if (xb_MoveHiLo)
      {
      ArrayCopySeries(gdt_Times,MODE_TIME,Symbol(),0);   
      ldt_Fibo = iTime(NULL,xi_Period,gi_BarOffset-1);
      if (ldt_Fibo>gdt_Times[0])
         barid = 0;
      else
         {
         barid = ArrayBsearch(gdt_Times,ldt_Fibo,WHOLE_ARRAY,0,MODE_DESCEND);
         if (ldt_Fibo<gdt_Times[barid]) barid++;
         }
      // Check if there is a newer high / low 
      li_High = iHighest(NULL,0,MODE_HIGH,barid,1);
      li_Low = iLowest(NULL,0,MODE_LOW,barid,1);
      if (High[li_High] > gd_High) gd_High = High[li_High];
      if (Low[li_Low] < gd_Low) gd_Low = Low[li_Low];
      }

   ObjectCreate(gs_Fibo,OBJ_FIBO,0,iTime(NULL,xi_Period,gi_BarOffset-1),gd_High,iTime(NULL,xi_Period,gi_BarOffset-1),gd_Low);
   WindowRedraw();
   ObjectSet(gs_Fibo,OBJPROP_FIBOLEVELS,gi_FibLevels);
   ObjectSet(gs_Fibo,OBJPROP_LEVELCOLOR,xc_Color_Fib);
   for (li_Id1=0; li_Id1<=gi_FibLevels; li_Id1++)
      {
      ObjectSet(gs_Fibo,OBJPROP_FIRSTLEVEL+li_Id1,gd_FLvl[li_Id1]);
      if (xb_ShowPrice)
         ObjectSetFiboDescription(gs_Fibo,li_Id1,DoubleToStr(gd_FLvl[li_Id1],3)+" %$");
      else
         ObjectSetFiboDescription(gs_Fibo,li_Id1,DoubleToStr(gd_FLvl[li_Id1],3));
      }

   
//----
   return(0);
  }
//+------------------------------------------------------------------+