ZigZagOnParabolic_v1
Download scriptThis tool is designed for the MetaTrader platform to provide a visual representation of market trends by identifying key price movements and turning points. It combines two classic technical analysis concepts—the Parabolic SAR and ZigZag—into a single cohesive indicator to help traders visualize price momentum and structural shifts.
Core Functionality
The logic operates by monitoring real-time price action (highs, lows, and midpoints) and comparing them against the Parabolic SAR values. Instead of just showing dots or lines, it uses the Sar's "flip" signals to trigger the creation of a ZigZag structure.
Key features include:
- Trend Identification (Parabolic SAR): The script calculates the Parabolic SAR, which serves as the primary engine for detecting trend reversals. It monitors when the value moves from above the price to below it (or vice versa), signaling a change in momentum.
- Structural Mapping (ZigZag): Once a trend reversal is detected via the Parabolic SAR, the script marks the highest peak or the lowest valley reached during that specific movement. This creates a "connect-the-dots" visual that highlights the significant highs and lows of the market history.
- Dynamic Positioning: The logic includes a unique "Shift" mechanism. Depending on your settings, it can place these peaks and valleys at the exact moment they were identified or visually offset them to better align with the actual historical price point.
- Data Management: The script is built to be efficient. It handles "missed bars" (gaps in data) by automatically re-initializing its calculations to ensure the visual lines remain continuous and accurate even if the internet connection flickers or the chart updates unexpectedly.
How it Benefits the User
By merging these two methods, the indicator filters out minor market "noise." Rather than showing every small price fluctuation, it highlights only the significant structural turns. This allows a trader to:
- Easily see the overall direction of the market at a glance.
- Identify support and resistance levels based on historical peaks and troughs.
- Observe the timing of trend reversals more clearly by watching how the ZigZag points are generated relative to the Parabolic SAR signals.
#property indicator_chart_window // â îêíå èíñòðóìåíòà
#property indicator_buffers 3
#property indicator_color1 Aqua
#property indicator_color3 Blue
//--âíåøíèå ïåðåìåííûå
extern double Step=0.02; // íà÷àëüíîå çíà÷åíèå è øàã
extern double Maximum=0.2; // êîíå÷íîå çíà÷åíèå
extern bool ExtremumsShift=1; // ïîëîæåíèå ýêñòðåìóìîâ: 0 - ïî âðåìåíè èõ îïðåäåëíèÿ; 1 - ïî èõ ôàêòè÷åñêîìó ïîëîæåíèþ
extern int History=0; // êîë-âî áàðîâ ïðåäûñòîðèè; 0 - âñå
//--
double Peak[], // áóôåð ZigZag ïî ïèêàì
Trough[], // áóôåð ZigZag ïî âïàäèíàì
SAR[]; // áóôåð Parabolic
//=============================================================
int init()
{
SetIndexBuffer(0,Peak); // ïèêè
SetIndexStyle(0,DRAW_ZIGZAG);
SetIndexLabel(0,"Peak");
SetIndexEmptyValue(0,0.0);
SetIndexBuffer(1,Trough); // êðåñòû, ò.å. âïàäèíû)))
SetIndexStyle(1,DRAW_ZIGZAG);
SetIndexLabel(1,"Trough");
SetIndexEmptyValue(1,0.0);
SetIndexBuffer(2,SAR); // Ïàðàáîëèê
SetIndexStyle(2,DRAW_ARROW);
SetIndexArrow(2,159);
SetIndexLabel(2,"SAR");
SetIndexEmptyValue(2,0.0);
return(0);
}
//=============================================================
int start()
{
static int BarsPrev; // çíà÷åíèå Bars íà ïðåä.áàðå
bool MissBars=Bars-BarsPrev>1; // 1 - åñòü ïðîïóùåííûå áàðû
bool NewBar=Bars-BarsPrev==1; // 1 - ïåðâûé òèê íóëåâîãî áàðà
if(MissBars && BarsPrev!=0) BarsPrev=reinit(); // ïðîïóùåííûå áàðû â ïðîöåññå - ïåðåñ÷åò çàíîâî
int limit=Bars-BarsPrev-(BarsPrev==0); BarsPrev=Bars; // êîë-âî ïåðåñ÷åòîâ
if(History!=0 && limit>History) limit=History-1; // êîë-âî ïåðåñ÷åòîâ ïî èñòîðèè
int counted_bars = IndicatorCounted();
if(counted_bars < 0) return(-1);
if(counted_bars > 0) counted_bars--;
limit = Bars - counted_bars;
if(counted_bars==0) limit-=2+ExtremumsShift*10;
for(int i=limit; i>=0; i--) // öèêë ïî íåïîñ÷èòàííûì è ïðåäïîñëåäíåìó áàðàì
{
SAR[i]=iSAR(NULL,0,Step,Maximum, i); // Ïàðàáîëèê
double mid[2]; // ñð. öåíà
mid[0]=(High[i]+Low[i])/2; // ñð.öåíà íà òåêóùåì áàðå
mid[1]=(High[i+1]+Low[i+1])/2; // ñð.öåíà íà ïðåä.áàðå
static int j; // ñ÷åò÷èê ñìåùåíèÿ ìåæäó ìîìåíòîì îïðåäåëåíÿ ýêñòðåìóìà è åãî ïîëîæåíèåì âî âðåìåíè
static bool dir; // ôëàã íàïðàâëåíèÿ; 0 - âíèç, 1 - ââåðõ
static double h,l; // òåêóùèå ýêñòðåìàëüíûå çíà÷åíèÿ
int shift; // ñìåùåíèå ìåæäó ìîìåíòîì îïðåäåëåíÿ ýêñòðåìóìà è åãî ïîëîæåíèåì âî âðåìåíè
if(i>0) j++; // åñëè áàð çàâåðøåí, òî èíêðåìåíò ñ÷åò÷èêà ñìåùåíèÿ
if(dir) // ëîâëÿ ïèêà
{
if(h<High[i]) {h=High[i]; j=NewBar;} // òåêóùèé ìàêñèìóì; ñáðîñ ñ÷åò÷èêà ñìåùåíèÿ
if(SAR[i+1]<=mid[1] && SAR[i]>mid[0]) // ïåðåâîðîò Ïàðàáîëèêà âíèç
{
shift=i+ExtremumsShift*(j+NewBar); // ñìåùåíèå
if (shift>=ArraySize(Peak)) break;
Peak[shift]=h; // ïèê
dir=0; // íàïðàâëåíèå âíèç
l=Low[i]; j=0; // òåêóùèé ìàêñèìóì, ñáðîñ ñ÷åò÷èêà ñìåùåíèÿ
}
}
else // ëîâëÿ âïàäèíû
{
if(l>Low[i]) {l=Low[i]; j=NewBar;} // òåêóùèé ìèíèìóì; ñáðîñ ñ÷åò÷èêà ñìåùåíèÿ
if(SAR[i+1]>=mid[1] && SAR[i]<mid[0]) // ïåðåâîðîò Ïàðàáîëèêà ââåðõ
{
shift=i+ExtremumsShift*(j+NewBar); // ñìåùåíèå
if (shift>=ArraySize(Trough)) break;
Trough[shift]=l; // âïàäèíà
dir=1; // íàïðàâëåíèå ââåðõ
h=High[i]; j=0; // òåêóùèé ìàêñèìóì, ñáðîñ ñ÷åò÷èêà ñìåùåíèÿ
}
}
}
// if(MissBars) Print("limit: ",limit," Bars:",Bars," IndicatorCounted: ",IndicatorCounted());
return(0);
}
//=============================================================
// ô-ÿ äîïîëíèòåëüíîé èíèöèàëèçàöèè
int reinit()
{
ArrayInitialize(Peak,0.0);
ArrayInitialize(Trough,0.0);
ArrayInitialize(SAR,0.0);
return(0);
}
Comments