//+------------------------------------------------------------------+ 
//|                                          TRO_DYNAMIC_FIBS_SR.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 bool   Sound.Alert    = true ;
extern bool   Plot.Alert    = true ;
extern double    iAlertBuyLevel   = 0.118; 
extern double    iAlertShortLevel = 0.882; 
extern color     iColorBuy = Green ;
extern color     iColorShort = Crimson ;
extern int       iAlertLineStyle    = STYLE_DOT ;


extern int myWingDing  = 119 ;
extern   int      max_bars=300; 
extern bool      bDrawFibs = false; 
extern bool      bDrawFibsLabels = false;
extern int       myBoxWidth  = 3;
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" ;
string tAlertBuy ="Buy_sr"  ; 
string tAlertShort = "Short_sr"; 

datetime Trigger ;

double    iAlertBuy   ; 
double    iAlertShort; 
 
string symbol, tChartPeriod,  tShortName ;  
int    digits, period  ; 
//+------------------------------------------------------------------+ 
//| 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); 
   
   period = Period();  
   tChartPeriod =  TimeFrameToString(period) ;
   symbol       =  Symbol() ;   
   digits       =  Digits ;   
   Trigger      =  Time[0] ;  
//---- 
   return(0); 
  } 
//+------------------------------------------------------------------+ 
//| Custom indicator deinitialization function                       | 
//+------------------------------------------------------------------+ 
int deinit() 
  { 
//---- 
   ObjectDelete(tRes0); 
   ObjectDelete(tSup0); 
   ObjectDelete(tFib1);    
   ObjectDelete(tFib2);
   ObjectDelete(tFib3);
   ObjectDelete(tFib4);
   ObjectDelete(tFib5); 
   ObjectDelete(tAlertBuy); 
   ObjectDelete(tAlertShort); 
   
       
//---- 
   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; 


  
 

       
   } //end for 





iAlertBuy    = DynS[0] + ( range * iAlertBuyLevel ) ;
iAlertBuy    = NormalizeDouble( iAlertBuy , digits ) ; 
 
iAlertShort  = DynS[0] + ( range * iAlertShortLevel ) ; 
iAlertShort  = NormalizeDouble( iAlertShort , digits ) ; 



      if ( Trigger != Time[0] &&  Sound.Alert ) 
      {
        if( Ask >= iAlertBuy   && Low[0] <= iAlertBuy )  { Trigger = Time[0] ; Alert(symbol,"  ", tChartPeriod, DoubleToStr(iAlertBuy ,digits), " DYN SR Buy  " ); }
        if( High[0] >= iAlertShort && Bid <= iAlertShort) { Trigger = Time[0]  ; Alert(symbol,"  ", tChartPeriod,  DoubleToStr(iAlertShort ,digits), " DYN SR Short "); }     
      }


if(bDrawFibsLabels ) { DoFibs() ;  }       

