ZigZagFirst_v2

Author: Copyright � 2005, MetaQuotes Software Corp.
ZigZagFirst_v2

This script functions as a specialized technical analysis tool designed to identify significant price turning points—specifically peaks and valleys—within a financial market chart. It simplifies the complex movement of price history into a clear visual path that highlights where the market has shifted direction.

Core Logic and Behavior

1. Dynamic Peak and Valley Identification The tool constantly scans recent price data (based on a user-defined depth) to find the highest and lowest points reached within a specific timeframe. Rather than showing every tiny fluctuation, it filters the data based on "Deviation" settings. This ensures that only meaningful price movements are recorded, ignoring minor market noise or insignificant price "jitters."

2. The "ZigZag" Filtering Mechanism The script employs a logic similar to a ZigZag indicator. It tracks the progression of highs and lows to determine the structural path of the market. If the market reaches a new high that is higher than the previous one recorded, or a new low that is lower than the previous one, the script updates the "active" point. If a movement is found to be less significant than a previously established point, the script essentially ignores it to maintain a clean, logical flow of data.

3. Data Validation and Refinement To ensure accuracy, the script performs a secondary "cleanup" pass on the data. It compares consecutive peaks and valleys to ensure they follow a logical sequence (i.e., a high must be followed by a low, and vice versa). If it detects an inconsistency—such as two highs occurring consecutively without a significant dip between them—it removes the weaker point. This refining process creates a continuous "mountain and valley" structure on the chart.

4. Final Visual Integration In its final stage, the script merges these identified high and low points into a unified visual representation. It ensures that the lines connecting these points are smooth and continuous, allowing traders to see the overarching trend of the market clearly. By highlighting only the most important structural shifts, it helps users identify support and resistance levels more effectively.

Key Parameters Explained

  • Depth: Determines how far back the script looks to find a high or low point (the "lookback" window).
  • Deviation: Acts as a filter; the price must move at least this much for the script to consider it a significant new point.
  • Backstep: A refinement setting that helps the script verify that price movements are distinct from one another, preventing the drawing of multiple points in very close proximity.
Miscellaneous
Implements a curve of type %1
14 Views
0 Downloads
0 Favorites
ZigZagFirst_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 White
//---- 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("ZigZagFirstBar("+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)) continue;//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)) continue;//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