//+------------------------------------------------------------------+ 
//|                                        TRO_DYNAMIC_FIBS_SR_L.mq4 | 
//|                                                                  | 
//| Based on ideas from TheRumpledOne & daniil                       | 
//| Any suggestions for improvements email dayraider@gmail.Com       | 
//|------------------------------------------------------------------+ 
#property copyright "" 
#property link      "" 

#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 myWingDing  = 119 ;
extern   int      max_bars=300; 
extern bool      bDrawFibs = false;  
extern bool      bDrawFibsLabels = true;
extern bool      bResetOnNewDay = false; 
extern int       iPeriods=5; 
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 DynR[]; 
double DynS[]; 
double fb1[]; 
double fb2[]; 
double fb3[]; 
double fb4[]; 
double fb5[]; 


double xfb1; 
double xfb2; 
double xfb3; 
double xfb4; 
double xfb5; 

string tRes0 = "Res_0" ;
string tSup0 = "Sup_0" ;
string tFib1 = "Fib1_0" ;
string tFib2 = "Fib2_0" ;
string tFib3 = "Fib3_0" ;
string tFib4 = "Fib4_0" ;
string tFib5 = "Fib5_0" ;

datetime StartTime;
int digits ;

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

digits = Digits ;

   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; 
    
   DynR[0] = Close[0]; 
   DynS[0] = Close[0]; 

   double nHH, nLL; 
   double range; 

   int processBars=MathMin(limit, max_bars); 
   for(int i=processBars;i>=0;i--){ 
      if ((Period() < PERIOD_D1) && //if timeframe below day 
         (TimeDay(Time[i])!=TimeDay(Time[i+1])) && // new day 
         (bResetOnNewDay)) { // reset if different day 
         DynR[i] = High[i]; 
         DynS[i]= Low[i]; 
      } //end if 
      else 
      { 
         nHH = High[Highest(NULL,0,MODE_HIGH,iPeriods,i)]; 
         nLL = Low[Lowest(NULL,0,MODE_LOW,iPeriods,i)]; 

         DynR[i] =   nHH; 
         DynS[i] =   nLL; 

         if ((DynR[i] != High[i]) && (DynR[i] < DynR[i+1]) &&  ( DynR[i+1] != 0 ))     
            DynR[i] = DynR[i+1]; 

         if (( DynS[i] != Low[i]) && (DynS[i] > DynS[i+1] ) && ( DynS[i+1] != 0 )) 
            DynS[i] = DynS[i+1]; 
      } 

      if (bDrawFibs){   // draw fibs              
            range = DynR[i] - DynS[i]; 
            fb1[i] = DynR[i] - range*iLevel1; 
            fb2[i] = DynR[i] - range*iLevel2; 
            fb3[i] = DynR[i] - range*iLevel3; 
            fb4[i] = DynR[i] - range*iLevel4; 
            fb5[i] = DynR[i] - range*iLevel5; 
            

            
       } //end if    
       
       
            range = DynR[0] - DynS[0]; 
            xfb1  = DynR[0] - range*iLevel1; 
            xfb2  = DynR[0] - range*iLevel2; 
            xfb3  = DynR[0] - range*iLevel3; 
            xfb4  = DynR[0] - range*iLevel4; 
            xfb5  = DynR[0] - range*iLevel5; 

 
if(bDrawFibsLabels ) { DoFibs() ;  }       
       
   } //end for 





