Storing highs and lows with ValueWhen function

Dear Seetu friends.

I have been testing the ValueWhen function and could successfully store the high and low of a candle after a certain trigger.

The issue is that, after prices go over or below the stored high and low (which is a buy or sell), I need the variable (which had the high and low stored) to become with 0 value again. If I do not do that, the buy and sell can be triggered again after several candles.

Simplified example of what I am trying to do:

===
MaxCandle = valuewhen(close > CalcEMA(close,9), high);
MinCandle = valuewhen(close > CalcEMA(close,9), low);

buy = close > MaxCandle;
Sell = close < MinCandle;

if (buy==1 or sell ==1) { MaxCandle = 0; MinCandle = 0; }

===

In the explore tab, I can see that MaxCandle and MinCandle still keeps the high and low after buy or sell is triggered.

Appreciate your support.

Thank you.
Leandro

Hello,

if-else statement works with boolean (or a single numeric expression), not with an array.

You need to use a vectorized IIF version:

Buy_or_Sell = buy OR sell;
MaxCandle = iif(Buy_or_Sell,0,MaxCandle);
MinCandle = iif(Buy_or_Sell,0,MinCandle);

If I do not do that, the buy and low can be triggered again after several candles.

But I guess it will not solve the problem.
I think that buy and sell are triggered again because conditions are TRUE again.

Thank you Bob. The condition I have coded triggers very few times (ME9 condition above was just an example), I think your suggestion might work. I will try this out. Thanks!