if( Plot.Alert ) 
{


      ObjectDelete(tAlertBuy);
      ObjectCreate(tAlertBuy,OBJ_HLINE,0,0,0);
      ObjectSet(tAlertBuy,OBJPROP_COLOR,iColorBuy);
      ObjectSet(tAlertBuy,OBJPROP_STYLE,iAlertLineStyle);
 
      ObjectDelete(tAlertShort);
      ObjectCreate(tAlertShort,OBJ_HLINE,0,0,0);
      ObjectSet(tAlertShort,OBJPROP_COLOR,iColorShort);
      ObjectSet(tAlertShort,OBJPROP_STYLE,iAlertLineStyle);
  
 
      ObjectMove(tAlertBuy,0,Time[0],iAlertBuy);
 
      ObjectMove(tAlertShort,0,Time[0],iAlertShort);
            
}

  if (ObjectFind(tRes0) != 0)
      {
          ObjectCreate(tRes0,OBJ_ARROW,0,Time[0],DynR[0]);
          ObjectSet(tRes0,OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
          ObjectSet(tRes0,OBJPROP_COLOR,Red);  
          ObjectSet(tRes0,OBJPROP_WIDTH,myBoxWidth);             
      } 
      else
      {
         ObjectMove(tRes0,0,Time[0],DynR[0]);
      }

  if (ObjectFind(tSup0) != 0)
      {
          ObjectCreate(tSup0,OBJ_ARROW,0,Time[0],DynS[0]);
          ObjectSet(tSup0,OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
          ObjectSet(tSup0,OBJPROP_COLOR,Blue);  
          ObjectSet(tSup0,OBJPROP_WIDTH,myBoxWidth);           
      } 
      else
      {
         ObjectMove(tSup0,0,Time[0],DynS[0]);
      }
         
//---- 

   WindowRedraw() ;
   return(0); 
  } 
//+------------------------------------------------------------------+ 


void DoFibs()
{
string tFib1 = "Fib1_0" ;

   ObjectDelete(tFib1);    
   
   
   

  if (ObjectFind(tFib1) != 0)
      {
          ObjectCreate(tFib1,OBJ_ARROW,0,Time[0],xfb1);
          ObjectSet(tFib1,OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
          ObjectSet(tFib1,OBJPROP_COLOR,DarkSeaGreen); 
          ObjectSet(tFib1,OBJPROP_WIDTH,myBoxWidth);             
      } 
      else
      {
         ObjectMove(tFib1,0,Time[0],xfb1);
      }
      
  if (ObjectFind(tFib2) != 0)
      {
          ObjectCreate(tFib2,OBJ_ARROW,0,Time[0],xfb2);
          ObjectSet(tFib2,OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
          ObjectSet(tFib2,OBJPROP_COLOR,Khaki);  
          ObjectSet(tFib2,OBJPROP_WIDTH,myBoxWidth);            
      } 
      else
      {
         ObjectMove(tFib2,0,Time[0],xfb2);
      }

  if (ObjectFind(tFib3) != 0)
      {
          ObjectCreate(tFib3,OBJ_ARROW,0,Time[0],xfb3);
          ObjectSet(tFib3,OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
          ObjectSet(tFib3,OBJPROP_COLOR,Gray);  
          ObjectSet(tFib3,OBJPROP_WIDTH,myBoxWidth);            
      } 
      else
      {
         ObjectMove(tFib3,0,Time[0],xfb3);
      }      
      
  if (ObjectFind(tFib4) != 0)
      {
          ObjectCreate(tFib4,OBJ_ARROW,0,Time[0],xfb4);
          ObjectSet(tFib4,OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
          ObjectSet(tFib4,OBJPROP_COLOR,Khaki);  
          ObjectSet(tFib4,OBJPROP_WIDTH,myBoxWidth);            
      } 
      else
      {
         ObjectMove(tFib4,0,Time[0],xfb4);
      }            
      
  if (ObjectFind(tFib5) != 0)
      {
          ObjectCreate(tFib5,OBJ_ARROW,0,Time[0],xfb5);
          ObjectSet(tFib5,OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
          ObjectSet(tFib5,OBJPROP_COLOR,DarkSeaGreen);  
          ObjectSet(tFib5,OBJPROP_WIDTH,myBoxWidth);  
      } 
      else
      {
         ObjectMove(tFib5,0,Time[0],xfb5);
      }
      
            
}
//+--------- TRO MODIFICATION ---------------------------------------+  

string TimeFrameToString(int tf)
{
   string tfs;
   switch(tf) {
      case PERIOD_M1:  tfs="M1"  ; break;
      case PERIOD_M5:  tfs="M5"  ; break;
      case PERIOD_M15: tfs="M15" ; break;
      case PERIOD_M30: tfs="M30" ; break;
      case PERIOD_H1:  tfs="H1"  ; break;
      case PERIOD_H4:  tfs="H4"  ; break;
      case PERIOD_D1:  tfs="D1"  ; break;
      case PERIOD_W1:  tfs="W1"  ; break;
      case PERIOD_MN1: tfs="MN";
   }
   return(tfs);
}


//+--------- TRO MODIFICATION ---------------------------------------+  


/*
Alert(  DoubleToStr(iAlertShort ,digits), " DYN SR Short ");

Comment(


"High[0]= ",    DoubleToStr(High[0] ,digits) , "\n",
"iAlertShort= ",    DoubleToStr(iAlertShort ,digits) , "\n",
"Bid= ",    DoubleToStr(Bid ,digits) , "\n",
"Low[0]= ",    DoubleToStr(Low[0] ,digits) , "\n",
" ",    "\n",

"High[0]= ",    DoubleToStr(High[0] ,digits) , "\n",
"Ask= ",    DoubleToStr(Ask ,digits) , "\n",
"iAlertBuy= ",    DoubleToStr(iAlertBuy ,digits) , "\n",
"Low[0]= ",    DoubleToStr(Low[0] ,digits) , "\n",


"") ; 

*/