Fibo_Retracement_Dynamic

Download script
Fibo_Retracement_Dynamic

This script is designed to automate the identification of key market price levels and potential reversal zones by analyzing historical price action. It focuses on identifying structural highs and lows to calculate dynamic Fibonacci retracement levels in real-time.

Market Structure Analysis

The core logic begins by scanning a specific depth of past price data (determined by the user's settings) to find significant peaks and valleys. Rather than just looking at static points, the script monitors the progression of these highs and lows:

  • High/Low Tracking: It continuously tracks the most recent significant high and low points.
  • Dynamic Updates: When a new price point exceeds the previous high or low within the specified depth, the script updates its internal "memory" of where the structural boundaries currently sit.

Fibonacci Retracement Calculations

Once the script identifies the most recent significant range (the distance between the current high and low), it automatically calculates three specific Fibonacci retracement levels:

  • 0.618 Level: Often considered a major turning point in technical analysis.
  • 0.500 Level: The midpoint of the price move.
  • 0.382 Level: A secondary support/resistance level.

The script prioritizes the 0.618 and 0.382 levels for visual display, as these are frequently used by traders to gauge how deeply a market might "pull back" before continuing its trend.

Dynamic Target Identification

One of the more advanced features of this script is its ability to project potential profit targets based on current price momentum:

  • Momentum Detection: The script checks if the current market price has broken through specific Fibonacci thresholds.
  • Target Projection: If the price moves significantly in one direction (up or down) and clears a key level, the script calculates a "Target" price based on the total range of the move.
  • Conditional Visibility: These targets only appear if certain conditions are met—specifically, when the price confirms it has entered a new trend phase.

Visual Feedback and Automation

To make this data actionable for a user, the script handles all the "heavy lifting" regarding visual organization:

  • Automatic Drawing: It draws horizontal lines and labels for the Highs, Lows, and Fibonacci levels automatically.
  • Real-time Cleanup: As new price data arrives, the script removes old, irrelevant objects and replaces them with updated ones, ensuring the chart stays clean.
  • Signal Arrows: When a specific combination of price action and target conditions are met, the script places visual arrows on the chart to highlight potential entry or exit points.

In summary, this tool transforms raw price history into a structured map of support, resistance, and targets, allowing a trader to see where the market is likely to react without having to manually draw Fibonacci levels every time the price moves.

Miscellaneous
Implements a curve of type %1
12 Views
0 Downloads
0 Favorites
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);
 
    }
    
}

Comments