I am looking for a code for anchored VWAP with standard deviation bands.
Goals for this code:
-I want an anchored vwap with standard deviation bands.
-I want 2 sets of deviation bands, and to be able to change the deviation numbers in the study settings.
-I also would like the study to give me the option to set an end date where the VWAP would end and not begin a new one, but the default option would be for the study to be ongoing
Hello, I am using the below listed AVWAP indicator and I think it works great. I am curious to find out, if there is anyway to be able to get an alert either via TOS or text or both, when the price of a stock reaches SD 2 or -2. This alert would obviously have to be dynamic, as SD on VWAP moves in relation to price action. Is this even possible?
if usehigher == usehigher.current and GetAggregationPeriod() < AggregationPeriod.DAY or
usehigher == usehigher.higher and agg < AggregationPeriod.DAY {
anchor = if GetYYYYMMDD() == startdate and
SecondsFromTime(starttime)[1] <= 0 and
SecondsFromTime(starttime) >= 0
then bn else anchor[1];
volumesum = if bn >= HighestAll(anchor) then volumesum[1] + volume else 0;
volumevwapsum = if bn >= HighestAll(anchor) then volumevwapsum[1] + volume * vwap else 0;
volumevwap2sum = if bn >= HighestAll(anchor) then volumevwap2sum[1] + volume * Sqr(vwap) else 0;
price = volumevwapsum / volumesum;
deviation = Sqrt(Max(volumevwap2sum / volumesum - Sqr(price), 0));
} else {
anchor = if GetYYYYMMDD() >= startdate then 1 else 0;
volumesum = if anchor then CompoundValue(1, volumesum[1] + v, v) else 0;
volumevwapsum = if anchor then CompoundValue(1, volumevwapsum[1] + v * vw, v * vw) else 0;
volumevwap2sum = if anchor then CompoundValue(1, volumevwap2sum[1] + v * Sqr(vw), v * Sqr(vw))
else 0;
price = volumevwapsum / volumesum;
deviation = Sqrt(Max(volumevwap2sum / volumesum - Sqr(price), 0));
}
input showbubblesline = yes;
input bubblemoverVWAP_Labels = 5;#Hint bubblemoverVWAP_Labels: Number of Spaces bubble offset in expansion area
def n = bubblemoverVWAP_Labels;
def n1 = n + 1;
New to this forum, so please excuse if this is not being posted at the correct location.
I'm simply trying to display VWAP standard deviation values in a watchlist column or on a chart label.
I can use one of the custom fields to show a period VWAP itself as a watchlist column like so:
plot Custom1 = vwap(period = AggregationPeriod.WEEK);
To add the deviation value to VWAP, I tried to mod the built-in VWAP indicator code (pasted below, and it looks like it's been enhanced in some excellent ways over time in this forum). All I'm trying to do is get at, say, 1 standard deviation up for daily VWAP, and simply display the current value (chart label or watchlist column), but the code is above my pay grade in thinkscript. I tried to cancel the user inputs and simplify because I don't need the optionality of choosing timeframes, but I haven't been able to strip the code to simply capture the deviation value, as in "plot UpperBand = price + numDevUp * deviation;" where price = vwap, without errors. I know how to create the chart label once I have the value. Any help would be much appreciated.
New to this forum, so please excuse if this is not being posted at the correct location.
I'm simply trying to display VWAP standard deviation values in a watchlist column or on a chart label.
I can use one of the custom fields to show a period VWAP itself as a watchlist column like so:
plot Custom1 = vwap(period = AggregationPeriod.WEEK);
To add the deviation value to VWAP, I tried to mod the built-in VWAP indicator code (pasted below, and it looks like it's been enhanced in some excellent ways over time in this forum). All I'm trying to do is get at, say, 1 standard deviation up for daily VWAP, and simply display the current value (chart label or watchlist column), but the code is above my pay grade in thinkscript. I tried to cancel the user inputs and simplify because I don't need the optionality of choosing timeframes, but I haven't been able to strip the code to simply capture the deviation value, as in "plot UpperBand = price + numDevUp * deviation;" where price = vwap, without errors. I know how to create the chart label once I have the value. Any help would be much appreciated.
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");
Thank you! This works totally fine as a set of chart labels. However, what do I actually use in thinkscript editor for the watchlist column to display the current value of, say, daily VWAP 1 SD upper band, since the watchlist column can only display a single value?
Tried single statement in custom column definition:
plot Custom1 = reference VWAP("num dev up" = 1.0, "time frame" = "DAY"); # Where it appears "DAY" has to be a string. AggregationPeriod.DAY, etc. does not work.
(This is typically how I do it, btw. There may be better ways, but if I'm looking for, say, the value of 8 EMA as a custom column, I'd simply say:
plot Custom1 = ExpAverage("data" = close, "length" = 8);
then select the aggregation and whether it's RTH or ETH.
Then tried:
plot Custom1 = reference VWAP("num dev dn" = -1.0, "num dev up" = 1.0, "time frame" = "DAY").UpperBand;
This one looks like it should work. But it simply returns the value of VWAP itself (as in reference VWAP() ), not the value of its 1 SD upper band.
Tried the entire script slightly modified to prevent obvious errors, pasted into custom watchlist column definition:
Thank you! This works totally fine as a set of chart labels. However, what do I actually use in thinkscript editor for the watchlist column to display the current value of, say, daily VWAP 1 SD upper band, since the watchlist column can only display a single value?
Tried single statement in custom column definition:
plot Custom1 = reference VWAP("num dev up" = 1.0, "time frame" = "DAY"); # Where it appears "DAY" has to be a string. AggregationPeriod.DAY, etc. does not work.
(This is typically how I do it, btw. There may be better ways, but if I'm looking for, say, the value of 8 EMA as a custom column, I'd simply say:
plot Custom1 = ExpAverage("data" = close, "length" = 8);
then select the aggregation and whether it's RTH or ETH.
Then tried:
plot Custom1 = reference VWAP("num dev dn" = -1.0, "num dev up" = 1.0, "time frame" = "DAY").UpperBand;
This one looks like it should work. But it simply returns the value of VWAP itself (as in reference VWAP() ), not the value of its 1 SD upper band.
Tried the entire script slightly modified to prevent obvious errors, pasted into custom watchlist column definition:
This works. Thank you again. I was able to replicate a single custom watchlist column for the daily VWAP and its bands. For the weekly and monthly, since there is no user input / edit properties dialog in custom columns to toggle the aggregation, I simply changed the timeFrame default order and it works.
I did not realize you can insert more than one value into a single column by using 'astext', nor that you can use AddLabel inside a watchlist column, but new to thinkscript here. This is useful because there are only 19 custom columns and 'stuffing' more data into each one helps. Added an anchored VWAP to the same column.
I export watchlists into Excel for the real computations - it's just a hack to get data out of TOS. With this method, we get multiple values imported into a single Excel cell, but I can use Excel functions (LEFT, RIGHT, MID, etc.) and a delimiter to parse and split the values. Appreciate the help.
This works. Thank you again. I was able to replicate a single custom watchlist column for the daily VWAP and its bands. For the weekly and monthly, since there is no user input / edit properties dialog in custom columns to toggle the aggregation, I simply changed the timeFrame default order and it works.
I did not realize you can insert more than one value into a single column by using 'astext', nor that you can use AddLabel inside a watchlist column, but new to thinkscript here. This is useful because there are only 19 custom columns and 'stuffing' more data into each one helps. Added an anchored VWAP to the same column.
I export watchlists into Excel for the real computations - it's just a hack to get data out of TOS. With this method, we get multiple values imported into a single Excel cell, but I can use Excel functions (LEFT, RIGHT, MID, etc.) and a delimiter to parse and split the values. Appreciate the help.
That's interesting! I did not know you could create more custom columns via share tool. Will definitely look into that shortly.
But yes, all this watchlist business is just to get the data into Excel so I can do the real computations there and distill a lot of data into what is for me tier-1 info. The raw data is too much for the eye.
On that note, one final question. Trying to get 3 pieces of profile data from the daily volume profile into a watchlist column (and from there into Excel). Today's (developing) POC, VA high and VA low - and ideally also last session's POC, VA high and VA low (which I'm hoping might be as easy as the [-1] versions of the former). All I need are the numerical values of these 3 (6) variables in a watchlist column.
I've tried the built-in / official volume profile script pasted below as well as 3 other scripts from this site and elsewhere. I can get past the multiple 'plot' statements erroring out in a watchlist column, but any one of them (e.g. POC, VAH, VAL) returns NaN on the watchlist or else the value is inaccurate regardless of the aggregation I choose for the watchlist column. The script works perfectly fine on a chart and I've confirmed through 2 other platforms that its values are accurate with default settings (time per profile = DAY, VA% = 70, etc).
Code:
#
# TD Ameritrade IP Company, Inc. (c) 2010-2023
#
input pricePerRowHeightMode = {default AUTOMATIC, TICKSIZE, CUSTOM};
input customRowHeight = 1.0;
input timePerProfile = {default CHART, MINUTE, HOUR, DAY, WEEK, MONTH, "OPT EXP", BAR};
input multiplier = 1;
input onExpansion = yes;
input profiles = 1000;
input showPointOfControl = yes;
input showValueArea = yes;
input valueAreaPercent = 70;
input opacity = 50;
def period;
def yyyymmdd = getYyyyMmDd();
def seconds = secondsFromTime(0);
def month = getYear() * 12 + getMonth();
def day_number = daysFromDate(first(yyyymmdd)) + getDayOfWeek(first(yyyymmdd));
def dom = getDayOfMonth(yyyymmdd);
def dow = getDayOfWeek(yyyymmdd - dom + 1);
def expthismonth = (if dow > 5 then 27 else 20) - dow;
def exp_opt = month + (dom > expthismonth);
switch (timePerProfile) {
case CHART:
period = 0;
case MINUTE:
period = floor(seconds / 60 + day_number * 24 * 60);
case HOUR:
period = floor(seconds / 3600 + day_number * 24);
case DAY:
period = countTradingDays(min(first(yyyymmdd), yyyymmdd), yyyymmdd) - 1;
case WEEK:
period = floor(day_number / 7);
case MONTH:
period = floor(month - first(month));
case "OPT EXP":
period = exp_opt - first(exp_opt);
case BAR:
period = barNumber() - 1;
}
def count = CompoundValue(1, if period != period[1] then (count[1] + period - period[1]) % multiplier else count[1], 0);
def cond = count < count[1] + period - period[1];
def height;
switch (pricePerRowHeightMode) {
case AUTOMATIC:
height = PricePerRow.AUTOMATIC;
case TICKSIZE:
height = PricePerRow.TICKSIZE;
case CUSTOM:
height = customRowHeight;
}
profile vol = volumeProfile("startNewProfile" = cond, "onExpansion" = onExpansion, "numberOfProfiles" = profiles, "pricePerRow" = height, "value area percent" = valueAreaPercent);
def con = compoundValue(1, onExpansion, no);
def pc = if IsNaN(vol.getPointOfControl()) and con then pc[1] else vol.getPointOfControl();
def hVA = if IsNaN(vol.getHighestValueArea()) and con then hVA[1] else vol.getHighestValueArea();
def lVA = if IsNaN(vol.getLowestValueArea()) and con then lVA[1] else vol.getLowestValueArea();
def hProfile = if IsNaN(vol.getHighest()) and con then hProfile[1] else vol.getHighest();
def lProfile = if IsNaN(vol.getLowest()) and con then lProfile[1] else vol.getLowest();
def plotsDomain = IsNaN(close) == onExpansion;
plot POC = if plotsDomain then pc else Double.NaN;
plot ProfileHigh = if plotsDomain then hProfile else Double.NaN;
plot ProfileLow = if plotsDomain then lProfile else Double.NaN;
plot VAHigh = if plotsDomain then hVA else Double.NaN;
plot VALow = if plotsDomain then lVA else Double.NaN;
DefineGlobalColor("Profile", GetColor(1));
DefineGlobalColor("Point Of Control", GetColor(5));
DefineGlobalColor("Value Area", GetColor(8));
vol.show(globalColor("Profile"), if showPointOfControl then globalColor("Point Of Control") else color.current, if showValueArea then globalColor("Value Area") else color.current, opacity);
POC.SetDefaultColor(globalColor("Point Of Control"));
POC.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
VAHigh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
VALow.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
VAHigh.SetDefaultColor(globalColor("Value Area"));
VALow.SetDefaultColor(globalColor("Value Area"));
ProfileHigh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ProfileLow.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ProfileHigh.SetDefaultColor(GetColor(3));
ProfileLow.SetDefaultColor(GetColor(3));
ProfileHigh.hide();
ProfileLow.hide();
That's interesting! I did not know you could create more custom columns via share tool. Will definitely look into that shortly.
But yes, all this watchlist business is just to get the data into Excel so I can do the real computations there and distill a lot of data into what is for me tier-1 info. The raw data is too much for the eye.
On that note, one final question. Trying to get 3 pieces of profile data from the daily volume profile into a watchlist column (and from there into Excel). Today's (developing) POC, VA high and VA low - and ideally also last session's POC, VA high and VA low (which I'm hoping might be as easy as the [-1] versions of the former). All I need are the numerical values of these 3 (6) variables in a watchlist column.
I've tried the built-in / official volume profile script pasted below as well as 3 other scripts from this site and elsewhere. I can get past the multiple 'plot' statements erroring out in a watchlist column, but any one of them (e.g. POC, VAH, VAL) returns NaN on the watchlist or else the value is inaccurate regardless of the aggregation I choose for the watchlist column. The script works perfectly fine on a chart and I've confirmed through 2 other platforms that its values are accurate with default settings (time per profile = DAY, VA% = 70, etc).
Code:
#
# TD Ameritrade IP Company, Inc. (c) 2010-2023
#
input pricePerRowHeightMode = {default AUTOMATIC, TICKSIZE, CUSTOM};
input customRowHeight = 1.0;
input timePerProfile = {default CHART, MINUTE, HOUR, DAY, WEEK, MONTH, "OPT EXP", BAR};
input multiplier = 1;
input onExpansion = yes;
input profiles = 1000;
input showPointOfControl = yes;
input showValueArea = yes;
input valueAreaPercent = 70;
input opacity = 50;
def period;
def yyyymmdd = getYyyyMmDd();
def seconds = secondsFromTime(0);
def month = getYear() * 12 + getMonth();
def day_number = daysFromDate(first(yyyymmdd)) + getDayOfWeek(first(yyyymmdd));
def dom = getDayOfMonth(yyyymmdd);
def dow = getDayOfWeek(yyyymmdd - dom + 1);
def expthismonth = (if dow > 5 then 27 else 20) - dow;
def exp_opt = month + (dom > expthismonth);
switch (timePerProfile) {
case CHART:
period = 0;
case MINUTE:
period = floor(seconds / 60 + day_number * 24 * 60);
case HOUR:
period = floor(seconds / 3600 + day_number * 24);
case DAY:
period = countTradingDays(min(first(yyyymmdd), yyyymmdd), yyyymmdd) - 1;
case WEEK:
period = floor(day_number / 7);
case MONTH:
period = floor(month - first(month));
case "OPT EXP":
period = exp_opt - first(exp_opt);
case BAR:
period = barNumber() - 1;
}
def count = CompoundValue(1, if period != period[1] then (count[1] + period - period[1]) % multiplier else count[1], 0);
def cond = count < count[1] + period - period[1];
def height;
switch (pricePerRowHeightMode) {
case AUTOMATIC:
height = PricePerRow.AUTOMATIC;
case TICKSIZE:
height = PricePerRow.TICKSIZE;
case CUSTOM:
height = customRowHeight;
}
profile vol = volumeProfile("startNewProfile" = cond, "onExpansion" = onExpansion, "numberOfProfiles" = profiles, "pricePerRow" = height, "value area percent" = valueAreaPercent);
def con = compoundValue(1, onExpansion, no);
def pc = if IsNaN(vol.getPointOfControl()) and con then pc[1] else vol.getPointOfControl();
def hVA = if IsNaN(vol.getHighestValueArea()) and con then hVA[1] else vol.getHighestValueArea();
def lVA = if IsNaN(vol.getLowestValueArea()) and con then lVA[1] else vol.getLowestValueArea();
def hProfile = if IsNaN(vol.getHighest()) and con then hProfile[1] else vol.getHighest();
def lProfile = if IsNaN(vol.getLowest()) and con then lProfile[1] else vol.getLowest();
def plotsDomain = IsNaN(close) == onExpansion;
plot POC = if plotsDomain then pc else Double.NaN;
plot ProfileHigh = if plotsDomain then hProfile else Double.NaN;
plot ProfileLow = if plotsDomain then lProfile else Double.NaN;
plot VAHigh = if plotsDomain then hVA else Double.NaN;
plot VALow = if plotsDomain then lVA else Double.NaN;
DefineGlobalColor("Profile", GetColor(1));
DefineGlobalColor("Point Of Control", GetColor(5));
DefineGlobalColor("Value Area", GetColor(8));
vol.show(globalColor("Profile"), if showPointOfControl then globalColor("Point Of Control") else color.current, if showValueArea then globalColor("Value Area") else color.current, opacity);
POC.SetDefaultColor(globalColor("Point Of Control"));
POC.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
VAHigh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
VALow.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
VAHigh.SetDefaultColor(globalColor("Value Area"));
VALow.SetDefaultColor(globalColor("Value Area"));
ProfileHigh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ProfileLow.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ProfileHigh.SetDefaultColor(GetColor(3));
ProfileLow.SetDefaultColor(GetColor(3));
ProfileHigh.hide();
ProfileLow.hide();
I have an attached code that shows the anchored VWAP concerning the date and time. I would like to add an upper and lower band with this vwap at the desired anchored date and time. I would like to request anyone, please, to help me modify the script to add upper and lower bands. @halcyonguy would you please see to solve the problem.
Thank you
Code:
@Date
input anchorDate = 20240216;
input anchorTime = 0222;
input numDevUp = 1.0;
input numDevDN = -1.0;
def postAnchorDate = if GetYYYYMMDD() >= anchorDate then 1 else 0;
def postAnchorTime = if SecondsTillTime(anchorTime) == 0 then 1 else if GetYYYYMMDD() < anchorDate then 0 else postAnchorTime[1];
def v1 = (high + low + close) / 3;
def v2 = volume;
def v3 = v2 * v1;
def v4 = v2 * Sqr(v1);
plot anchoredVWAP = TotalSum(if postAnchorDate and postAnchorTime then (v1) * (v2) else 0) / TotalSum(if postAnchorDate and postAnchorTime then v2 else 0);
anchoredVWAP.SetStyle(Curve.FIRM);
anchoredVWAP.SetLineWeight(3);
anchoredVWAP.SetDefaultColor(Color.CYAN);
Thinkorswim has a default anchored VWAP indicator but that anchors date only, not time. I have attached an image and link to the study for reference. I am requesting the same; instead, I want to anchor time, too.
I have an attached code that shows the anchored VWAP concerning the date and time. I would like to add an upper and lower band with this vwap at the desired anchored date and time. I would like to request anyone, please, to help me modify the script to add upper and lower bands. @halcyonguy would you please see to solve the problem.
Thank you
Code:
@Date
input anchorDate = 20240216;
input anchorTime = 0222;
input numDevUp = 1.0;
input numDevDN = -1.0;
def postAnchorDate = if GetYYYYMMDD() >= anchorDate then 1 else 0;
def postAnchorTime = if SecondsTillTime(anchorTime) == 0 then 1 else if GetYYYYMMDD() < anchorDate then 0 else postAnchorTime[1];
def v1 = (high + low + close) / 3;
def v2 = volume;
def v3 = v2 * v1;
def v4 = v2 * Sqr(v1);
plot anchoredVWAP = TotalSum(if postAnchorDate and postAnchorTime then (v1) * (v2) else 0) / TotalSum(if postAnchorDate and postAnchorTime then v2 else 0);
anchoredVWAP.SetStyle(Curve.FIRM);
anchoredVWAP.SetLineWeight(3);
anchoredVWAP.SetDefaultColor(Color.CYAN);
Thinkorswim has a default anchored VWAP indicator but that anchors date only, not time. I have attached an image and link to the study for reference. I am requesting the same; instead, I want to anchor time, too.
useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.
How do I get started?
We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.
If you are new, or just looking for guidance, here are some helpful links to get you started.
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.