ZigZag_Fibo_v2beta_v2
Download scriptThis script is designed to automatically analyze price movements on a trading chart to identify significant structural turning points, specifically focusing on highs and lows. By distilling the "noise" of minor price fluctuations, it helps traders visualize the underlying trend and provides automated Fibonacci retracement levels based on those movements.
How the Logic Works
1. Identifying Key Price Points The script scans historical price data over a specific window (defined by depth) to find significant peaks and troughs. Instead of reacting to every tiny price tick, it uses a "deviation" filter. This means it only recognizes a new high or low if the price moves a meaningful amount away from the previous point. This prevents the tool from creating cluttered lines during sideways or consolidated markets.
2. Smoothing and Filtering To ensure the trend lines are clean, the script employs a multi-pass filtering logic. It checks preceding bars to verify that a detected high or low is truly prominent. If a newer price point contradicts a previously identified peak (for example, if a new higher high appears), the script intelligently "prunes" the old data points to ensure only the most relevant structural peaks remain visible.
3. Dynamic Trend Mapping The tool maintains two primary tracking systems: one for identifying the "ceiling" (highs) and one for the "floor" (lows). By constantly comparing these, it determines the most recent major swing in the market. This allows the script to distinguish between a temporary price retracement and a fundamental change in trend direction.
4. Automated Fibonacci Drawing The most user-friendly feature of this script is its ability to automate technical analysis. Once the logic confirms a completed "swing" (a sequence of one major high and one major low), it automatically draws a Fibonacci Retracement object on your chart.
5. Real-Time Alerts Because the script constantly recalculates as new candles form, it can detect the exact moment a new structural swing is completed. When the market completes a move that qualifies as a new "ZigZag" pattern, the script will:
- Automatically delete the old Fibonacci drawing.
- Draw a fresh Fibonacci retracement based on the newest high and low points.
- Trigger a visual alert on your screen and play an audible notification sound.
Summary for the User
In short, this tool acts as an automated analyst. It watches the market for you, ignores the minor "jitter" of price movements, identifies the most important structural peaks and valleys, and automatically draws your Fibonacci retracement levels whenever the market establishes a new significant trend move.
//+------------------------------------------------------------------+
//| fukinagashi |
//| Copyright © 2000-2007, MetaQuotes Software Corp. |
//| http://www.metaquotes.ru |
//+------------------------------------------------------------------+
#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[];
int OldLastZigZag, OldPreviousZigZag;
//+------------------------------------------------------------------+
//| 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("Fibodrawer");
//---- initialization done
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int deinit()
{
ObjectDelete("Fibo");
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
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;
}
}
int i=0;
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");
ObjectCreate("Fibo", OBJ_FIBO, 0, Time[PreviousZigZag], ExtMapBuffer[LastZigZag], Time[LastZigZag], ExtMapBuffer[PreviousZigZag]);
Alert("New ZigZag and new Fibo drawn");
PlaySound("alert.wav");
}
}
//+------------------------------------------------------------------+
Comments