Fibo_Retracement_Dynamic





//+------------------------------------------------------------------+
//|                                     Fibo_Retracement_Dynamic.mq4 |
//+------------------------------------------------------------------+

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Aqua
#property indicator_color2 Blue
#property indicator_color3 Red

//---- indicator parameters
extern int ExtDepth=34;
extern color Line_color = Red;
extern color Target_color = Aqua;
extern bool Draw_ZZ=false;
extern bool ShowTarget=true;

int      UpArrowCode             = 241;
int      DownArrowCode           = 242;
color    UpArrowColor            = Blue;
color    DownArrowColor          = Red;
int      UpArrowSize             = 3;
int      DownArrowSize           = 3;

//---- indicator buffers
double zzL[];
double zzH[];
double zz[];
double target1=0,target2=0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
 //  IndicatorBuffers(3);
//---- drawing settings
   SetIndexStyle(2,DRAW_ARROW);
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexStyle(0,DRAW_SECTION);
   SetIndexArrow(2,159);
   SetIndexArrow(1,159);
//---- indicator buffers mapping
if(Draw_ZZ) SetIndexBuffer(0,zz);
   SetIndexBuffer(1,zzH);
   SetIndexBuffer(2,zzL);
   SetIndexEmptyValue(0,0.0);
   SetIndexEmptyValue(1,0.0);
   SetIndexEmptyValue(2,0.0);

   return(0);
  }
  int deinit()
  {
//----
   ObjectDelete("xpMA");
   ObjectDelete("Target1=");
   ObjectDelete("Target2=");
   ObjectDelete("TargetUp");
   ObjectDelete("TargetDown");
   ObjectDelete("TP1");
   ObjectDelete("TP2");
   ObjectDelete("R1");
   ObjectDelete("R2");
   ObjectDelete("R3");
   ObjectDelete("R4");
   ObjectDelete("LH");
   ObjectDelete("LL");
   ObjectDelete("Low");
   ObjectDelete("High");
   ObjectDelete("0.618");
   ObjectDelete("0.5");
   ObjectDelete("0.382");
   ObjectDelete("0.236");
   Comment(" ");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   int    i,shift,pos,lasthighpos,lastlowpos,curhighpos,curlowpos;
   double curlow,curhigh,lasthigh,lastlow;
   double min, max;
   ArrayInitialize(zz,0.0);
   ArrayInitialize(zzL,0.0);
   ArrayInitialize(zzH,0.0);
   
   lasthighpos=Bars; lastlowpos=Bars;
   lastlow=Low[Bars];lasthigh=High[Bars];
 
  for(shift=Bars-ExtDepth; shift>=0; shift--)
    {
      curlowpos=Lowest(NULL,0,MODE_LOW,ExtDepth,shift);
      curlow=Low[curlowpos];
      curhighpos=Highest(NULL,0,MODE_HIGH,ExtDepth,shift);
      curhigh=High[curhighpos];
      //------------------------------------------------
      if( curlow>=lastlow ) { lastlow=curlow; }
      else
         { 
            if( lasthighpos>curlowpos  ) 
            { 
            zzL[curlowpos]=curlow;
              min=100000; pos=lasthighpos;
               for(i=lasthighpos; i>=curlowpos; i--)
                  { 
                    if (zzL[i]==0.0) continue;
                    if (zzL[i]<min) { min=zzL[i]; pos=i; }
                    zz[i]=0.0;
                  } 
               zz[pos]=min;
            } 
          lastlowpos=curlowpos;
          lastlow=curlow; 
         }
      //--- high
      if( curhigh<=lasthigh )  { lasthigh=curhigh;}
      else
         {  
            if( lastlowpos>curhighpos ) 
            {  
            zzH[curhighpos]=curhigh;
               max=-100000; pos=lastlowpos;
               for(i=lastlowpos; i>=curhighpos; i--)
                  { 
                    if (zzH[i]==0.0) continue;
                    if (zzH[i]>max) { max=zzH[i]; pos=i; }
                    zz[i]=0.0;
                  } 
               zz[pos]=max;
            }  
         lasthighpos=curhighpos;
         lasthigh=curhigh;    
         }       
double p, r4,r3,r2,r1;    
double R= lasthigh - lastlow;//range

if(lastlowpos<lasthighpos) {
p = curlow;
r4 = p + (R * 0.618);
r3 = p + (R * 0.5);
r2 = p + (R * 0.382);
r1 = p + (R * 0.236);
if(Close[0]>r4 )target1= curhigh + (R * 0.618);
if(Close[0]>target1)target1= 0; 
}

if(lasthighpos<lastlowpos) {
 p = curhigh;
r4 = p - (R * 0.618);
r3 = p - (R * 0.5);
r2 = p - (R * 0.382);
r1 = p - (R * 0.236);
if(Close[0]<r4 )target2= curlow - (R * 0.618);
if(Close[0]<target2 )target2= 0; 
}

drawLine(curhigh,"LH", Line_color,1);
drawLabel("High",curhigh,Line_color,13);
drawLine(curlow,"LL", Line_color,1);
drawLabel("Low",curlow,Line_color,13);
   
drawLine(r4,"R4", Yellow,2);
drawLabel("0.618",r4,Yellow,13);
//drawLine(r3,"R3", Yellow,2);
//drawLabel("0.5",r3,Yellow,13);
drawLine(r2,"R2", Yellow,2);
drawLabel("0.382",r2,Yellow,13);
//drawLine(r1,"R1", Yellow,2);
//drawLabel("0.236",r1,Yellow,13);
 
ObjectDelete("xpMA");
if(target1>0 && ShowTarget==true){
drawLine(target1,"TP1", Target_color,2);
drawLabel("TargetUp",target1,Target_color,7);
drawTarget("Target1=",target1,Target_color,1);
if(r4>r2 && Close[0]>r4 && target1>r4)  DrawObject(1, (target1+curhigh)/2);
if(r4<r2 && Close[0]>r2 && target1>r2)  DrawObject(1, (target1+curhigh)/2);
}

if(target2>0 && ShowTarget==true){
drawLine(target2,"TP2", Target_color,2);
drawLabel("TargetDown",target2,Target_color,7);
drawTarget("Target2=",target2,Target_color,2);
if(r4<r2 && Close[0]<r4 && target2<r4)  DrawObject(2,  (target2+curlow)/2);
if(r4>r2 && Close[0]<r2 && target2<r2)  DrawObject(2,  (target2+curlow)/2);
}


}
  }
  
  void drawTarget(string name,double lvl,color Color,int num)
{
 
    string target=DoubleToStr(lvl, Digits);
    string ver="Ve";
     ObjectCreate(name, OBJ_LABEL, 0, 0, 0);
if(num==1){
        ObjectSet(name, OBJPROP_XDISTANCE, 470);
        ObjectSet(name, OBJPROP_YDISTANCE, 2);

    ObjectSetText(name, "Target UP        = "+target, 9, "Arial", Lime);
}
if(num==2){
        ObjectSet(name, OBJPROP_XDISTANCE, 470);
        ObjectSet(name, OBJPROP_YDISTANCE, 15);

    ObjectSetText(name, "Target DOWN = "+target, 9, "Arial", Lime);
}
       
}

