Alert on close crossing above or below VWAP


Category:
0
0

How to set an alert in TOS when price bar closes over or under VWAP?

 

Marked as spam
Posted by (Questions: 2, Answers: 3)
Asked on September 26, 2018 12:29 pm
3986 views
0

This can be built using the condition wizard. No need to write any lines of code at all.
Did you really want to be alerted for every single bar that closed above the VWAP and every single bar that closes below it? Because that’s how your request is structured. Really makes little sense at all. Since every single bar on the chart will be triggering an alert. Also, did you want this to be just a chart study alert? Or did you have plans for receiving text/email notifications instead?

( at September 26, 2018 1:42 pm)
0

I should have termed it as a cross, i.e., previous price bar closes below the VWAP and then next price bar closes above the VWAP. No alert until next price bar ends. Alert comes as a sound. A chart arrow (up/down) would be useful. Just like the “show breakout signals” feature for closes above/below moving averages in TOS.

( at September 27, 2018 7:30 am)
0
Private answer

After receiving further details I have updated the title of the question to reflect the context of the request. I have also moved this to the Chart Studies topic.

So based on the clarifications, what we have in the following code is a Chart Study that plots the VWAP study. (exactly as the built-in study provided by Thinkorswim). It displays up and down arrows at places where the closing price of a candle crosses above or below the VWAP plot. Alerts are also included, and per the request the alerts will not trigger until the candle that crosses the VWAP plot has closed and the next bar has opened.

Here is the full code. Screenshot below shows the result.

input numDevDn = -2.0;
input numDevUp = 2.0;
input timeFrame = {default DAY, WEEK, MONTH};
def cap = getAggregationPeriod();
def errorInAggregation =
timeFrame == timeFrame.DAY and cap >= AggregationPeriod.WEEK or
timeFrame == timeFrame.WEEK and cap >= AggregationPeriod.MONTH;
assert(!errorInAggregation, "timeFrame should be not less than current chart aggregation period");
def yyyyMmDd = getYyyyMmDd();
def periodIndx;
switch (timeFrame) {
case DAY:
periodIndx = yyyyMmDd;
case WEEK:
periodIndx = Floor((daysFromDate(first(yyyyMmDd)) + getDayOfWeek(first(yyyyMmDd))) / 7);
case MONTH:
periodIndx = roundDown(yyyyMmDd / 100, 0);
}
def isPeriodRolled = compoundValue(1, periodIndx != periodIndx[1], yes);
def volumeSum;
def volumeVwapSum;
def volumeVwap2Sum;
if (isPeriodRolled) {
volumeSum = volume;
volumeVwapSum = volume * vwap;
volumeVwap2Sum = volume * Sqr(vwap);
} else {
volumeSum = compoundValue(1, volumeSum[1] + volume, volume);
volumeVwapSum = compoundValue(1, volumeVwapSum[1] + volume * vwap, volume * vwap);
volumeVwap2Sum = compoundValue(1, volumeVwap2Sum[1] + volume * Sqr(vwap), volume * Sqr(vwap));
}
def price = volumeVwapSum / volumeSum;
def deviation = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(price), 0));
plot vwapValue = price;
plot upperBand = price + numDevUp * deviation;
plot lowerBand = price + numDevDn * deviation;
plot crossingAbove = close[1] < vwapValue[1] and close > vwapValue;
crossingAbove.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
crossingAbove.SetDefaultColor(Color.CYAN);
crossingAbove.SetLineWeight(3);
plot crossingBelow = close[1] > vwapValue[1] and close < vwapValue;
crossingBelow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
crossingBelow.SetDefaultColor(Color.MAGENTA);
crossingBelow.SetLineWeight(3);
Alert(crossingAbove[1], "Cross Above VWAP", Alert.BAR, Sound.RING);
Alert(crossingBelow[1], "Cross Below VWAP", Alert.BAR, Sound.RING);

 

Attachments:
Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on September 27, 2018 8:02 am
0

Outstanding!

( at September 27, 2018 9:14 am)
0

This works like a charm. I would like to incorporate it into a watchlist on a 1min and 5 min basis. A cluster of 1min up/down arrows at the VWAP often helps identify a range tradable for breakouts. I wonder if the TOS platform would be able to handle the workload on say 7 or 8 assets? I’m guessing I could add the “crossing vwap” alert indicator to a watchlist using the “lookup a column”…”custom quotes” field. Is that correct?

( at September 28, 2018 10:58 am)
0

1 min time frames on a watchlist column is problematic. All the processing is handled on Thinkorswim servers, rather than locally. It is therefore subject to delays and processing time can increase under active market conditions.

You will be able to take this code and add it to a custom quote in a watchlist. However will need to remove the alert statements from the code as those are not supported in a watchlist. You will also need to flip all but one of the plot statements to def. And remove the style statements associated with all plots. If you are not savvy with the code this can be a very complicated and error prone process.

You have hinted that you find value in detecting repeat crosses on a specific time frame. This is a pattern we can write into the code. But it’s much more complex than anything we would post in the free Q&A forum.

( at September 28, 2018 11:48 am)