VALUEWHEN - seems to ignore the expression

Hi all, any ideas why my ValueWhen statement 'appears to me to' always return the Array value, regardless of the Expression? My code is below and in my screenshot, you will see the values of the PivotTrue array which are occasionally==1, so why is the LOW value always returned?

size=20;
LeftPeriodLow = LLV(L,Size+1); // LLv of x bars to the left (plus current bar)
RightPeriodLow = Ref(LeftPeriodLow,Size); // LLv of x bars to the right
BI=BarIndex();
PivotTrue = LeftPeriodLow == Low AND RightPeriodLow == Low AND bi+1<(BarCount-Size); // creates an array of TRUE/FALSE at each pivot location

Pivots=0;
Pivots = ValueWhen(PivotTrue==1,Low); //creates an array of Pivot LOWs 

x=1;

screenshot
Thanks for you help in advance...

regards, Tim..

That's what 'ValueWhen' does - returns array. See ValueWhen.

1 Like

Hi Trendsurfer, thanks for your reply. Hmmm. I had previously read the link you provided but I interpreted it differently.

Is it then the case that when the expression is false, it will return the array value of when it was last true.

ValueWhen keeps true state till next change to true. So if there is a true state on a bar then its Low (that you inserted as 2nd argument of ValueWhen) is kept for all following bars not having true state. If there is a new true state then that one's Low is chosen next.

Don't know what you are trying to do but If you want to return Low only when there is TRUE at bar then use IIf function instead.

E.g.

Pivots = IIf(PivotTrue, Low, Null); //creates an array of Pivot LOWs 

PlotShapes( PivotTrue * shapeSmallSquare, colorRed, layer = 0, y = Pivots, -12 );

So in upper IIf example Low is available only at bar where PivotTrue is true else Null is returned (if false at bar).

5 Likes

Thanks again fxshrat. That's really helpful. It would be handy to add that to the reference guide explanation.
regards, Tim.

This is not new knowledge - this has been explained over and over again in pretty much the very same words in a number of places including but not limited to this detailed thread:

and this Knowledge Base article written 4 years ago (2014):

http://www.amibroker.com/kb/2014/09/29/debugging-techniques-part-1-exploration/

Let me quote

If you run above code you will clearly see how ValueWhen picks the value when condition is true and “holds” it for all other bars (when condition is false).

And in this forum thread:

and also here: Convert Metastock indicator to AFL

Really, everything is already described in great detail. All you need to do is to use Google search.

If you used Google this way you would end up with the answers in no-time.

Just type your search term + amibroker into Google and look at 2-3 top answers.

https://www.google.com/search?q=valuewhen+amibroker

and obviously the forum search

https://forum.amibroker.com/search?q=ValueWhen

there is even valuewhen tag on the forum: https://forum.amibroker.com/tags/valuewhen

3 Likes