XLab_ZZ
Download scriptThis tool is designed to identify significant structural shifts in price movement by plotting a "ZigZag" pattern on your trading chart. Instead of showing every minor fluctuation, it filters out market noise to highlight the primary highs and lows, helping traders visualize the overall trend and key reversal points.
Core Logic and Behavior
The script operates by continuously monitoring price action and comparing current movements against previous peaks and valleys. Here is how it processes the data:
1. Trend Identification The tool tracks whether the market is currently in an "Up" or "Down" phase. It doesn't just look at whether the price is higher or lower than the previous candle; it looks for structural confirmation to decide when a trend has actually shifted.
2. Dynamic Peak and Valley Tracking
- In an Upward Trend: The script keeps track of the highest point reached. If the price moves higher than the current recorded peak, the script updates that peak to the new, higher level.
- In a Downward Trend: Conversely, if the market is moving down, the script tracks the lowest point. If the price drops lower than the previous recorded valley, it updates that low point.
3. The "Channel Points" Filter A key feature of this logic is the use of a sensitivity threshold (defined by "ChannelPoints"). This acts as a buffer to prevent the zigzag from reacting to tiny, insignificant price ticks. A reversal in trend direction is only officially registered when the price moves significantly enough away from the previous peak or valley to exceed this specific point threshold. This ensures that the lines drawn on your chart represent meaningful structural changes rather than minor market "jitter."
4. Real-Time Processing The script analyzes historical data to build the initial structure and then updates in real-time as new price information arrives. It is designed to "re-evaluate" points dynamically: if a new high or low is formed that surpasses a previous one before a reversal is confirmed, the script adjusts the coordinates to ensure the most accurate peak is displayed.
Visual Representation
On your chart, this logic results in a continuous series of connected lines (a ZigZag).
- When the market makes a higher high, the line extends upward.
- When the price drops enough to trigger a reversal based on your settings, the line turns and heads downward to mark a new low.
- The result is a clean, streamlined path that illustrates the "skeleton" of the price action, making it easier to identify support, resistance, and major trend changes at a glance.
//+------------------------------------------------------------------+
//| ZZP.mq4 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2012, TheXpert"
#property link "theforexpert@gmail.com"
#property indicator_chart_window
extern int ChannelPoints = 100;
// attention! when used, this option can lead to incorrect visualization of last ZZ ray at 0 bar
// incorrect visualization will be redrawn when new bar appears
// use it carefully and don't expect complete correctness
// never(!) use this indicator values from 0 bar in your automated strategies.
// never(!) use this option if this indicator is used in automated strategy.
// if used, this option can lead to incorrect extremum parsing in your EA
/* extern */ bool DrawZeroBar = false;
#property indicator_buffers 5
#property indicator_color1 LightGray
#property indicator_color2 LightGray
double Maxs[];
double Mins[];
double Direction[];
double MaxTime[];
double MinTime[];
string symbol;
#define UP 0.0001
#define DN -0.0001
#define NONE 0
bool Inited;
datetime LastTime;
int init()
{
SetIndexBuffer(0, Maxs);
SetIndexBuffer(1, Mins);
SetIndexBuffer(2, MinTime);
SetIndexBuffer(3, MaxTime);
SetIndexBuffer(4, Direction);
SetIndexStyle(0, DRAW_ZIGZAG);
SetIndexStyle(1, DRAW_ZIGZAG);
SetIndexStyle(2, DRAW_NONE);
SetIndexStyle(3, DRAW_NONE);
SetIndexStyle(4, DRAW_NONE);
symbol = Symbol();
IndicatorDigits(Digits);
Inited = false;
LastTime = 0;
return(0);
}
int start()
{
if (!Inited)
{
ArrayInitialize(Mins, EMPTY_VALUE);
ArrayInitialize(Maxs, EMPTY_VALUE);
ArrayInitialize(MinTime, EMPTY_VALUE);
ArrayInitialize(MaxTime, EMPTY_VALUE);
ArrayInitialize(Direction, EMPTY_VALUE);
}
int ToCount = Bars - IndicatorCounted();
// DON'T fix to >= 0. For the 0 bar the logic is in DrawZeroBar block
for (int i = ToCount - 1; i > 0; i--)
{
if (i < Bars - 1)
{
MinTime[i] = MinTime[i + 1];
MaxTime[i] = MaxTime[i + 1];
Direction[i] = Direction[i + 1];
Maxs[i] = EMPTY_VALUE;
Mins[i] = EMPTY_VALUE;
}
Check(i);
}
if (DrawZeroBar)
{
if (LastTime != Time[0])
{
LastTime = Time[0];
MinTime[0] = MinTime[1];
MaxTime[0] = MaxTime[1];
Direction[0] = Direction[1];
Maxs[0] = EMPTY_VALUE;
Mins[0] = EMPTY_VALUE;
}
CheckPrice(0, Close[0]);
}
else
{
Maxs[0] = EMPTY_VALUE;
Mins[0] = EMPTY_VALUE;
}
return(0);
}
void Check(int offset)
{
double o = Open[offset];
double h = High[offset];
double l = Low[offset];
double c = Close[offset];
if (c < o)
{
CheckPrice(offset, h);
CheckPrice(offset, l);
}
else
{
CheckPrice(offset, l);
CheckPrice(offset, h);
}
}
void CheckPrice(int offset, double p)
{
if (!Inited)
{
CheckInit(offset, p);
Inited = true;
}
else
{
if (Direction[offset] == UP) CheckUp(offset, p);
else if (Direction[offset] == DN) CheckDn(offset, p);
}
}
void CheckInit(int offset, double c)
{
MaxTime[offset] = Time[offset];
MinTime[offset] = Time[offset];
Maxs[offset] = c;
Mins[offset] = c;
Direction[offset] = UP;
}
void CheckUp(int offset, double c)
{
int lastOffset = iBarShift(symbol, 0, MaxTime[offset]);
if (Maxs[lastOffset] == EMPTY_VALUE)
{
Maxs[lastOffset] = High[lastOffset];
}
if (c > Maxs[lastOffset])
{
Maxs[lastOffset] = EMPTY_VALUE;
Maxs[offset] = c;
MaxTime[offset] = Time[offset];
lastOffset = offset;
}
if (c + ChannelPoints*Point < Maxs[lastOffset])
{
Direction[offset] = DN;
Mins[offset] = c;
MinTime[offset] = Time[offset];
}
}
void CheckDn(int offset, double c)
{
int lastOffset = iBarShift(symbol, 0, MinTime[offset]);
if (Mins[lastOffset] == EMPTY_VALUE)
{
Mins[lastOffset] = Low[lastOffset];
}
if (c < Mins[lastOffset])
{
Mins[lastOffset] = EMPTY_VALUE;
Mins[offset] = c;
MinTime[offset] = Time[offset];
lastOffset = offset;
}
if (c - ChannelPoints*Point > Mins[lastOffset])
{
Direction[offset] = UP;
Maxs[offset] = c;
MaxTime[offset] = Time[offset];
}
}
Comments