//+------------------------------------------------------------------+
void drawLabel(string name,double lvl,color Color,int time)
{

    if(ObjectFind(name) != 0)
    {
        ObjectCreate(name, OBJ_TEXT, 0, Time[time], lvl);
        ObjectSetText(name, name, 8, "Arial", EMPTY);
        ObjectSet(name, OBJPROP_COLOR, Color);
    }
    else
    {
        ObjectMove(name, 0, Time[time], lvl);
    }
}

void drawLine(double lvl,string name, color Col,int type)
{
         if(ObjectFind(name) != 0)
         {  
               
            if(type == 1){
            ObjectCreate(name, OBJ_HLINE, 0, Time[0], lvl,Time[0],lvl); 
  
            }
            else {
            ObjectCreate(name, OBJ_GANNLINE, 0, Time[15], lvl,Time[0],lvl);
            }
            
            ObjectSet(name, OBJPROP_STYLE, STYLE_SOLID);    
            ObjectSet(name, OBJPROP_COLOR, Col);
            ObjectSet(name,OBJPROP_WIDTH,1);
            
         }
         else
         {
            ObjectDelete(name);
            
            if(type == 1){
            ObjectCreate(name, OBJ_HLINE, 0, Time[0], lvl,Time[0],lvl);
            
            }
            else {
            ObjectCreate(name, OBJ_GANNLINE, 0, Time[15], lvl,Time[0],lvl);
            }
                 
            ObjectSet(name, OBJPROP_STYLE, STYLE_SOLID);
            ObjectSet(name, OBJPROP_COLOR, Col);        
            ObjectSet(name,OBJPROP_WIDTH,1);
          
         }

    //----------------------------------------------------------------------
    }

void DrawObject(int direction,  double price)
{

   static int count = 0;
   count++;
   string Obj = "";
    Obj = "xpMA" ;

   if (direction==1) //up arrow
   {        
      ObjectCreate(Obj,OBJ_ARROW,0,Time[0],price);
      ObjectSet(Obj,OBJPROP_COLOR,UpArrowColor);
      ObjectSet(Obj,OBJPROP_ARROWCODE,UpArrowCode);
      ObjectSet(Obj,OBJPROP_WIDTH,UpArrowSize);
 }  
   
   if (direction==2) //down arrow
   {
      ObjectCreate(Obj,OBJ_ARROW,0,Time[0],price);
      ObjectSet(Obj,OBJPROP_COLOR,DownArrowColor);
      ObjectSet(Obj,OBJPROP_ARROWCODE,DownArrowCode);
      ObjectSet(Obj,OBJPROP_WIDTH,DownArrowSize);
 
    }
    
}



Sample





Analysis



Market Information Used:

Series array that contains the highest prices of each bar
Series array that contains the lowest prices of each bar
Series array that contains close prices for each bar
Series array that contains open time of each bar


Indicator Curves created:

Implements a curve of type DRAW_ARROW
Implements a curve of type DRAW_SECTION


Indicators Used:



Custom Indicators Used:

Order Management characteristics:

Other Features: