Is the HHV Value Returned by This Code a Bug?

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:

  1. The combination of HHV and BarsSince triggers a bug.

  2. 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.

You should be using HighestSince , LowestSince functions.

1 Like

Thank you. Following your advice, I changed the code to

hhh = HighestSince( !IsEmpty( dt ), H ); 

and tested it, but the exact same issue occurs (it seems to behave exactly like the original code).
Is there another way I should be using HighestSince?

I still have a lot to learn, so I’m having trouble resolving this.

I still have the same issue with null values at the beginning of the data that existed before.

And HighestSince seems to work a bit differently from what I want.

In the case of HHV( x, 0 ), it returns the high from the very beginning of the chart, but HighestSince seems to not support 0 as the third argument and treats it as 1.

What part did I make a mistake in?

No it is not a bug, it is misuse of functions.

The "period" parameter can't be ZERO. Period is a "number of bars" to perform calculation on. The period MUST BE POSITIVE (i.e. greater than 0).

If you want to treat zero as 1, just ADD ONE

what_you_want = HHV( x, period + 1 );
1 Like

If you're just trying to get the values at the beginning of the array populated with non-null values, you could try something like this:

bi = BarIndex();
hhh = HighestSince( !IsEmpty( dt ) OR bi == 0, H ); 
1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.