High_Low v2 (ZigZag)
Download scriptThis script is designed as a visual trading tool for the MetaTrader platform, specifically crafted to identify and map out significant structural turning points in price action. By analyzing historical data, it filters out minor market "noise" to highlight the most important peaks and valleys.
Core Logic Overview
The primary objective of this script is to trace the "skeleton" of a price movement. Instead of showing every single tick or small fluctuation, it looks for distinct swings—moments where the market direction changes significantly. It achieves this by evaluating price data over a specific window of time (defined by the user) to determine whether a point is a legitimate high or low relative to its immediate surroundings.
How it Works
The script follows a systematic process to filter and draw these points:
- Neighborhood Analysis: The tool examines a fixed number of previous candles (the "Length" parameter). For every point on the chart, it checks if the current price is higher or lower than the extremes found within that neighborhood.
- Swing Identification:
- It identifies High Swings when the price reaches a peak that exceeds the surrounding high points.
- It identifies Low Swings when the price drops to a trough that falls below the surrounding low points.
- If a point is higher than both the recent highs and lows, it is flagged as a significant "swing" point where trend reversal may be occurring.
- Trend Tracking: As the script moves backward through history, it keeps track of the most recent major high and low. It updates these values dynamically; if a new price point exceeds the current recorded high during an upward move, it "refreshes" that peak to ensure the line stays at the absolute extremity.
- Data Mapping: Once a confirmed swing is identified (either a peak or a valley), the script stores the coordinates of that point. It then draws a continuous section on your chart connecting these major points.
Visual Result
For the user, this means the chart is populated with a clean line that connects major market highs and lows. It provides a clear visual representation of the current trend, making it easier to see the overall structure of the market—helping traders identify support levels, resistance zones, and potential reversal areas without being distracted by minor price fluctuations.
//+------------------------------------------------------------------+
//| High_Low v2 (ZigZag).mq4 |
//| rewritten by CrazyChart |
//| |
//+------------------------------------------------------------------+
#property copyright "rewritten by CrazyChart"
#property link ""
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Blue
//---- input parameters
extern int barn=300;
extern int Length=6;
//---- buffers
double ExtMapBuffer1[];
//double ExtMapBuffer2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexEmptyValue(0,0.0);
//SetIndexDrawBegin(0, barn);
SetIndexStyle(0,DRAW_SECTION);
SetIndexBuffer(0,ExtMapBuffer1);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars=IndicatorCounted();
int shift,Swing,Swing_n,uzl,i,zu,zd,mv;
double LL,HH,BH,BL,NH,NL;
double Uzel[10000][3];
string text;
// loop from first bar to current bar (with shift=0)
Swing_n=0;Swing=0;uzl=0;
BH =High[barn];BL=Low[barn];zu=barn;zd=barn;
for (shift=barn;shift>=0;shift--) {
LL=10000000;HH=-100000000;
for (i=shift+Length;i>=shift+1;i--) {
if (Low[i]< LL) {LL=Low[i];}
if (High[i]>HH) {HH=High[i];}
}
if (Low[shift]<LL && High[shift]>HH){
Swing=2;
if (Swing_n==1) {zu=shift+1;}
if (Swing_n==-1) {zd=shift+1;}
} else {
if (Low[shift]<LL) {Swing=-1;}
if (High[shift]>HH) {Swing=1;}
}
if (Swing!=Swing_n && Swing_n!=0) {
if (Swing==2) {
Swing=-Swing_n;BH = High[shift];BL = Low[shift];
}
uzl=uzl+1;
if (Swing==1) {
Uzel[uzl][1]=zd;
Uzel[uzl][2]=BL;
}
if (Swing==-1) {
Uzel[uzl][1]=zu;
Uzel[uzl][2]=BH;
}
BH = High[shift];
BL = Low[shift];
}
if (Swing==1) {
if (High[shift]>=BH) {BH=High[shift];zu=shift;}}
if (Swing==-1) {
if (Low[shift]<=BL) {BL=Low[shift]; zd=shift;}}
Swing_n=Swing;
}
for (i=1;i<=uzl;i++) {
//text=DoubleToStr(Uzel[i][1],0);
//text=;
mv=StrToInteger(DoubleToStr(Uzel[i][1],0));
ExtMapBuffer1[mv]=Uzel[i][2];
}
return(0);
}
//+------------------------------------------------------------------+
Comments