Author: Copyright � 2005, MetaQuotes Software Corp.
ZigZag_006

This script is a technical analysis tool designed to identify and visualize significant price movements on a trading chart. Rather than showing every minor fluctuation in price, it filters out "market noise" to highlight the primary peaks (highs) and valleys (lows) that define the overall trend of an asset.

Core Logic and Functionality

The script operates through a multi-stage filtering process to ensure that only the most relevant price points are displayed:

1. Identifying Potential Extremes The tool scans historical price data using three specific user-defined parameters:

  • Depth: Determines how far back the script looks to find a local high or low.
  • Deviation: Acts as a sensitivity filter. It ensures that a new point is only recognized if it moves significantly enough away from the previous one, preventing the tool from marking every tiny tick as a turning point.
  • Backstep: A refinement setting that helps smooth the connection between points, ensuring that very close price movements don't create cluttered, jagged lines.

2. Data Filtering and Comparison Once the script identifies potential high and low points, it performs a comparative check. It looks back at recently identified points to see if the new data "beats" the previous extreme. If a new peak is higher than the previous one (or a new valley is lower), the script updates its coordinates. If the new movement is less significant, it ignores it.

3. Final Refinement and Connection In the final stage of its logic, the script performs a "cleaning" pass. It verifies the sequence of peaks and valleys to ensure they are logically connected. It specifically looks for alternating movements—ensuring that a high point is followed by a low point and vice versa. This step removes any overlapping or redundant data points, resulting in a clean, continuous line (often referred to as a "ZigZag" pattern).

Summary of Purpose

For a trader, this logic serves as a visual map of the market's structure. By stripping away minor price vibrations and highlighting only the major structural shifts, it allows you to see the "big picture" of market momentum, making it easier to identify support levels, resistance zones, and overall trend directions.

Miscellaneous
Implements a curve of type %1
14 Views
0 Downloads
0 Favorites
ZigZag_006
//+------------------------------------------------------------------+
//|                                        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)&&(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;
        }
     }
  }

Comments