Column to show number of bars above or below VWAP


Category:
0
0

Hi Pete,

I’m looking to add a custom column to show the number of days the stock had been either above/below after a VWAP crossover. Ex: A bullish cross would show previous days below and vice versa for a bearish cross.

Thanks again!

Marked as spam
Posted by (Questions: 20, Answers: 27)
Asked on May 16, 2020 8:42 pm
170 views
0
Private answer

You used the word days but in reality this measures bars. The only time this would measure actual days is if you set the custom column to daily time frame. But for most users, the VWAP chart study is used on an intraday basis, so the count is going to show the number of bars above or below the VWAP and not the number of "days".

Please note in this solution I am breaking from my typical policy of using the full source code from the built-in chart study. The reason for this is that code is well over 24 lines so in this case I will reference the study rather than include the full source code from the original. This is less than ideal so for any solution that requires absolute control over the end result I always recommend using the full source code from the original rather than just a reference to the chart study.

input numDevDn = -2.0;
input numDevUp = 2.0;
input timeFrame = {default DAY, WEEK, MONTH};
def vwapValue = reference VWAP(numDevDn, numDevUp, timeFrame).VWAP;
def crossAbove = close[1] < vwapValue[1] and close > vwapValue;
def crossBelow = close[1] > vwapValue[1] and close < vwapValue; rec countBarsAbove = if crossAbove then 1 else if close > vwapValue then countBarsAbove[1] + 1 else 0;
rec countBarsBelow = if crossBelow then -1 else if close < vwapValue then countBarsBelow[1] - 1 else 0; plot data = if countBarsAbove > -countBarsBelow then countBarsAbove else countBarsBelow;
data.AssignValueColor(if data <> 0 then Color.BLACK else Color.CURRENT);
AssignBackgroundColor(if data > 0 then Color.GREEN else if data < 0 then Color.RED else Color.CURRENT);

Marked as spam
Posted by (Questions: 37, Answers: 4079)
Answered on May 17, 2020 7:44 am
0
Thank you so much again Pete! My attempt wasn't even close to your usual outstanding work!
( at May 18, 2020 10:47 am)