//+------------------------------------------------------------------+
//|                                      Auto Fibo Retracement-V2.mq4|
//|   This tool draws a fibonacci retracement with 0 to 100%         |
//|   automatically on a chart, in the direction of the trend.       |
//|   It can also show the unretraced zone                           |
//|   More free tools @ tradertools-fx.com                           |
//|                                                       Paul Nordin|
//|                                    http://www.tradertools-fx.com |
//+------------------------------------------------------------------+
#property copyright "� 2010 TRADERTOOLS-FX.COM"
#property link      "http://www.tradertools-fx.com"

#property indicator_chart_window
#property indicator_buffers    0

//User Parameters
extern color fiboColor = Yellow;
extern double fiboWidth = 1;
extern  double fiboStyle = 0;
extern color unretracedZoneColor = Green;
extern bool showUnretracedZone = true;

string headerString = "AutoFibo_";

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init() {
   IndicatorBuffers(0); 
   return(0);
}

int deinit() {
   deleteObjects();
   Comment( "" ); 
   return(0);
}

int start() {
   Comment( "TRADERTOOLS-FX.COM" );
   deleteObjects();
   createFibo();
   return(0);
}

void deleteObjects() {
   for ( int i = ObjectsTotal() - 1;  i >= 0;  i-- ) {
      string name = ObjectName( i );
      if ( StringSubstr( name, 0, StringLen( headerString ) ) == headerString )
         ObjectDelete( name );
   }
}

void createFibo() {
   int bar = WindowFirstVisibleBar();
   
   int shiftLowest  = iLowest( NULL, 0, MODE_LOW, bar - 1, 1 );
   int shiftHighest = iHighest( NULL, 0, MODE_HIGH, bar - 1, 1 );
   
   bool   isDownTrend = shiftHighest > shiftLowest;
   string fiboObjectId1 = headerString + "1";
   string fiboObjectHigh = headerString + "High";
   string fiboObjectLow = headerString + "Low";
   string unretracedZoneObject = headerString + "UnretracedZone";
   double retracementExtent;
   int shiftMostRetraced;
  
   if ( isDownTrend == true ) {     
      ObjectCreate( fiboObjectId1, OBJ_FIBO,0, Time[shiftHighest], High[shiftHighest], Time[shiftLowest], Low[shiftLowest] );    
      ObjectSet( fiboObjectId1, OBJPROP_LEVELWIDTH, fiboWidth );
      ObjectSet( fiboObjectId1, OBJPROP_LEVELSTYLE, fiboStyle );
      if ( showUnretracedZone == true ) {
         if ( shiftLowest > 0 ) {
            shiftMostRetraced = iHighest( NULL, 0, MODE_HIGH, shiftLowest - 1, 0 );
            ObjectCreate( unretracedZoneObject, OBJ_RECTANGLE, 0, Time[shiftMostRetraced], High[shiftHighest], Time[0], High[shiftMostRetraced] );      
            ObjectSet( unretracedZoneObject, OBJPROP_COLOR, unretracedZoneColor );     
         } 
      }  
   }
   
   else {
     ObjectCreate( fiboObjectId1, OBJ_FIBO, 0, Time[shiftLowest], Low[shiftLowest], Time[shiftHighest], High[shiftHighest] );   
     ObjectSet( fiboObjectId1, OBJPROP_LEVELWIDTH, fiboWidth );
     ObjectSet( fiboObjectId1, OBJPROP_LEVELSTYLE, fiboStyle );
        if( showUnretracedZone == true ) {
           if ( shiftHighest > 0 ) {
               shiftMostRetraced = iLowest( NULL, 0, MODE_LOW, shiftHighest - 1, 0 );
               ObjectCreate( unretracedZoneObject, OBJ_RECTANGLE, 0, Time[shiftMostRetraced], Low[shiftLowest], Time[0], Low[shiftMostRetraced] );      
               ObjectSet( unretracedZoneObject, OBJPROP_COLOR, unretracedZoneColor );
           }
        }
    }
  
   ObjectSet( fiboObjectId1, OBJPROP_LEVELCOLOR, fiboColor );
   ObjectSet( fiboObjectId1, OBJPROP_LEVELSTYLE, fiboStyle );
   ObjectSet( fiboObjectId1, OBJPROP_LEVELWIDTH, fiboWidth );
   ObjectSet( fiboObjectId1, OBJPROP_FIBOLEVELS,7 );
   ObjectSet( fiboObjectId1, OBJPROP_FIRSTLEVEL + 1, 0.00 );
   ObjectSetFiboDescription( fiboObjectId1, 1, "0.00- %$" );
   ObjectSet( fiboObjectId1, OBJPROP_FIRSTLEVEL + 2, 0.236 );
   ObjectSetFiboDescription( fiboObjectId1, 2, "23.6- %$" );
   ObjectSet( fiboObjectId1, OBJPROP_FIRSTLEVEL + 3, 0.382 );
   ObjectSetFiboDescription( fiboObjectId1, 3, "38.2- %$" );
   ObjectSet( fiboObjectId1, OBJPROP_FIRSTLEVEL + 4, 0.50 );
   ObjectSetFiboDescription( fiboObjectId1, 4, "50.0- %$" );
   ObjectSet( fiboObjectId1, OBJPROP_FIRSTLEVEL + 5, 0.618 );
   ObjectSetFiboDescription( fiboObjectId1, 5, "61.8 %$" );
   ObjectSet( fiboObjectId1, OBJPROP_FIRSTLEVEL + 6, 0.786 );
   ObjectSetFiboDescription( fiboObjectId1, 6, "78.6- %$" );
   ObjectSet( fiboObjectId1, OBJPROP_FIRSTLEVEL + 0, 1.00 );
   ObjectSetFiboDescription( fiboObjectId1, 0, "100- %$" );  
}