ZigZag_v1
Download scriptThis script is a specialized visual tool designed to identify and map out significant structural trends in financial charts. It simplifies complex price movements into a clear, "zigzag" pattern that highlights the most important peaks and valleys in market history.
How the Logic Works
The core objective of this tool is to filter out minor market "noise"—the small, insignificant fluctuations in price—to reveal the larger underlying direction of the asset. It achieves this through three primary stages of logic:
1. Identifying Extremes The script constantly scans recent price data (based on a depth setting defined by the user) to find the highest highs and lowest lows. Rather than recording every single price change, it focuses only on points that represent a significant move away from the current trend.
2. Filtering for Significance To prevent the chart from becoming cluttered with too many lines, the script applies a "deviation" filter. It measures how much the price has moved; if a price movement is too small to be considered meaningful based on your settings, the script ignores it. This ensures that only substantial price swings are captured as turning points.
3. Trend Refining and Re-drawing The most sophisticated part of the logic occurs when new price data arrives. If the market continues to move in one direction (for example, reaching a new high), the script "undoes" previous smaller peaks that were identified along that same path. It continuously adjusts the lines so that only the absolute highest peak and lowest valley remain visible.
Key Features for the User
- Visual Clarity: By connecting only the significant highs and lows, it provides a clean visual representation of market structure, making it easier to see where trends are starting or ending.
- Dynamic Adjustment: The script automatically recalculates based on the latest price action, updating the "zigzag" as the market evolves.
- Customizable Sensitivity: Users can adjust parameters (Depth, Deviation, and Backstep) to make the tool more or less sensitive. A higher depth will capture broader, long-term trends, while lower settings will highlight shorter-term fluctuations.
In summary, this script acts as a filter that strips away the "messy" details of price action, leaving you with a clear, organized map of the market's major movements.
/*-----------------------------+
| |
| 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)&&(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