What the Lowest and Highest functions return - page 2

 
nen
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.
 
Vladislav, for some reason your code does not work on all timeframes. It works in some timeframes, but not in others. It's strange. It works.
 
Rosh, here is the code from codebase.mql4.com. I have the same one. If it is possible to overcome it with the whole world, I will be very grateful. I won't be the only one. Thank you in advance for your help.

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;
}
}
}
 
Vladislav, for some reason your code does not work on all timeframes. It works in some timeframes, but not in others. It's strange. It works. <br / translate="no">


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.
 
Vladislav, it is clear that this is not your code. I have the code I provided in my delivery.
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 have it displayed on all t\f on the specified pairs. I don't remember where this code came from - I thought it was in the 197 build delivery - I have it in all MTs for some reason :). I'll check it tomorrow.
 
Ashamed to say I've never been seriously interested in zigzags before :). The code in nen's post 18.10.06 17:46
I can't figure it out yet. It seems to me it should be something like .
.. // val=High[Highest(NULL,0,MODE_HIGH,ExtDepth,shift)]; highpos=Highest(NULL,0,MODE_HIGH,ExtDepth,shift); val=High[highpos]; if(MathAbs(val-lastlow) < Point) val=0.0; // if(val==lasthigh) val=0.0; else { lasthigh=val; if(highpos!=shift) val=0.0; // if((val-High[shift])>(ExtDeviation*Point)) val=0.0; else { ...


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.

 
Candid, thank you. I'll see how it works. But the question in the thread title doesn't go away. Just zigzagging has revealed the problem of "incomprehensible" work of functions searching for extrema. The line highpos=Highest(NULL,0,MODE_HIGH,ExtDepth,shift); helps find an extremum. However, the highpos index returned by the Highest function when running in the zigzag code is often random. There is a deviation from the true bar position. In other words, highpos will be, for example, 15. But in fact, the bar will have a different index: 13 or 16 or something else. So you get the difference. Therefore, the zigzag with small parameter values and on "small" timeframes (1-minute, 5-minute ...) starts working unstable.

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.
 
Candid, your words: Ashamed to say, ..... The code in nen's post 18.10.06 17:46 has yet to be understood.

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.
 
2 nen:
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?
Reason: