Static value when condition is met

Hello Experts,

Is there any way for storing a value in a variable which changes dynamically. I will explain in brief.

For suppose, I want to store current price in a variable "LTP" for static, when a condition is met. Like when RSI == 60, i want the the market price at that time stored in LTP statically. It should not change. The value should change again when the condition is met.

Is this possible?

Thank You in Advance.

@fxshrat, please help.

Use ValueWhen function.

LTP = ValueWhen(RSI() == 60, C);

Now, it is highly unlikely that RSI() would be exactly equal to value of 60.
So what you probably mean is when it crosses 60 upwards or downwards.

my_rsi = RSI();
rsi_value = 60;
up_cross = Cross(my_rsi, rsi_value);
dn_cross = Cross(rsi_value, my_rsi);
crss = up_cross OR dn_cross;
LTP = ValueWhen(crss, C);

Or use AlmostEqual() to approach from above.

For approaching from either above or below you would have to adjust is_almost_there.
To store price at approach

my_rsi = RSI();
rsi_value = 60;
percent = 0.2;// approach range in %

is_almost_there = AlmostEqual(my_rsi, rsi_value, percent*1e5) AND 
					(my_rsi > rsi_value OR my_rsi < rsi_value);

LTP = ValueWhen(is_almost_there, C);

Plot(C, "Price", colorDefault, styleBar);
Plot(LTP, "LTP", colorRed );

Thank You so much @fxshrat. You solved it!

Thanks Again.

1 Like

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