How do I make a variable true until something else happens?


Category:
0
0

I need help with some code logic. suppose x is the high and y is the low of an indicator. how do I make a variable true if “close crosses above x” and false when “close crosses below y”? The problem if I use “close crosses above x” is that it will only be true for 1 bar and then turn false since it is recalculated at every bar. If I do “close > x” the logic will be wrong when the close is between x and y because once the close crosses x I want it to stay true until it closes below y. I can’t use “close > x and close > y” because once the close falls below x the statement still still turn false. if I do “close > x or close > y” then the problem is I will get a true condition too early. if there was a global variable of some sort that I could manually assign then I wouldn’t have any issues, but because the thinkscript checks conditions every bar, I can’t get my logic to work. Your help is appreciated. Thanks!

 

to summarize:

  • close crosses above x = 1 bar only
  • close > x = condition turns false too soon
  • close > x and close > y = condition still turns false too soon because both statements must be true.
  • close > x or close > y = condition turns true too early
Marked as spam
Posted by (Questions: 1, Answers: 0)
Asked on March 29, 2019 2:58 am
79 views
0
Private answer

The solution is to use recursive variables. These are variables which test for a specific condition and return a value that is dependent on a previous value of that same variable. However it hurt my brain trying to follow to decipher your conditions. A screenshot showing these conditions clearly on a chart will help me understand exactly what you are trying to achieve.

Marked as spam
Posted by (Questions: 37, Answers: 4087)
Answered on April 6, 2019 9:36 am