Author: Copyright � 2005, MetaQuotes Software Corp.
ZigZag1_v2

This tool is designed to identify and visualize significant structural shifts in price action by filtering out minor market "noise." It automatically identifies key high and low points on a chart, connecting them to form a clear trend path. Here is a breakdown of the logic used to achieve this:

Core Methodology

The script functions by scanning historical price data to find prominent peaks (highs) and valleys (lows). Rather than marking every single price movement, it uses three primary parameters to determine what qualifies as a significant turning point:

  • Depth: This defines the window of time or "lookback" period the tool examines to find a high or low. A larger depth means the tool focuses on more significant, long-term structural changes, while a smaller depth makes it more sensitive to shorter-term price swings.
  • Deviation: This acts as a filter to ensure that only meaningful moves are captured. It requires the price to move a specific minimum distance before a new point is recognized, preventing the tool from reacting to tiny, insignificant fluctuations.
  • Backstep: This manages how "close" two points can be to one another. It helps ensure that multiple points aren't clustered too tightly together, which keeps the visual output clean and readable.

The Processing Logic

The script operates in several distinct stages to refine the data:

  1. Initial Identification: The tool first scans the chart to find candidate highs and lows based on the Depth and Deviation settings. It looks for price extremes that stand out significantly from the surrounding bars.
  2. Conflict Resolution: Once potential points are identified, the logic performs a "cleanup" pass. If two high points (or low points) are found too close together, it compares them and retains only the most extreme one, discarding the other to maintain a smooth visual flow.
  3. Dynamic Updating: The script constantly evaluates new price data as it arrives. It is designed to update the "current" peak or valley dynamically, ensuring that the most recent trend information is always represented on the chart.

Visual Result

The end result for the user is a clean, zig-zagging line that highlights the primary "skeleton" of the market's movement. By stripping away the minor fluctuations and only highlighting the major turning points, it allows traders to more easily identify the overall direction of the trend and significant levels of support and resistance.

Miscellaneous
Implements a curve of type %1
12 Views
0 Downloads
0 Favorites
ZigZag1_v2
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

//+------------------------------------------------------------------+
//|                                        Custom Moving Average.mq4 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/"

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red
//---- indicator parameters
extern int ExtDepth=12;
extern int ExtDeviation=5;
extern int ExtBackstep=3;
//---- indicator buffers
double ExtMapBuffer[];
double ExtMapBuffer2[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   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("ZigZag("+ExtDepth+","+ExtDeviation+","+ExtBackstep+")");
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   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.0)||(curhigh==0.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;
           };//z
         //---
         if((curlow<lastlow)||(lastlow<0))
           {
            lastlow=curlow;
            lastlowpos=shift;
           };//z 
         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;
         //if(res>0.0) ExtMapBuffer[shift]=res;
        }
     }
  }

Comments