//+------------------------------------------------------------------+
//|                                       _TRO_STATIC_FIBS_B         |
//|   Copyright � 2008, Avery T. Horton, Jr. aka TheRumpledOne       |
//|                                                                  |
//|   PO BOX 43575, TUCSON, AZ 85733                                 |
//|                                                                  |
//|   GIFTS AND DONATIONS ACCEPTED                                   | 
//|                                                                  |
//|   therumpledone@gmail.com                                        |  
//+------------------------------------------------------------------+ 

#property  copyright "Copyright � 2008, Avery T. Horton, Jr. aka TRO" 
#property  link      "http://www.therumpledone.com/" 

#property indicator_chart_window 
#property indicator_buffers 7 
#property indicator_color1 Red 
#property indicator_color2 Blue 
#property indicator_color3 DarkSeaGreen 
#property indicator_color4 Khaki 
#property indicator_color5 Gray 
#property indicator_color6 Khaki 
#property indicator_color7 DarkSeaGreen 

//---- input parameters 
extern   int      max_bars=300; 
 
 

extern bool      show.comment = true; 

extern   int     iBarsAgo = 52 ;

extern double    iLevel1=0.24; 
extern double    iLevel2=0.382; 
extern double    iLevel3=0.5; 
extern double    iLevel4=0.618; 
extern double    iLevel5=0.76; 

//---- buffers 
double oTop[]; 
double oBot[]; 
double fb1[]; 
double fb2[]; 
double fb3[]; 
double fb4[]; 
double fb5[]; 

string  mySymbol  ;
int     myChartPeriod ;


string tRes0 = "bRes_0" ;
string tSup0 = "bSup_0" ;
string tFib1 = "bFib1_0" ;
string tFib2 = "bFib2_0" ;
string tFib3 = "bFib3_0" ;
string tFib4 = "bFib4_0" ;
string tFib5 = "bFib5_0" ;

//+------------------------------------------------------------------+ 
//| Custom indicator initialization function                         | 
//+------------------------------------------------------------------+ 
int init() 
  { 
  
  
//---- indicators 
   SetIndexStyle(0,DRAW_ARROW); 
   SetIndexArrow(0,119); 
   SetIndexBuffer(0,oTop); 
   SetIndexEmptyValue(0,0.0); 
   SetIndexStyle(1,DRAW_ARROW); 
   SetIndexArrow(1,119); 
   SetIndexBuffer(1,oBot); 
   SetIndexEmptyValue(1,0.0); 
   SetIndexStyle(2,DRAW_ARROW); 
   SetIndexArrow(2,119); 
   SetIndexBuffer(2,fb1); 
   SetIndexEmptyValue(2,0.0); 
   SetIndexStyle(3,DRAW_ARROW); 
   SetIndexArrow(3,119); 
   SetIndexBuffer(3,fb2); 
   SetIndexEmptyValue(3,0.0); 
   SetIndexStyle(4,DRAW_ARROW); 
   SetIndexArrow(4,119); 
   SetIndexBuffer(4,fb3); 
   SetIndexEmptyValue(4,0.0); 
   SetIndexStyle(5,DRAW_ARROW); 
   SetIndexArrow(5,119); 
   SetIndexBuffer(5,fb4); 
   SetIndexEmptyValue(5,0.0); 
   SetIndexStyle(6,DRAW_ARROW); 
   SetIndexArrow(6,119); 
   SetIndexBuffer(6,fb5); 
   SetIndexEmptyValue(6,0.0); 
 

 mySymbol      = Symbol();
 myChartPeriod = Period() ;

   return(0); 
  } 
//+------------------------------------------------------------------+ 
//| Custom indicator deinitialization function                       | 
//+------------------------------------------------------------------+ 
int deinit() 
  { 
   ObjectDelete(tRes0); 
   ObjectDelete(tSup0); 
   ObjectDelete(tFib1);    
   ObjectDelete(tFib2);
   ObjectDelete(tFib3);
   ObjectDelete(tFib4);
   ObjectDelete(tFib5); 
   return(0); 
  } 
