//+------------------------------------------------------------------+
//|                                                   Fibos v1.5.mq4 |
//|                                        Developed by Coders' Guru |
//|                                            http://www.xpworx.com |
//|                                              Modified by tigpips |
//+------------------------------------------------------------------+

#property copyright "Coders' Guru"
#property link      "http://www.xpworx.com"
string   ver  = "Last Modified: 2008.02.22 19:20";

#property indicator_chart_window
#property  indicator_buffers 8

extern bool  Higher_To_Lower = true;    //else Lower_To_Higher
extern bool  DrawVerticalLines = false;
extern color VerticalLinesColor = LightBlue;
extern color TrendUpColor = Green;
extern color TrendDownColor = Red;
int   StartBar = 0;
int   BarsBack = 24;

int sbar1;
color FiboColor;
double HiPrice, LoPrice, Range;
datetime StartTime;

double f_1[];
double f_2[];
double f_3[];
double f_4[];
double f_5[];
double f_6[];
double f_7[];
double f_8[];

void DeleteAllObjects()
{
   int objs = ObjectsTotal();
   string name;
   for(int cnt=ObjectsTotal()-1;cnt>=0;cnt--)
   {
      name=ObjectName(cnt);
      if (StringFind(name,"V_",0)>-1) ObjectDelete(name);
      if (StringFind(name,"H_",0)>-1) ObjectDelete(name);
      if (StringFind(name,"f_",0)>-1) ObjectDelete(name);
      if (StringFind(name,"fib",0)>-1) ObjectDelete(name);
      if (StringFind(name,"trend",0)>-1) ObjectDelete(name);
      WindowRedraw();
   }
}


void CalcFibo()
{
  DeleteAllObjects();

 
  int lowest_bar = iLowest(NULL,0,MODE_LOW,tf_num(),StartBar);
  int highest_bar = iHighest(NULL,0,MODE_HIGH,tf_num(),StartBar);
  
  double higher_point = 0;
  double lower_point = 0;
  higher_point=High[highest_bar];
  lower_point=Low[lowest_bar];
  
   if(highest_bar>lowest_bar)
   {
      WindowRedraw();
      FiboColor = TrendDownColor;
      Higher_To_Lower = true;
   }
   else{
      WindowRedraw();
      FiboColor = TrendUpColor;
      Higher_To_Lower = false;
   }
  
  if(DrawVerticalLines) DrawVerticalLine("V_UPPER",highest_bar,VerticalLinesColor);
  if(DrawVerticalLines) DrawVerticalLine("V_LOWER",lowest_bar,VerticalLinesColor);
  
  int i = 0;

  if(Higher_To_Lower)
  {
      for(i = 0; i < 500; i++)
      {
         f_1[i] = higher_point;
         f_2[i] = NormalizeDouble(lower_point+(higher_point-lower_point)*0.236,Digits);
         f_3[i] = NormalizeDouble(lower_point+(higher_point-lower_point)*0.382,Digits);
         f_4[i] = NormalizeDouble(lower_point+(higher_point-lower_point)*0.5,Digits);
         f_5[i] = NormalizeDouble(lower_point+(higher_point-lower_point)*0.618,Digits);
         f_6[i] = NormalizeDouble(lower_point+(higher_point-lower_point)*0.786,Digits);
         f_7[i] = NormalizeDouble(lower_point+(higher_point-lower_point)*1.618,Digits);
         f_8[i] = lower_point;
      }
      //ObjectCreate("fib",OBJ_FIBO,0,0,higher_point,0,lower_point);
      ObjectCreate("fib",OBJ_FIBO,0,Time[sbar1],higher_point,Time[StartBar],lower_point);
      ObjectSet("fib",OBJPROP_LEVELCOLOR,FiboColor); 
      ObjectSet("fib",OBJPROP_FIBOLEVELS,9);
      ObjectSet("fib",OBJPROP_FIRSTLEVEL+0,0.0); ObjectSetFiboDescription( "fib",0,"0.0"); 
      ObjectSet("fib",OBJPROP_FIRSTLEVEL+1,0.236); ObjectSetFiboDescription( "fib",1,"23.6"); 
      ObjectSet("fib",OBJPROP_FIRSTLEVEL+2,0.382); ObjectSetFiboDescription( "fib",2,"38.2"); 
      ObjectSet("fib",OBJPROP_FIRSTLEVEL+3,0.500); ObjectSetFiboDescription( "fib",3,"50.0"); 
      ObjectSet("fib",OBJPROP_FIRSTLEVEL+4,0.618); ObjectSetFiboDescription( "fib",4,"61.8"); 
      ObjectSet("fib",OBJPROP_FIRSTLEVEL+5,0.786); ObjectSetFiboDescription( "fib",5,"78.6"); 
      ObjectSet("fib",OBJPROP_FIRSTLEVEL+6,1.000); ObjectSetFiboDescription( "fib",6,"100.0"); 
      ObjectSet("fib",OBJPROP_FIRSTLEVEL+7,1.382); ObjectSetFiboDescription( "fib",7,"1.382");       
      ObjectSet("fib",OBJPROP_FIRSTLEVEL+8,1.618); ObjectSetFiboDescription( "fib",8,"1.618");        

      //ObjectCreate("trend",OBJ_TREND,0,Time[highest_bar],higher_point,Time[lowest_bar],lower_point);
      //ObjectSet("trend",OBJPROP_STYLE,STYLE_DOT);
      //ObjectSet("trend",OBJPROP_RAY,false);
  }
  else
  {
      for(i = 0; i < 500; i++)
      {
         f_8[i] = higher_point;
         f_7[i] = NormalizeDouble(lower_point+(higher_point-lower_point)*0.236,Digits);
         f_6[i] = NormalizeDouble(lower_point+(higher_point-lower_point)*0.382,Digits);
         f_5[i] = NormalizeDouble(lower_point+(higher_point-lower_point)*0.5,Digits);
         f_4[i] = NormalizeDouble(lower_point+(higher_point-lower_point)*0.618,Digits);
         f_3[i] = NormalizeDouble(lower_point+(higher_point-lower_point)*0.786,Digits);
         f_2[i] = NormalizeDouble(lower_point+(higher_point-lower_point)*1.618,Digits);
         f_1[i] = lower_point;
      }
      //DeleteAllObjects();
      ObjectCreate("fib",OBJ_FIBO,0,Time[sbar1],lower_point,Time[StartBar],higher_point);
      //ObjectCreate("fib",OBJ_FIBO,0,9,lower_point,0,higher_point);
      ObjectSet("fib",OBJPROP_LEVELCOLOR,FiboColor); 
      ObjectSet("fib",OBJPROP_FIBOLEVELS,9);
      ObjectSet("fib",OBJPROP_FIRSTLEVEL+0,0.0); ObjectSetFiboDescription( "fib",0,"0.0"); 
      ObjectSet("fib",OBJPROP_FIRSTLEVEL+1,0.236); ObjectSetFiboDescription( "fib",1,"23.6"); 
      ObjectSet("fib",OBJPROP_FIRSTLEVEL+2,0.382); ObjectSetFiboDescription( "fib",2,"38.2"); 
      ObjectSet("fib",OBJPROP_FIRSTLEVEL+3,0.500); ObjectSetFiboDescription( "fib",3,"50.0"); 
      ObjectSet("fib",OBJPROP_FIRSTLEVEL+4,0.618); ObjectSetFiboDescription( "fib",4,"61.8"); 
      ObjectSet("fib",OBJPROP_FIRSTLEVEL+5,0.786); ObjectSetFiboDescription( "fib",5,"78.6"); 
      ObjectSet("fib",OBJPROP_FIRSTLEVEL+6,1.000); ObjectSetFiboDescription( "fib",6,"100.0");       
      ObjectSet("fib",OBJPROP_FIRSTLEVEL+7,1.382); ObjectSetFiboDescription( "fib",7,"1.382");       
      ObjectSet("fib",OBJPROP_FIRSTLEVEL+8,1.618); ObjectSetFiboDescription( "fib",8,"1.618");  
      
      //ObjectCreate("trend",OBJ_TREND,0,Time[lowest_bar],lower_point,Time[highest_bar],higher_point);
      //ObjectSet("trend",OBJPROP_STYLE,STYLE_DOT);
      //ObjectSet("trend",OBJPROP_RAY,false);
  }
  
  
}