//---- 
	if(ObjectFind(tRes0) == -1)
		ObjectCreate(tRes0,OBJ_FIBO,0,StartTime,DynR[0]+range,StartTime,DynR[0]);
	else
	{
		ObjectSet(tRes0,OBJPROP_TIME2, StartTime);
		ObjectSet(tRes0,OBJPROP_TIME1, StartTime);
		ObjectSet(tRes0,OBJPROP_PRICE1,DynR[0]+range);
		ObjectSet(tRes0,OBJPROP_PRICE2,DynR[0]);
	}
   ObjectSet(tRes0,OBJPROP_LEVELCOLOR,indicator_color1);
   ObjectSet(tRes0,OBJPROP_FIBOLEVELS,1);
   ObjectSet(tRes0,OBJPROP_FIRSTLEVEL+0,0.0);	
   ObjectSetFiboDescription(tRes0,0, DoubleToStr(DynR[0],digits)+ " RESISTANCE(" + iPeriods+ ")"); 
   ObjectSet(tRes0,OBJPROP_RAY,true);
   ObjectSet(tRes0,OBJPROP_BACK,true);


		if(ObjectFind(tSup0) == -1)
			ObjectCreate(tSup0,OBJ_FIBO,0,StartTime,DynS[0]-range,StartTime+PERIOD_H4*60,DynS[0]);
		else
		{
			ObjectSet(tSup0,OBJPROP_TIME2, StartTime);
			ObjectSet(tSup0,OBJPROP_TIME1, StartTime);
			ObjectSet(tSup0,OBJPROP_PRICE1,DynR[0]);
			ObjectSet(tSup0,OBJPROP_PRICE2,DynS[0]);
		}
   	ObjectSet(tSup0,OBJPROP_LEVELCOLOR,indicator_color2); 
   	ObjectSet(tSup0,OBJPROP_FIBOLEVELS,1);
   
   	ObjectSet(tSup0,OBJPROP_FIRSTLEVEL+0,0.000);	
   	ObjectSetFiboDescription(tSup0,0,DoubleToStr(DynS[0],digits)+ " Support(" + iPeriods+ ")"); 

   	ObjectSet(tSup0,OBJPROP_RAY,true);
   	ObjectSet(tSup0,OBJPROP_BACK,true);


         
//---- 
   return(0); 
  } 
//+------------------------------------------------------------------+ 


