![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Couldn't reply (I was away). Post the code you need to parse to talk about one thing.
ZS In that zigzag was not an error but a typo (compared to the code in MT3), I found only this typo and did not correct anything else.
I have now accumulated a large number of those or other edits. They work quite well. However, the first ray - the one starting from the zero bar or any of the first bars - fails with errors in functions of identifying extrema. And the indicator works unstably.
//+------------------------------------------------------------------+
//| Custom Moving Average.mq4 |
//| Copyright © 2005, MetaQuotes Software Corp.
//| https://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link "https://www.metaquotes.net/
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red
//---- indicator parameters
extern intDepth=12;
extern inttern ExtDeviation=5;
extern inttern ExtBackstep=3;
//---- indicator buffers
double ExtMapBuffer[];
double ExtMapBuffer2[];
//+------------------------------------------------------------------+
//| Custom indicator initialisation function |
//+------------------------------------------------------------------+
int init()
{
IndicatorBuffers(2);
//---- drawing settings
SetIndexStyle(0,DRAW_SECTION);
//---- indicator buffers mapping
SetIndexBuffer(0,ExtMapBuffer);
SetIndexBuffer(1,ExtMapBuffer2);
SetIndexEmptyValue(0,0.0);
//---- indicator short name
IndicatorShortName("ZigZag("+ExtDepth+", "+ExtDeviation+", "+ExtBackstep+")");
//---- initialization done
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
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;
}
}
}
It works on other timeframes but not on others. It seems to work for me. Yes, and it's not my code - from the standard supply seems to be. At least I have in all versions of MT.
On which timeframes it is not displayed. BC - Brezan.
GBP-CHF
It is not shown on m1 and m15. It is shown on all the others.
EUR-USD
It is not shown - m1, m5
AUD-USD - all of them are shown.
I do not know why it appears so.
Perhaps, you have the code which Slava reworked in summer. But I didn't like his rework. Some problems were not solved there.
================
GODZILLA (Nikolay) did a good job. But it has only two problems solved.
1) Recalculation on every tick. He drastically reduced it
2) Added a hump removal algorithm. Humps mainly appear due to presence of both minimum and maximum on one bar. The zigzag algorithm leaves only highs. This often results in making several highs in a row.
I have some variants of indicator improvement fixed by GODZILL. The kinks hanging in the air have been removed. However, I suppose the incorrect operation of the functions of extremum search still persists in the first two rays. It is because of this problem that I brought up this topic here.
However, perhaps the search function works correctly but there is no clear description of this function. This is why it is used incorrectly.
I can't figure it out yet. It seems to me it should be something like .
Here the 'extra' old code is commented out. Although I may not fully understand the zigzag idea yet. In any case this code does not produce dangling extrema. If this code variant fits but it was not invented by me :), I apologize for lack of reference.
Alright. Never mind the zigzag. The question is not about the zigzag. The question is that functions of MQ4 language should work steadily and predictably.
And this IS the MAIN PROBLEM. The search functions are used not only in the ZigZag. But also in many other indicators.
The zigzag is a special case. But it helps to highlight the problem. Many indicators are based on zigzags. The zigzag works unstable. It is not a secret for anyone. Many people have to spend their time on programming their own zigzags for their own needs. How much time is wasted. In vain. Because of a lack of understanding of the root of the problem.
Those are golden words. You are not the first to say it. Nevertheless, the ideas behind the zigzag and partially implemented in this code are very good. These ideas must be brought to fruition.
When we move the window (shift,shift+ExtDepth) while calculating the indicator, the appearance of a new extremum may be related both to the new price and to the fact that the old extremum has left the window. This point needs to be handled. For this purpose, my insertion contains the line if(highpos!=shift) val=0.0; . How this is done in the standard code, I don't understand. Judging by missing dangling extrema in my version, it is either done incorrectly or not at all.
And what exactly is the instability of the zigzag?