ZigZag_Fibo_v2beta

Download script
ZigZag_Fibo_v2beta

This script is designed to automate the identification of significant price movements and the visual plotting of Fibonacci retracement levels on a trading chart. By analyzing historical price data, it filters out "noise" to identify major structural highs and lows, allowing traders to see key areas of potential support or resistance.

Core Logic Overview

1. Identification of Market Structure (ZigZag Logic) The script begins by scanning recent price history to find significant "peaks" and "valleys." It does not simply mark every minor price change; instead, it uses several filters:

  • Depth and Deviation: It looks back over a specific number of bars to identify the highest highs and lowest lows. It only considers a point a "significant" peak or valley if the price moves by a certain minimum amount (the deviation), which helps ignore minor market fluctuations.
  • Smoothing: It employs a "backstep" logic to ensure that multiple points in a tight row are not all treated as significant turns, creating a cleaner, more readable trend line.

2. Dynamic Trend Analysis Once the highs and lows are identified, the script performs a second pass to organize them into a coherent sequence. It establishes a "ZigZag" pattern by:

  • Comparing current peaks/valleys against previous ones.
  • Updating the most recent significant high or low point only when a new, more extreme price point is reached.
  • Ensuring that the connection between points follows the logical flow of the market (e.g., identifying a move from one major low to the next major high).

3. Automated Fibonacci Plotting After the script identifies the most recent major "swing" (the distance between two significant peaks or valleys), it automatically draws:

  • Primary Fibonacci Retracements: A standard set of percentage levels (such as 25%, 50%, 75%, and 100%) is projected based on the identified swing. These help traders visualize where the price might "retrace" or pull back during a trend.
  • Specific Target Levels: It highlights specific high-interest levels, such as the 38.2% retracement, which is often a key area for technical analysis.

4. Real-Time Data Display To assist the trader in making quick decisions, the script calculates and displays several pieces of live information directly on the chart:

  • Price Calculations: It calculates the distance in "pips" for specific price movements.
  • Target Labels: It creates visual labels on the chart showing "Upper Targets" and "Pips to Open," providing a clear at-a-glance view of the current trade setup's potential outcomes.

User Experience

From a user's perspective, this script replaces the manual effort of clicking points on a chart to draw Fibonacci levels. It automatically detects where the market has turned, draws the relevant geometry, and labels the key price targets, ensuring that the technical analysis remains consistent and objective.

Price Data Components
Series array that contains open prices of each barSeries array that contains close prices for each bar
Miscellaneous
Implements a curve of type %1
6 Views
0 Downloads
0 Favorites
ZigZag_Fibo_v2beta
// fukinagashi
#include <stdlib.mqh>
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red
//---- indicator parameters
extern int ExtDepth=5;
extern int ExtDeviation=4;
extern int ExtBackstep=6;
extern color FibColor1= Yellow;
extern bool Corner_of_Chart_RIGHT_TOP = false;
extern int level1=0.25;
extern int level2=0.50;
extern int level3=0.75;
extern int level4=1.00;
extern int level5=0.00;


//---- indicator buffers
double ExtMapBuffer[];
double ExtMapBuffer2[];
double level_array[13]={0,0.25,0.50,0.75,1};
string leveldesc_array[13]={"0","25%","50%","75%","100%"};
double level_array2[13]={0.25};
string leveldesc_array2[13]={"25%"+ ")" + " - %$"} ;
double level_array3[13]={0.5};
int level_count;
string level_name;
double text_y;
int OldLastZigZag, OldPreviousZigZag;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//string level_array[13]={0,level1,level2,level3,level4,level5};
  
   IndicatorBuffers(2);
//---- drawing settings
   SetIndexStyle(0,DRAW_SECTION);
//---- indicator buffers mapping
   SetIndexBuffer(0,ExtMapBuffer);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexEmptyValue(0,0.0);
   ArraySetAsSeries(ExtMapBuffer,true);
   ArraySetAsSeries(ExtMapBuffer2,true);
//---- indicator short name
   IndicatorShortName("Fibodrawer");
