How to require a sequence of conditions?


Category:
0
0

Hi.  I have just started working with ThinkScript, thanks to Hahn-Tech, so thank you all for that.

I started with a VWAP crossover script, modified it to detect VWAP at upper/lower bands. What I would like to learn how to do next is write a script that alerts only after several conditions have occured.

For instance: require price to cross over VWAP, price to cross over upper/lower VWAP band and then price to cross back across VWAP upper/lower band.  How would this be done?

I can see how to do it with a more-traditional programming language, by using a boolean array and then setting the array values to true as they occur and checking the values but when it comes to ThinkScript I have no idea. A pointer to a script that does something similar would be a great help.

Thank you.

Marked as spam
Posted by (Questions: 1, Answers: 1)
Asked on September 28, 2020 9:49 am
270 views
1
Private answer

The solution requires that you first have a starting point. Some event or time on the chart in which the conditions are set to zero. The following line creates a variable that is true on the first bar of each new trading session.

def newDay = GetDay() <> GetDay()[1];

Then we need to use recursive variables to keep track of events that occur AFTER the reset point. I will use short-hand notation to retrieve the default plot values from the VWAP  study, then apply recursive variables to keep track of two of the events you listed.

def newDay = GetDay() <> GetDay()[1];
def myVwap = reference VWAP().VWAP;
def vwapUpper = reference VWAP().UpperBand;
def vwapLower = reference VWAP().LowerBand;
rec trackCrossAboveVwap = if newDay then 0 else if close[1] < myVwap[1] and close > myVwap then 1 else trackCrossAboveVwap[1];
rec trackCrossAboveUpper = if newDay then 0 else if trackCrossAboveVwap and close[1] < vwapUpper and close > vwapUpper then 1 else trackCrossAboveUpper[1];
plot signal = trackCrossAboveUpper and !trackCrossAboveUpper[1];

Each new trading session the values are rest to zero. Once the price crosses above the VWAP the code then keeps track that the VWAP cross has already happened once in the current trading session (excluding the opening bar of the session). That's the purpose of the first recursive variable. The second recursive variable looks for price crossing above the upper band of the VWAP, but requires the price had previously crossed above the VWAP plot at any previous point of the current trading session, including the current bar.

That's all I have time for. There are actually tons of examples spread throughout the forum. But they are tough to find because they don't really state it as clearly as you did "How to require a sequence of conditions?"

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on September 28, 2020 4:37 pm
0
Thank you Pete. This is great to have as a starting point. I really appreciate it.
( at September 29, 2020 2:10 pm)