//+------------------------------------------------------------------+ 
//| Custom indicator iteration function                              | 
//+------------------------------------------------------------------+ 

int start() 
  { 
   int    counted_bars=IndicatorCounted(); 
    if(counted_bars < 0) 
        return(-1); 

   int limit=Bars-counted_bars;     

   double range; 


   int processBars=MathMin(limit, max_bars); 
   for(int i=processBars;i>=0;i--){ 


      oTop[i]  = High[Highest(mySymbol,myChartPeriod,MODE_HIGH,iBarsAgo,i)]; 
      oBot[i]  = Low[Lowest(mySymbol,myChartPeriod,MODE_LOW,iBarsAgo,i)]; 
             
            range  = oTop[i] - oBot[i]; 
            fb1[i] = oTop[i] - range*iLevel5; 
            fb2[i] = oTop[i] - range*iLevel4; 
            fb3[i] = oTop[i] - range*iLevel3; 
            fb4[i] = oTop[i] - range*iLevel2; 
            fb5[i] = oTop[i] - range*iLevel1; 
    
   } //end for 

if(show.comment) 
{ Comment( "fib bars = ", iBarsAgo, "\n",
           "fib range = ", DoubleToStr(range,Digits), "\n"       ) ;}  
           
           
           

 
 DoFibs() ;      
       
  

  if (ObjectFind(tRes0) != 0)
      {
          ObjectCreate(tRes0,OBJ_ARROW,0,Time[0],oTop[0]);
          ObjectSet(tRes0,OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
          ObjectSet(tRes0,OBJPROP_COLOR,Red);  
      } 
      else
      {
         ObjectMove(tRes0,0,Time[0],oTop[0]);
      }

  if (ObjectFind(tSup0) != 0)
      {
          ObjectCreate(tSup0,OBJ_ARROW,0,Time[0],oBot[0]);
          ObjectSet(tSup0,OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
          ObjectSet(tSup0,OBJPROP_COLOR,Blue);  
      } 
      else
      {
         ObjectMove(tSup0,0,Time[0],oBot[0]);
      }           
           
   return(0); 
  } 
//+------------------------------------------------------------------+ 


void DoFibs()
{

   ObjectDelete(tFib1);    
   
  if (ObjectFind(tFib1) != 0)
      {
          ObjectCreate(tFib1,OBJ_ARROW,0,Time[0],fb1[0]);
          ObjectSet(tFib1,OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
          ObjectSet(tFib1,OBJPROP_COLOR,DarkSeaGreen);  
      } 
      else
      {
         ObjectMove(tFib1,0,Time[0],fb1[0]);
      }
      
  if (ObjectFind(tFib2) != 0)
      {
          ObjectCreate(tFib2,OBJ_ARROW,0,Time[0],fb2[0]);
          ObjectSet(tFib2,OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
          ObjectSet(tFib2,OBJPROP_COLOR,Khaki);  
      } 
      else
      {
         ObjectMove(tFib2,0,Time[0],fb2[0]);
      }

  if (ObjectFind(tFib3) != 0)
      {
          ObjectCreate(tFib3,OBJ_ARROW,0,Time[0],fb3[0]);
          ObjectSet(tFib3,OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
          ObjectSet(tFib3,OBJPROP_COLOR,Gray);  
      } 
      else
      {
         ObjectMove(tFib3,0,Time[0],fb3[0]);
      }      
      
  if (ObjectFind(tFib4) != 0)
      {
          ObjectCreate(tFib4,OBJ_ARROW,0,Time[0],fb4[0]);
          ObjectSet(tFib4,OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
          ObjectSet(tFib4,OBJPROP_COLOR,Khaki);  
      } 
      else
      {
         ObjectMove(tFib4,0,Time[0],fb4[0]);
      }            
      
  if (ObjectFind(tFib5) != 0)
      {
          ObjectCreate(tFib5,OBJ_ARROW,0,Time[0],fb5[0]);
          ObjectSet(tFib5,OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
          ObjectSet(tFib5,OBJPROP_COLOR,DarkSeaGreen);  
      } 
      else
      {
         ObjectMove(tFib5,0,Time[0],fb5[0]);
      }
      
            
}