Hello. Before the question, I live in a non–English-speaking country and am not good at English. I am writing this post with AI assistance, so I ask for your understanding. Because of the language barrier, I have only been reading this forum for a long time without posting. Finally, I encountered a problem I absolutely cannot solve on my own, so I am writing.
Below is part of an ATR ZigZag code I obtained from this forum (I’ve included only the portion relevant to my question; if I have omitted anything important, please let me know).
perBull = perBear = Param( "perBear", 50, 1, 100, 1 );
multBull = multBear = Param( "multBear", 10, 1, 20, 1 );
bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );
sup = C - multBull * ATR( perBull );
res = C + multBear * ATR( perBear );
trailArray = Null;
trailstop = 0;
for( i = 1; i < BarCount; i++ )
{
if( C[ i ] > trailstop AND C[ i - 1 ] > trailstop )
trailstop = Max( trailstop, sup[ i ] );
else
if( C[ i ] < trailstop AND C[ i - 1 ] < trailstop )
trailstop = Min( trailstop, res[ i ] );
else
trailstop = IIf( C[ i ] > trailstop, sup[ i ], res[ i ] );
trailArray[ i ] = trailstop;
}
dntrend = dt = IIf( trailArray > C, trailArray, Null );
uptrend = ut = IIf( trailArray < C, trailArray, Null );
lll = LLV( L, BarsSince( !IsEmpty( ut ) ) );
Plot(lll,"lll",ParamColor("lll",colorCustom11),ParamStyle("lll"));
lll = IIf( dt, lll, Null );
trm = dt AND L == lll;
hhh = HHV( H, BarsSince( !IsEmpty( dt ) ) );
Plot(hhh,"hhh",ParamColor("hhh",colorBrightGreen),ParamStyle("hhh"));
hhh = IIf( ut, hhh, Null );
pkm = ut AND H == hhh;
And here is an image of the chart showing the problematic plot. The green line is hhh, and the teal (sky-blue) line is lll. The captured section is the very beginning of the data, and the same behavior occurs on any other symbol.
The issue occurs in this line of code:
hhh = HHV( H, BarsSince( !IsEmpty( dt ) ) );
When BarsSince( !IsEmpty( dt ) ) returns 0, I expect hhh to return the highest high from the very first bar up to the current bar. However, when I plot it, it returns the highest high starting from the bar where dt first becomes non-empty, rather than from the very first bar.
At first I thought there might be an intentional reason, so I analyzed the code in various ways but could not understand why it behaves this way.
Finally (though I should have done it earlier), I also plotted and printed
lll = LLV( L, BarsSince( !IsEmpty( ut ) ) );
under the same conditions. In that case, lll behaves as I expect: when BarsSince( !IsEmpty( ut ) ) is 0, it functions exactly like LLV(L, 0).
From this, I have drawn two possible conclusions:
-
The combination of
HHVandBarsSincetriggers a bug. -
There is a flaw in my programming knowledge that I do not understand.
I have spent quite a long time pondering this, but I still cannot figure out why this occurs or how to fix it. Please help…
One more note: I know that in the actual code, hhh and lll are further processed with
hhh = IIf( ut, hhh, Null );
so ultimately this apparent error does not affect the code’s outcome. However, I am interested only in the issue caused by HHV( H, BarsSince( !IsEmpty( dt ) ) ) itself.

