Swing_ZZ
Download scriptThis script is designed to act as a visual navigation tool on a trading chart, specifically identifying significant structural turning points in price action. It functions similarly to a "ZigZag" indicator, which simplifies the complex movements of a market into a series of connected peaks and valleys.
Core Logic and Behavior
The primary objective of this script is to filter out minor price fluctuations (market noise) and highlight only the most substantial shifts in trend. It achieves this through several logical layers:
1. Price Trend Tracking The script constantly monitors the high and low prices of each candle. It keeps track of whether the current market movement is trending upwards or downwards by comparing the current price range to the preceding one. It classifies bars as "Internal" (part of a continuing move) or "External" (potentially a new turning point).
2. Movement Validation (The Buffer System)
To prevent the indicator from jumping at every small tick, it uses a verification system. It counts how many consecutive candles are moving in a specific direction. Only when this count exceeds a user-defined minimum threshold (controlled by the minBars parameter) does the script recognize a confirmed trend change. This ensures that short-lived price spikes do not trigger a new "peak" or "valley" on your chart.
3. Peak and Valley Identification The script identifies two specific types of structural points:
- Highs (Peaks): When a sustained upward trend is interrupted by a significant downward move, the script marks the high point.
- Lows (Valleys): When a sustained downward trend is interrupted by a significant upward move, the script marks the low point.
4. Dynamic Updates and "Cleaning" Because markets are live, the script includes logic to handle real-time updates. If a new high or low is formed before the current "swing" is finalized, the indicator can adjust its markers dynamically. Additionally, it contains a "cleaning" mechanism that ensures that once a confirmed peak or valley is established, the visual lines do not clutter the chart with redundant data from intermediate price movements.
Visual Output
- ZigZag Lines: The script connects the identified highs and lows with lines, providing a clear skeleton of the market's history.
- Directional Markers: It uses specific symbols (arrows) to highlight exactly where the trend shifted from bullish to bearish, or vice versa.
- Visual Flexibility: Users can toggle certain labels (via the
ExtLabelparameter) to show or hide extra visual markers at the turning points, depending on how much detail they want to see on their screen.
Summary for the User
Think of this script as a "trend filter." Instead of seeing every tiny heartbeat of the market, it shows you the "major strides" the price is taking. By identifying significant structural highs and lows based on user-defined minimum requirements, it helps traders visualize the overarching direction of the market more clearly.
//+------------------------------------------------------------------+
//| Swing_ZZ_1.mq4 |
//+------------------------------------------------------------------+
#property copyright "onix"
#property link "http://onix-trade.net/forum/index.php?s=&showtopic=4786&view=findpost&p=275885"
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Aqua
#property indicator_color2 Blue
#property indicator_color3 Red
#property indicator_width2 2
#property indicator_width3 2
//---- indicator parameters
extern int minBars = 1;
extern int ExtLabel= 1;
//---- indicator buffers
double zzL[];
double zzH[];
double zz[];
int cbi;
double lLast=0,hLast=0, hBar, lBar;
int fs=0;
int ai,bi,ai0,bi0,aim,bim;
datetime tai,tbi,ti, tmh, tml, tiZZ;
// Ïåðåìåííûå äëÿ Ñâèíãîâ Ãàííà
double lLast_m=0, hLast_m=0;
int countBarExt; // ñ÷åò÷èê âíåøíèõ áàðîâ
int countBarl,countBarh;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
// IndicatorBuffers(3);
//---- drawing settings
SetIndexStyle(0,DRAW_SECTION);
SetIndexStyle(1,DRAW_ARROW);
SetIndexStyle(2,DRAW_ARROW);
SetIndexArrow(1,159);
SetIndexArrow(2,159);
//---- indicator buffers mapping
SetIndexBuffer(0,zz);
SetIndexBuffer(1,zzH);
SetIndexBuffer(2,zzL);
SetIndexEmptyValue(0,0.0);
SetIndexEmptyValue(1,0.0);
SetIndexEmptyValue(2,0.0);
//---- indicator short name
IndicatorShortName("Swing_ZZ("+minBars+")");
//---- initialization done
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
int i,n;
// lLast, hLast - ìèíèìóì è ìàêñèìóì àêòèâíîãî áàðà
// lLast_m, hLast_m - ìèíèìóì è ìàêñèìóì "ïðîìåæóòî÷íûõ" áàðîâ
if (lBar<=Low[0] && hBar>=High[0] && tiZZ==Time[0]) return(0);
else {lBar=Low[0]; hBar=High[0]; tiZZ=Time[0];}
// cbi=Bars-IndicatorCounted();
cbi=Bars-1;
ArrayInitialize(zz,0.0);
ArrayInitialize(zzL,0.0);
ArrayInitialize(zzH,0.0);
//---------------------------------
for (i=cbi; i>=0; i--)
{
//-------------------------------------------------
// Óñòàíàâëèâàåì íà÷àëüíûå çíà÷åíèÿ ìèíèìóìà è ìàêñèìóìà áàðà
if (lLast==0) {lLast=Low[i]; hLast=High[i]; ai=i; bi=i;}
if (ti!=Time[i])
{
ti=Time[i];
if (lLast_m==0 && hLast_m==0)
{
if (lLast>Low[i] && hLast<High[i]) // Âíåøíèé áàð
{
lLast=Low[i];hLast=High[i];lLast_m=Low[i];hLast_m=High[i];countBarExt++;
if (fs==1) {countBarl=countBarExt; ai=i; tai=Time[i];}
else if (fs==2) {countBarh=countBarExt; bi=i; tbi=Time[i];}
else {countBarl++;countBarh++;}
}
else if (lLast<=Low[i] && hLast<High[i]) // Òåíäåíöèÿ íà òåêóùåì áàðå âîñõîäÿùàÿ
{
lLast_m=0;hLast_m=High[i];countBarl=0;countBarExt=0;
if (fs!=1) countBarh++;
else {lLast=Low[i]; hLast=High[i]; lLast_m=0; hLast_m=0; ai=i; tai=Time[i];}
}
else if (lLast>Low[i] && hLast>=High[i]) // Òåíäåíöèÿ íà òåêóùåì áàðå íèñõîäÿùàÿ
{
lLast_m=Low[i];hLast_m=0;countBarh=0;countBarExt=0;
if (fs!=2) countBarl++;
else {lLast=Low[i]; hLast=High[i]; lLast_m=0; hLast_m=0; bi=i; tbi=Time[i];}
}
}
else if (lLast_m>0 && hLast_m>0) // Âíåøíèé áàð (ïðåäûäóùèé)
{
if (lLast_m>Low[i] && hLast_m<High[i]) // Âíåøíèé áàð
{
lLast=Low[i];hLast=High[i];lLast_m=Low[i];hLast_m=High[i];countBarExt++;
if (fs==1) {countBarl=countBarExt; ai=i; tai=Time[i];}
else if (fs==2) {countBarh=countBarExt; bi=i; tbi=Time[i];}
else {countBarl++;countBarh++;}
}
else if (lLast_m<=Low[i] && hLast_m<High[i]) // Òåíäåíöèÿ íà òåêóùåì áàðå âîñõîäÿùàÿ
{
lLast_m=0;hLast_m=High[i];countBarl=0;countBarExt=0;
if (fs!=1) countBarh++;
else {lLast=Low[i]; hLast=High[i]; lLast_m=0; hLast_m=0; ai=i; tai=Time[i];}
}
else if (lLast_m>Low[i] && hLast_m>=High[i]) // Òåíäåíöèÿ íà òåêóùåì áàðå íèñõîäÿùàÿ
{
lLast_m=Low[i];hLast_m=0;countBarh=0;countBarExt=0;
if (fs!=2) countBarl++;
else {lLast=Low[i]; hLast=High[i]; lLast_m=0; hLast_m=0; bi=i; tbi=Time[i];}
}
}
else if (lLast_m>0)
{
if (lLast_m>Low[i] && hLast<High[i]) // Âíåøíèé áàð
{
lLast=Low[i];hLast=High[i];lLast_m=Low[i];hLast_m=High[i];countBarExt++;
if (fs==1) {countBarl=countBarExt; ai=i; tai=Time[i];}
else if (fs==2) {countBarh=countBarExt; bi=i; tbi=Time[i];}
else {countBarl++;countBarh++;}
}
else if (lLast_m<=Low[i] && hLast<High[i]) // Òåíäåíöèÿ íà òåêóùåì áàðå âîñõîäÿùàÿ
{
lLast_m=0;hLast_m=High[i];countBarl=0;countBarExt=0;
if (fs!=1) countBarh++;
else {lLast=Low[i]; hLast=High[i]; lLast_m=0; hLast_m=0; ai=i; tai=Time[i];}
}
else if (lLast_m>Low[i] && hLast>=High[i]) // Òåíäåíöèÿ íà òåêóùåì áàðå íèñõîäÿùàÿ
{
lLast_m=Low[i];hLast_m=0;countBarh=0;countBarExt=0;
if (fs!=2) countBarl++;
else {lLast=Low[i]; hLast=High[i]; lLast_m=0; hLast_m=0; bi=i; tbi=Time[i];}
}
}
else if (hLast_m>0)
{
if (lLast>Low[i] && hLast_m<High[i]) // Âíåøíèé áàð
{
lLast=Low[i];hLast=High[i];lLast_m=Low[i];hLast_m=High[i];countBarExt++;
if (fs==1) {countBarl=countBarExt; ai=i; tai=Time[i];}
else if (fs==2) {countBarh=countBarExt; bi=i; tbi=Time[i];}
else {countBarl++;countBarh++;}
}
else if (lLast<=Low[i] && hLast_m<High[i]) // Òåíäåíöèÿ íà òåêóùåì áàðå âîñõîäÿùàÿ
{
lLast_m=0;hLast_m=High[i];countBarl=0;countBarExt=0;
if (fs!=1) countBarh++;
else {lLast=Low[i]; hLast=High[i]; lLast_m=0; hLast_m=0; ai=i; tai=Time[i];}
}
else if (lLast>Low[i] && hLast_m>=High[i]) // Òåíäåíöèÿ íà òåêóùåì áàðå íèñõîäÿùàÿ
{
lLast_m=Low[i];hLast_m=0;countBarh=0;countBarExt=0;
if (fs!=2) countBarl++;
else {lLast=Low[i]; hLast=High[i]; lLast_m=0; hLast_m=0; bi=i; tbi=Time[i];}
}
}
// Îïðåäåëÿåì íàïðàâëåíèå òðåíäà.
if (fs==0)
{
if (lLast<lLast_m && hLast>hLast_m) // âíóòðåííèé áàð
{
lLast=Low[i]; hLast=High[i]; ai=i; bi=i; countBarl=0;countBarh=0;countBarExt=0;
}
if (countBarh>countBarl && countBarh>countBarExt && countBarh>minBars)
{
lLast=Low[i]; hLast=High[i]; lLast_m=0; hLast_m=0;
fs=1;countBarh=0;countBarl=0;countBarExt=0;
zz[bi]=Low[bi];
zzL[bi]=Low[bi];
zzH[bi]=0;
ai=i;
tai=Time[i];
}
else if (countBarl>countBarh && countBarl>countBarExt && countBarl>minBars)
{
lLast=Low[i]; hLast=High[i]; lLast_m=0; hLast_m=0;
fs=2;countBarl=0;countBarh=0;countBarExt=0;
zz[ai]=High[ai];
zzH[ai]=High[ai];
zzL[ai]=0;
bi=i;
tbi=Time[i];
}
}
else
{
if (lLast_m==0 && hLast_m==0)
{
countBarl=0;countBarh=0;countBarExt=0;
}
// Òåíäåíöèÿ âîñõîäÿùàÿ
if (fs==1)
{
if (countBarl>countBarh && countBarl>countBarExt && countBarl>minBars) // Îïðåäåëÿåì òî÷êó ñìåíû òåíäåíöèè.
{
// çàïîìèíàåì çíà÷åíèå íàïðàâëåíèÿ òðåíäà fs íà ïðåäûäóùåì áàðå
ai=iBarShift(Symbol(),Period(),tai);
fs=2;
countBarl=0;
zz[ai]=High[ai];
zzH[ai]=High[ai];
zzL[ai]=0;
if (ExtLabel>0) {zzH[ai]=High[ai]; zzL[ai]=0; tml=Time[i]; zzH[i]=0; zzL[i]=Low[i];}
bi=i;
tbi=Time[i];
lLast=Low[i]; hLast=High[i]; lLast_m=0; hLast_m=0;
for (n=0;countBarExt<minBars;n++)
{
if (lLast<Low[i+n+1] && hLast>High[i+n+1]) {countBarExt++; countBarh++; lLast=Low[i+n+1]; hLast=High[i+n+1]; hLast_m=High[i];}
else break;
}
lLast=Low[i]; hLast=High[i];
}
}
// Òåíäåíöèÿ íèñõîäÿùàÿ
if (fs==2)
{
if (countBarh>countBarl && countBarh>countBarExt && countBarh>minBars) // Îïðåäåëÿåì òî÷êó ñìåíû òåíäåíöèè.
{
// çàïîìèíàåì çíà÷åíèå íàïðàâëåíèÿ òðåíäà fs íà ïðåäûäóùåì áàðå
bi=iBarShift(Symbol(),Period(),tbi);
fs=1;
countBarh=0;
zz[bi]=Low[bi];
zzL[bi]=Low[bi];
zzH[bi]=0;
if (ExtLabel>0) {zzH[bi]=0; zzL[bi]=Low[bi]; tmh=Time[i]; zzH[i]=High[i]; zzL[i]=0;}
ai=i;
tai=Time[i];
lLast=Low[i]; hLast=High[i]; lLast_m=0; hLast_m=0;
for (n=0;countBarExt<minBars;n++)
{
if (lLast<Low[i+n+1] && hLast>High[i+n+1]) {countBarExt++; countBarl++; lLast=Low[i+n+1]; hLast=High[i+n+1]; lLast_m=Low[i];}
else break;
}
lLast=Low[i]; hLast=High[i];
}
}
}
}
if (i==0)
{
if (hLast<High[i] && fs==1) // Òåíäåíöèÿ íà òåêóùåì áàðå âîñõîäÿùàÿ
{
ai=i; tai=Time[i]; zz[ai]=High[ai]; zzH[ai]=High[ai]; zzL[ai]=0;
}
else if (lLast>Low[i] && fs==2) // Òåíäåíöèÿ íà òåêóùåì áàðå íèñõîäÿùàÿ
{
bi=i; tbi=Time[i]; zz[bi]=Low[bi]; zzL[bi]=Low[bi]; zzH[bi]=0;
}
//===================================================================================================
// Íóëåâîé áàð. Ðàñ÷åò ïåðâîãî ëó÷à ZigZag-a
ai0=iBarShift(Symbol(),0,tai);
bi0=iBarShift(Symbol(),0,tbi);
if (bi0>1) if (fs==1) {for (n=bi0-1; n>=0; n--) {zzH[n]=0.0; zz[n]=0.0;} zz[ai0]=High[ai0]; zzH[ai0]=High[ai0]; zzL[ai0]=0.0;}
if (ai0>1) if (fs==2) {for (n=ai0-1; n>=0; n--) {zzL[n]=0.0; zz[n]=0.0;} zz[bi0]=Low[bi0]; zzL[bi0]=Low[bi0]; zzH[bi0]=0.0;}
if (ExtLabel>0)
{
if (fs==1) {aim=iBarShift(Symbol(),0,tmh); zzH[aim]=High[aim];}
else if (fs==2) {bim=iBarShift(Symbol(),0,tml); zzL[bim]=Low[bim];}
}
if (ti<Time[1]) i=2;
}
//====================================================================================================
}
//--------------------------------------------
return(0);
}
//+------------------------------------------------------------------+
Comments