//---- initialization done
   return(0);
  }
  
int deinit() {
	ObjectDelete("Fibo");
}
//+------------------------------------------------------------------+
//|   script program start function                                                                     |
//+------------------------------------------------------------------+
int start()

{
   
   string SPREAD="",PIPS="",PRC,Pips="",PIPS2="",PIPS3="";


   double OPEN = iOpen(NULL,1440,0);
   double CLOSE = iClose(NULL,1440,0);
   double SPRD = (Ask - Bid)/Point;
   //double SPRD = fibolevel*0.25 - fibolevel*0.50;///Point;
   PIPS =  DoubleToStr((CLOSE-OPEN)/Point,0);
   SPREAD = (DoubleToStr(SPRD,Digits-4));
   PIPS2= (OBJPROP_FIRSTLEVEL)*Ask;//+ level_array2[0] - level_array3[0])* 2;
   //PIPS2= (level_array2[0] - level_array3[0]+ 0.618)* 2;//(Ask+ level_array2[13])/Point;//1,0.382;//*;,ObjectGetFiboDescription
   //PIPS2 = (DoubleToStr(PIPS3,Digits-1));


  datetime date = D'2009.07.10';
  date -= 7*24*60*60;
  Print( TimeToStr(date) );
  
   int    shift, back,lasthighpos,lastlowpos;
   double val,res;
   double curlow,curhigh,lasthigh,lastlow;

   for(shift=Bars-ExtDepth; shift>=0; shift--)
     {
      val=Low[Lowest(NULL,0,MODE_LOW,ExtDepth,shift)];
      if(val==lastlow) val=0.0;
      else 
        { 
         lastlow=val; 
         if((Low[shift]-val)>(ExtDeviation*Point)) val=0.0;
         else
           {
            for(back=1; back<=ExtBackstep; back++)
              {
               res=ExtMapBuffer[shift+back];
               if((res!=0)&&(res>val)) ExtMapBuffer[shift+back]=0.0; 
              }
           }
        } 
      ExtMapBuffer[shift]=val;
      //--- high
      val=High[Highest(NULL,0,MODE_HIGH,ExtDepth,shift)];
      if(val==lasthigh) val=0.0;
      else 
        {
         lasthigh=val;
         if((val-High[shift])>(ExtDeviation*Point)) val=0.0;
         else
           {
            for(back=1; back<=ExtBackstep; back++)
              {
               res=ExtMapBuffer2[shift+back];
               if((res!=0)&&(res<val)) ExtMapBuffer2[shift+back]=0.0; 
              } 
           }
        }
      ExtMapBuffer2[shift]=val;
     }

   // final cutting 
   lasthigh=-1; lasthighpos=-1;
   lastlow=-1;  lastlowpos=-1;

   for(shift=Bars-ExtDepth; shift>=0; shift--)
     {
      curlow=ExtMapBuffer[shift];
      curhigh=ExtMapBuffer2[shift];
      if((curlow==0)&&(curhigh==0)) continue;
      //---
      if(curhigh!=0)
        {
         if(lasthigh>0) 
           {
            if(lasthigh<curhigh) ExtMapBuffer2[lasthighpos]=0;
            else ExtMapBuffer2[shift]=0;
           }
         //---
         if(lasthigh<curhigh || lasthigh<0)
           {
            lasthigh=curhigh;
            lasthighpos=shift;
           }
         lastlow=-1;
        }
      //----
      if(curlow!=0)
        {
         if(lastlow>0)
           {
            if(lastlow>curlow) ExtMapBuffer[lastlowpos]=0;
            else ExtMapBuffer[shift]=0;
           }
         //---
         if((curlow<lastlow)||(lastlow<0))
           {
            lastlow=curlow;
            lastlowpos=shift;
           } 
         lasthigh=-1;
        }
     }
  
   for(shift=Bars-1; shift>=0; shift--)
     {
      if(shift>=Bars-ExtDepth) ExtMapBuffer[shift]=0.0;
      else
        {
         res=ExtMapBuffer2[shift];
         if(res!=0.0) ExtMapBuffer[shift]=res;
        }

     }
     
  for(int i=0;i<32;i++)
  	int LastZigZag, PreviousZigZag;
   
   int h=0;
   while ( ExtMapBuffer[h]==0 && ExtMapBuffer2[h]==0) {
   	h++;
   }
   
   LastZigZag=h;
   
   h++;
   while(ExtMapBuffer[h]==0 && ExtMapBuffer2[h]==0) {
   	h++;
   }
   
   PreviousZigZag=h;
   
   if (OldLastZigZag!=LastZigZag || OldPreviousZigZag!=PreviousZigZag) {
   	OldLastZigZag=LastZigZag;
   	OldPreviousZigZag=PreviousZigZag;
   
   
   
   	ObjectDelete("Fibo");
  		//ObjectDelete("Line1");
      //ObjectDelete("Line2");
   	ObjectDelete("Fibo1");
   	ObjectDelete("Fibo2");
   	ObjectDelete("Fibo3");
   	ObjectDelete("MMLEVELS9");
   	ObjectDelete("MMLEVELS10");   	
   	
   	
   	
// START DRAWING FIBS
   ObjectCreate("Fibo", OBJ_FIBO,0, Time[5], ExtMapBuffer[LastZigZag], Time[0], ExtMapBuffer[PreviousZigZag]);
   ObjectSet("Fibo", OBJPROP_COLOR, Red);
   ObjectSet("Fibo", OBJPROP_STYLE, STYLE_DASHDOT);
   ObjectSet("Fibo", OBJPROP_FIBOLEVELS, level_count);
   ObjectSet("Fibo",  OBJPROP_RAY, 0 );
   	
   for(int j=0; j<level_count; j++)
   {//Print(j," ",[j]);
   
   ObjectSet("Fibo",OBJPROP_FIRSTLEVEL+j, level_array[j]);
   ObjectSetFiboDescription("Fibo",j,leveldesc_array[j] + ")" + " - %$");

   ObjectSet( "Fibo", OBJPROP_LEVELCOLOR, FibColor1) ;
   ObjectSet("Fibo",OBJPROP_LEVELSTYLE,STYLE_DOT);
   ObjectSet("Fibo",  OBJPROP_RAY, 0 );
   
//+------------------------------------------------------------------+
//|   NOUVEAU                                                                |
//+------------------------------------------------------------------+   
   
   //ObjectCreate("Line1", OBJ_TREND, 0, Time[20], Low[20], Time[0], Low[20]);
   //ObjectSet("Line1", OBJPROP_COLOR, Yellow );
   //ObjectSet("Line1",  OBJPROP_RAY, 0 );
   
   //ObjectCreate("Line2", OBJ_TREND, 0, Time[20], High[20], Time[0], High[0]);
   //ObjectSet("Line2", OBJPROP_COLOR, White );
   //ObjectSet("Line2",  OBJPROP_RAY, 0 );

   }
 
// START DRAWING FIBS # 

ObjectCreate("Fibo1", OBJ_FIBO,0, Time[3], ExtMapBuffer[LastZigZag], Time[1], ExtMapBuffer[PreviousZigZag]);

   //ObjectSet("Fibo1",OBJPROP_FIRSTLEVEL+1,0.382);
   //ObjectSetFiboDescription("Fibo1",j,leveldesc_array2[j] + ")" + " - %$"); 	
   //ObjectSetText("Fibo1",""+level_array2[j]+"", 15, "Arial Bold", Yellow);   
   
   //ObjectSet("Fibo1", OBJPROP_COLOR, White);
   //ObjectSet("Fibo1", OBJPROP_STYLE, STYLE_SOLID);
   ObjectSet("Fibo1", OBJPROP_FIBOLEVELS, level_count);
   ObjectSet("Fibo1",OBJPROP_FIRSTLEVEL+1,0.382);
   ObjectSetFiboDescription("Fibo1", 1, "38.2" + ")" + " - %$");   	
   ObjectSet( "Fibo1", OBJPROP_LEVELCOLOR, Pink) ;
   ObjectSet("Fibo1",OBJPROP_LEVELSTYLE,SYMBOL_LEFTPRICE);

//#include <stdlib.mqh>
  
 // string text;
//  for(int i=0;i<32;i++)
 //   {
 //   {
 //    text=ObjectGetFiboDescription("Fibo3",i);
 //    //---- checking whether the objects has less than 32 levels
     //if(GetLastError()!=ERR_NO_ERROR) break;
 //    Print("Fibo3","level: ",i," description: ",text);
 //   }



   ObjectCreate("Fibo3", OBJ_TEXT, 0, 0, 0);
   ObjectSetText("Fibo3",0, 10, "Arial Bold", Lime); //ObjectGetFiboDescription, ObjectGetFiboDescription
   ObjectSet("Fibo3", OBJPROP_CORNER, Corner_of_Chart_RIGHT_TOP);
   ObjectSet("Fibo3", OBJPROP_XDISTANCE, 40);
   ObjectSet("Fibo3", OBJPROP_YDISTANCE, 205); 
        
    }
  
   //ObjectCreate("Fibo1", OBJ_LABEL, 0, 0, 0);
   //ObjectSet("Fibo1",OBJPROP_FIRSTLEVEL+j, level_array2[j]);

   //ObjectSet("Fibo1", OBJPROP_CORNER, Corner_of_Chart_RIGHT_TOP);
   //ObjectSet("Fibo1", OBJPROP_XDISTANCE, 40);
   //ObjectSet("Fibo1", OBJPROP_YDISTANCE, 125);
   //ObjectSet("Fibo1",OBJPROP_FIRSTLEVEL+1,0.382);
   //ObjectSetFiboDescription("Fibo1", 1, "38.2  -->  "+DoubleToStr(fiboPrice*0.382+fiboPrice1, cPoint) ); 
   
   
   ObjectCreate("Fibo2", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("Fibo2", "Upper Target=  "+PIPS2+"", 10, "Arial Bold", Lime);
   ObjectSet("Fibo2", OBJPROP_CORNER, Corner_of_Chart_RIGHT_TOP);
   ObjectSet("Fibo2", OBJPROP_XDISTANCE, 40);
   ObjectSet("Fibo2", OBJPROP_YDISTANCE, 165);
 
                 ObjectCreate("MMLEVELS9", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("MMLEVELS9","Pips to Open", 9, "Arial", White);
   ObjectSet("MMLEVELS9", OBJPROP_CORNER, Corner_of_Chart_RIGHT_TOP);
   ObjectSet("MMLEVELS9", OBJPROP_XDISTANCE, 40);
   ObjectSet("MMLEVELS9", OBJPROP_YDISTANCE, 75); 
   
               ObjectCreate("MMLEVELS10", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("MMLEVELS10",""+PIPS+"", 9, "Arial Bold", Yellow);
   ObjectSet("MMLEVELS10", OBJPROP_CORNER, Corner_of_Chart_RIGHT_TOP);
   ObjectSet("MMLEVELS10", OBJPROP_XDISTANCE, 10);
   ObjectSet("MMLEVELS10", OBJPROP_YDISTANCE, 75);  
   
        
//string ObjectGetFiboDescription( string name, int index);     
    
    
// new text object
//if(!ObjectCreate("text_object", OBJ_TEXT, 0, D'2004.02.20 12:30', 1.0045))
//{
//Print("error: can't create text_object! code #",GetLastError());
//return(0);
//}
// new label object
//if(!ObjectCreate("label_object", OBJ_LABEL, 0, 0, 0))
//{
//Print("error: can't create label_object! code #",GetLastError());
//return(0);
//}
//ObjectSet("label_object", OBJPROP_XDISTANCE, 200);
//ObjectSet("label_object", OBJPROP_YDISTANCE, 100);

   	//Alert("New ZigZag and new Fibo drawn");
   	//PlaySound("alert.wav");
      level_count=ArraySize(level_array);
      level_count=ArraySize(level_array2);
   //Alert(level_count); 
   }
//
  
   

Comments