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;
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).
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):
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).