void DrawVerticalLine(string name , int bar , color clr)
{
   if(ObjectFind(name)==false)
   {
      ObjectCreate(name,OBJ_VLINE,0,Time[bar],0);
      ObjectSet(name,OBJPROP_COLOR,clr);
      ObjectSet(name,OBJPROP_WIDTH,2);
      WindowRedraw();
   }
   else
   {
      ObjectDelete(name);
      ObjectCreate(name,OBJ_VLINE,0,Time[bar],0);
      ObjectSet(name,OBJPROP_COLOR,clr);
      ObjectSet(name,OBJPROP_WIDTH,2);
      WindowRedraw();
   }

}



int deinit()
{
   DeleteAllObjects();
   return (0);
}

int init()
{
  DeleteAllObjects();
  SetIndexBuffer(0,f_1);
  SetIndexStyle(0,DRAW_NONE);
  SetIndexBuffer(1,f_2);
  SetIndexStyle(1,DRAW_NONE);
  SetIndexBuffer(2,f_3);
  SetIndexStyle(2,DRAW_NONE);
  SetIndexBuffer(3,f_4);
  SetIndexStyle(3,DRAW_NONE);
  SetIndexBuffer(4,f_5);
  SetIndexStyle(4,DRAW_NONE);
  SetIndexBuffer(5,f_6);
  SetIndexStyle(5,DRAW_NONE);
  SetIndexBuffer(6,f_7);
  SetIndexStyle(6,DRAW_NONE);
  SetIndexBuffer(7,f_8);
  SetIndexStyle(7,DRAW_NONE);
  return(0);
}


int start()
{
  int j = 0;
  for(j=0;j<tf_num();j++)
  {
      if(TimeHour(Time[j]) == 0 && TimeMinute(Time[j]) == 0)
      {
         StartBar = j;           
         sbar1 = j + tf_num();
      }
  }
  CalcFibo();
  return(0);
}

int tf_num()
{
   int result;
   if(Period() == PERIOD_M1) result = 1440;
   else if(Period() == PERIOD_M5) result = 288;
   else if(Period() == PERIOD_M15) result = 96;   
   else if(Period() == PERIOD_M30) result = 48;
   else if(Period() == PERIOD_H1) result = 24;
   else if(Period() == PERIOD_H4) result = 6;
   else if(Period() == PERIOD_D1) result = 1;
               
   return(result);
}

