ZigZag_Fibo_Alert
Download scriptThis script is designed to automatically identify significant structural turning points in market price action—specifically the highest peaks and lowest troughs—and use those points to draw Fibonacci retracement levels on your trading chart.
How the Logic Works
1. Identifying Key Price Points The tool begins by scanning a specific window of recent historical data (the "Depth") to look for significant highs and lows. It doesn't just mark every price movement; instead, it filters out minor fluctuations. It looks for price points that meet specific criteria regarding how much the price has moved relative to nearby bars. By using these parameters, the script ignores "market noise" and focuses only on the meaningful shifts in price direction.
2. Refining the Structure (ZigZag Logic) To ensure the lines drawn on the chart are clean and logical, the script performs a secondary refinement process. It compares new peaks and troughs against previous ones:
- If it finds a new peak that is higher than the previous one, it "re-adjusts" the structure by removing the old, lower peak.
- Conversely, if it finds a new low that is deeper than the previous low, it updates the structural point accordingly.
This ensures that the chart only displays the most prominent "swings," creating a clear visual path of the market's journey.
3. Automated Fibonacci Drawing Once the script has successfully identified the most recent major peak and the most recent major trough, it automatically connects them with a Fibonacci Retracement tool.
Because the market is live and constantly moving, the script continuously monitors these points. Whenever the price creates a new significant high or low that changes the overall structure:
- The script identifies the new "start" and "end" of the trend.
- It automatically deletes the old Fibonacci level.
- It draws a brand-new Fibonacci tool based on the updated structural points.
4. Notifications To keep the trader informed without needing to watch the screen constantly, the script includes an alert system. Every time the market structure changes enough to trigger a new Fibonacci drawing, the tool will provide a visual alert and play a sound notification, letting you know that the primary trend boundaries have shifted.
// fukinagashi
#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[PreviousZigZag], Time[LastZigZag], ExtMapBuffer[LastZigZag]);
Alert("New ZigZag and new Fibo drawn");
PlaySound("alert.wav");
}
}
Comments