//+------------------------------------------------------------------+ 
//|                                          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   Show.Gauge = true ;
extern int    myChartX   = 10 ;
extern int    myChartY   = 200 ;  
  
extern int myCorner   = 1 ;
extern string myFont  = "Courier New" ;
extern int myFontSize = 12 ;


extern bool   Show.PriceBox  = true ;
extern int       myBoxWidth  = 3;
extern int       myLineWidth = 0;

extern bool   Sound.Alert    = false ;
extern bool   Plot.Alert    = false ;

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    = 1000; 

extern bool      bDrawFibs       = false; 
extern bool      bDrawFibsLabels = false;

extern bool      bResetOnNewDay = false; 

extern int       iPeriods  = 5; 
extern int       iRangeMin = 20; 

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; 

extern color colorHead = Orange ;
extern color colorRes  = Red ;
extern color colorSup  = Blue ;
extern color colorUp   = Lime ;
extern color colorEq   = Yellow ;
extern color colorDn   = Red ;

extern color Fibcolor1 = DarkSeaGreen ;
extern color Fibcolor2 = Khaki ;
extern color Fibcolor3 = Gray ;
extern color Fibcolor4 = Khaki ;
extern color Fibcolor5 = DarkSeaGreen ; 

//---- 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, shortName, ShortName ;  

int    digits2, digits, period, win ; 

string lbl[15], lbl2[15];

string TAG = "dfsr" ;
string   ObjG001,ObjG002,ObjG003,ObjG004,ObjG005,ObjG006,ObjG007,ObjG008,ObjG009 ;  

int FillAmt = 11 , x001, x002, x003, x004, x005, x006, x007 ;  
int n, j ;

double close, diff, value, point, xMin ;


color pColor = Blue ; 

//+------------------------------------------------------------------+ 
int init() 
  { 
//---- indicators 
   SetIndexStyle(0,DRAW_ARROW,0,myLineWidth,colorRes); 
   SetIndexArrow(0,myWingDing); 
   SetIndexBuffer(0,DynR); 
   SetIndexEmptyValue(0,0.0); 
   SetIndexStyle(1,DRAW_ARROW,0,myLineWidth,colorSup);  
   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] ;  
   point        =  Point ;
     
   if(digits == 5 || digits == 3) { digits = digits - 1 ; point = point * 10 ; }   

   ShortName    =  TAG+symbol+iPeriods ;
   
     
tRes0 =  TAG + "Res_0"  + iPeriods ;
tSup0 =  TAG + "Sup_0"  + iPeriods ;
tFib1 =  TAG + "Fib1_0"  + iPeriods ;
tFib2 =  TAG + "Fib2_0"  + iPeriods ;
tFib3 =  TAG + "Fib3_0"  + iPeriods ;
tFib4 =  TAG + "Fib4_0"  + iPeriods ;
tFib5 =  TAG + "Fib5_0"  + iPeriods ;
tAlertBuy =   TAG + "Buy_sr"   + iPeriods ; 
tAlertShort =   TAG + "Short_sr" + iPeriods ;    
   
if( myCorner == 0 || myCorner == 2 )
{  
   x001 = 0 ;
   x002 = 0 ;
   x003 = 90 ; 
   x004 = 150;
   x005 = 100 ;
   x006 = 90 ;  
   x007 = 90 ;
   FillAmt = 20 ;     
}
else
{
   x001 = 0 ;
   x002 = 10 ; //  
   x003 = 60 ;  // val  
   x004 = 0 ; //  dif
   x005 = 10 ;
   x006 = 10 ;  
   x007 = 10 ;  
   FillAmt = 23 ; 
}


   
   deinit() ;
   
   return(0);
  }


//+------------------------------------------------------------------+

void ObDeleteObjectsByPrefix(string Prefix)
  {
   int L = StringLen(Prefix);
   int i = 0; 
   while(i < ObjectsTotal())
     {
       string ObjName = ObjectName(i);
       if(StringSubstr(ObjName, 0, L) != Prefix) 
         { 
           i++; 
           continue;
         }
       ObjectDelete(ObjName);
     }
  }
    
//+------------------------------------------------------------------+
int deinit()
  {
        
   ObDeleteObjectsByPrefix(TAG);  
      TRO() ;
      
   return(0);
  }
  
//+------------------------------------------------------------------+ 
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(Show.PriceBox) {
  if (ObjectFind(tRes0) != 0)
      {
          ObjectCreate(tRes0,OBJ_ARROW,0,Time[0],DynR[0]);
          ObjectSet(tRes0,OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
          ObjectSet(tRes0,OBJPROP_COLOR,colorRes);  
          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,colorSup);  
          ObjectSet(tSup0,OBJPROP_WIDTH,myBoxWidth);           
      } 
      else
      {
         ObjectMove(tSup0,0,Time[0],DynS[0]);
      }
 }    
 
      if(Show.Gauge) { DoShowGauge(); }
        
//---- 

   WindowRedraw() ;
   return(0); 
  } 
//+------------------------------------------------------------------+ 