void DoFibs()
{
    
   
		if(ObjectFind(tFib1) == -1)
			ObjectCreate(tFib1,OBJ_FIBO,0,StartTime,DynR[0],StartTime+PERIOD_H4*60,DynS[0]);
		else
		{
			ObjectSet(tFib1,OBJPROP_TIME2, StartTime);
			ObjectSet(tFib1,OBJPROP_TIME1, StartTime);
			ObjectSet(tFib1,OBJPROP_PRICE1,DynR[0]);
			ObjectSet(tFib1,OBJPROP_PRICE2,DynS[0]);
		}
   	ObjectSet(tFib1,OBJPROP_LEVELCOLOR,indicator_color3); 
   	ObjectSet(tFib1,OBJPROP_FIBOLEVELS,1);
   
   	ObjectSet(tFib1,OBJPROP_FIRSTLEVEL+0,0.76);	
   	ObjectSetFiboDescription(tFib1,0,DoubleToStr(xfb1,digits)+ " "+ DoubleToStr(iLevel5,3)+" Fib(" + iPeriods+ ")"); 

   	ObjectSet(tFib1,OBJPROP_RAY,true);
   	ObjectSet(tFib1,OBJPROP_BACK,true);
  
   
		if(ObjectFind(tFib2) == -1)
			ObjectCreate(tFib2,OBJ_FIBO,0,StartTime,DynR[0],StartTime+PERIOD_H4*60,DynS[0]);
		else
		{
			ObjectSet(tFib2,OBJPROP_TIME2, StartTime);
			ObjectSet(tFib2,OBJPROP_TIME1, StartTime);
			ObjectSet(tFib2,OBJPROP_PRICE1,DynR[0]);
			ObjectSet(tFib2,OBJPROP_PRICE2,DynS[0]);
		}
   	ObjectSet(tFib2,OBJPROP_LEVELCOLOR,indicator_color4); 
   	ObjectSet(tFib2,OBJPROP_FIBOLEVELS,2);
   
   	ObjectSet(tFib2,OBJPROP_FIRSTLEVEL+0,0.618);	
   	ObjectSetFiboDescription(tFib2,0,DoubleToStr(xfb2,digits)+ " "+DoubleToStr(iLevel4,3)+" Fib(" + iPeriods+ ")"); 

   	ObjectSet(tFib2,OBJPROP_RAY,true);
   	ObjectSet(tFib2,OBJPROP_BACK,true);
 
		if(ObjectFind(tFib3) == -1)
			ObjectCreate(tFib3,OBJ_FIBO,0,StartTime,DynR[0],StartTime+PERIOD_H4*60,DynS[0]);
		else
		{
			ObjectSet(tFib3,OBJPROP_TIME2, StartTime);
			ObjectSet(tFib3,OBJPROP_TIME1, StartTime);
			ObjectSet(tFib3,OBJPROP_PRICE1,DynR[0]);
			ObjectSet(tFib3,OBJPROP_PRICE2,DynS[0]);
		}
   	ObjectSet(tFib3,OBJPROP_LEVELCOLOR,indicator_color5); 
   	ObjectSet(tFib3,OBJPROP_FIBOLEVELS,3);
   
   	ObjectSet(tFib3,OBJPROP_FIRSTLEVEL+0,0.50);	
   	ObjectSetFiboDescription(tFib3,0,DoubleToStr(xfb3,digits)+ " "+DoubleToStr(iLevel3,3)+" Fib(" + iPeriods+ ")"); 

   	ObjectSet(tFib3,OBJPROP_RAY,true);
   	ObjectSet(tFib3,OBJPROP_BACK,true);
        
		if(ObjectFind(tFib4) == -1)
			ObjectCreate(tFib4,OBJ_FIBO,0,StartTime,DynR[0],StartTime+PERIOD_H4*60,DynS[0]);
		else
		{
			ObjectSet(tFib4,OBJPROP_TIME2, StartTime);
			ObjectSet(tFib4,OBJPROP_TIME1, StartTime);
			ObjectSet(tFib4,OBJPROP_PRICE1,DynR[0]);
			ObjectSet(tFib4,OBJPROP_PRICE2,DynS[0]);
		}
   	ObjectSet(tFib4,OBJPROP_LEVELCOLOR,indicator_color6); 
   	ObjectSet(tFib4,OBJPROP_FIBOLEVELS,4);
   
   	ObjectSet(tFib4,OBJPROP_FIRSTLEVEL+0,0.382);	
   	ObjectSetFiboDescription(tFib4,0,DoubleToStr(xfb4,digits)+ " "+DoubleToStr(iLevel2,3)+" Fib(" + iPeriods+ ")"); 
   	
   	ObjectSet(tFib4,OBJPROP_RAY,true);
   	ObjectSet(tFib4,OBJPROP_BACK,true);
            
      
		if(ObjectFind(tFib5) == -1)
			ObjectCreate(tFib5,OBJ_FIBO,0,StartTime,DynR[0],StartTime+PERIOD_H4*60,DynS[0]);
		else
		{
			ObjectSet(tFib5,OBJPROP_TIME2, StartTime);
			ObjectSet(tFib5,OBJPROP_TIME1, StartTime);
			ObjectSet(tFib5,OBJPROP_PRICE1,DynR[0]);
			ObjectSet(tFib5,OBJPROP_PRICE2,DynS[0]);
		}
   	ObjectSet(tFib5,OBJPROP_LEVELCOLOR,indicator_color7); 
   	ObjectSet(tFib5,OBJPROP_FIBOLEVELS,5);
   
   	ObjectSet(tFib5,OBJPROP_FIRSTLEVEL+0,0.24);	
   	ObjectSetFiboDescription(tFib5,0,DoubleToStr(xfb5,digits)+ " "+DoubleToStr(iLevel1,3)+" Fib(" + iPeriods+ ")"); 

   	ObjectSet(tFib5,OBJPROP_RAY,true);
   	ObjectSet(tFib5,OBJPROP_BACK,true);
      
            
}