void DoFibs()
{
 

   ObjectDelete(tFib1);    
   
   
   
 if(Show.PriceBox) {
  if (ObjectFind(tFib1) != 0)
      {
          ObjectCreate(tFib1,OBJ_ARROW,0,Time[0],xfb1);
          ObjectSet(tFib1,OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
          ObjectSet(tFib1,OBJPROP_COLOR,Fibcolor1); 
          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,Fibcolor2);  
          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,Fibcolor3);  
          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,Fibcolor4);  
          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,Fibcolor5);  
          ObjectSet(tFib5,OBJPROP_WIDTH,myBoxWidth);  
      } 
      else
      {
         ObjectMove(tFib5,0,Time[0],xfb5);
      }
 }   
            
}

//+------------------------------------------------------------------+


void DoShowGauge()
{
n       = 10;
j       = 0 ;

close   = iClose(symbol,period,0) ;


lbl[j]  = ShortName+j;
lbl2[j] = fFill(tChartPeriod + " DFSR(" + iPeriods + ")"   , FillAmt) ;
DoShow(j, colorHead);



j       = j + 1;
lbl[j]  = ShortName+j;
value   = DynR[0] ;
diff    = (close - value) / point ;
DoColor(diff, 0) ;
lbl2[j] = fFill("Dyn Res "  , FillAmt) ;
DoShow(j, pColor);



j       = j + 1;
lbl[j]  = ShortName+j;
value   = DynS[0] ;
diff    = (close - value) / point ;
DoColor(diff, 0) ;
lbl2[j] = fFill("Dyn Sup "  , FillAmt) ;
DoShow(j, pColor);



j       = j + 1;
lbl[j]  = ShortName+j;
value   = (DynR[0] - DynS[0]) / point  ;
diff    = 100 * ( close - DynS[0]) /  (DynR[0] - DynS[0]) ;
DoColor(value, iRangeMin) ;
lbl2[j] = fFill("Range| %"  , FillAmt) ;
DoShow(j, pColor);




}


//+------------------------------------------------------------------+

void DoShow( int u, color dsColor )
{

ObjectCreate(lbl[u],23,0,Time[0],PRICE_CLOSE);
ObjectSet(lbl[u], OBJPROP_CORNER, myCorner );
ObjectSet(lbl[u],OBJPROP_XDISTANCE, x002+myChartX);
ObjectSet(lbl[u],OBJPROP_YDISTANCE,n+myChartY);
ObjectSetText(lbl[u],lbl2[u],myFontSize,myFont,dsColor);

if(u > 0)
{
string Obj001 = lbl[u] + "val" ;
string sVal = fFill(DoubleToStr(value,digits), 7)    ;
ObjectCreate(Obj001,23,0,Time[0],PRICE_CLOSE);
ObjectSet(Obj001, OBJPROP_CORNER, myCorner );
ObjectSet(Obj001,OBJPROP_XDISTANCE, x003+myChartX);
ObjectSet(Obj001,OBJPROP_YDISTANCE,n+myChartY);
ObjectSetText(Obj001,sVal,myFontSize,myFont,dsColor);


string Obj002 = lbl[u] + "dif" ;
string sDif =  rtadjust( DoubleToStr(diff,0) ) ;
ObjectCreate(Obj002,23,0,Time[0],PRICE_CLOSE);
ObjectSet(Obj002, OBJPROP_CORNER, myCorner );
ObjectSet(Obj002,OBJPROP_XDISTANCE, x004+myChartX);
ObjectSet(Obj002,OBJPROP_YDISTANCE,n+myChartY);
ObjectSetText(Obj002,sDif,myFontSize,myFont,dsColor);
}

n = n+20;

return(0);

}

//+------------------------------------------------------------------+
  
void DoColor( double c1, double c2 )
{

if(c1 > c2 ) { pColor = colorUp ; } else {
if(c1 < c2 ) { pColor = colorDn ; } else { pColor = colorEq ; } } 

}  
   
//+------------------------------------------------------------------+

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);
}

 


//+------------------------------------------------------------------+

string fFill(string filled, int f ) 
{
   string FILLED ;
   
   FILLED = StringSubstr(filled + "                                         ",0,f) ;
   
return(FILLED);
}



//+------------------------------------------------------------------+
string rtadjust( string rString )
{
 
 int sl = StringLen(rString) ;
 
 while(true)
 {
 
   if( sl == 5) { break ; } 
   if( sl == 4) { rString = " " + rString ; break ; }    
   if( sl == 3) { rString = "  " + rString ; break ; } 
   if( sl == 2) { rString = "   " + rString ; break ; } 
   if( sl == 1) { rString = "    " + rString ; break ; } 
    
   break ;
 }

return(rString) ;
}       


//+------------------------------------------------------------------+  
void TRO()
{   
   
   string tObjName03    = "TROTAG"  ;  
   ObjectCreate(tObjName03, OBJ_LABEL, 0, 0, 0);//HiLow LABEL
   ObjectSetText(tObjName03, CharToStr(78) , 12 ,  "Wingdings",  DimGray );
   ObjectSet(tObjName03, OBJPROP_CORNER, 3);
   ObjectSet(tObjName03, OBJPROP_XDISTANCE, 5 );
   ObjectSet(tObjName03, OBJPROP_YDISTANCE, 5 );  
}
//+--------- 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",


"") ; 

*/