Anchored VWAP Indicator for ThinkorSwim

MerryDay

Administrative
Staff member
Staff
VIP
@Xhrx No one can explain the slight differences between indicators on different platforms. An enterprising poster has a thread on here somewhere that showed that even the EOD close can be microscopically different.
 

eztrader

New member
You can load multiple copies of the script and change the symbols at the input. The scripts will only display the one associated with the chart symbol
Hello SleepyZ. I had somewhat the same question on the Anchored VWAP. I understand that studies have the settings persistent and transfer to the next symbol selected. The way I look at an Anchored VWAP is that it should be an optional item with specifics to only that symbol. The closest thing I can think of is a drawing which is symbol specific. Take fibonaccis for instance. You cannot transfer that to another symbol.

I did a search on drawings to see if custom drawings could be coded but did not find anywhere where that is possible. I do appreciate your helping on this indicator and recognize that adding the Anchored VWAP multiple times does allow one to apply this to multiple symbols with specific entries, but was hoping there was a way to code more like the drawing tools. Are you aware of any way to accomplish this? Thank you.

ez
 

SleepyZ

Well-known member
Hello SleepyZ. I had somewhat the same question on the Anchored VWAP. I understand that studies have the settings persistent and transfer to the next symbol selected. The way I look at an Anchored VWAP is that it should be an optional item with specifics to only that symbol. The closest thing I can think of is a drawing which is symbol specific. Take fibonaccis for instance. You cannot transfer that to another symbol.

I did a search on drawings to see if custom drawings could be coded but did not find anywhere where that is possible. I do appreciate your helping on this indicator and recognize that adding the Anchored VWAP multiple times does allow one to apply this to multiple symbols with specific entries, but was hoping there was a way to code more like the drawing tools. Are you aware of any way to accomplish this? Thank you.

ez
Sorry, I am not.
 

David45

Member
2019 Donor
In reference to the code in post #1 (Anchored VWAP Stops), is there a way to paint the candlesticks RED when the Down Arrow appears and continue to paint all subsequent candlesticks RED until an Up Arrow appears? At which point the candlesticks would paint GREEN and continue to paint all subsequent candlesticks GREEN until a Down Arrow appears?

I really appreciate any assistance with this! Thanks in advance!
 

SleepyZ

Well-known member
In reference to the code in post #1 (Anchored VWAP Stops), is there a way to paint the candlesticks RED when the Down Arrow appears and continue to paint all subsequent candlesticks RED until an Up Arrow appears? At which point the candlesticks would paint GREEN and continue to paint all subsequent candlesticks GREEN until a Down Arrow appears?

I really appreciate any assistance with this! Thanks in advance!

Try this.

Capture.jpg
Ruby:
#START STUDY
#Anchored_VWAP_STOPS
#linus, 2014-03-10, v0.1

#hint: VWAP stops anchored off FractalTrader pivots.

#hint n: Lookback period for finding swing highs, lows.
input n = 20;

#hint ticks: Offset VWAP lines by this number of ticks.
input ticks = 2.0;

def bnOK = barNumber() > n;

def isHigher = fold i = 1 to n + 1 with p = 1
               while p do high > GetValue(high, -i);

def HH = if bnOK and isHigher
         and high == Highest(high, n)
         then high else Double.NaN;

def isLower = fold j = 1 to n + 1 with q = 1
              while q do low < GetValue(low, -j);

def LL = if bnOK and isLower
         and low == Lowest(low, n)
         then low else Double.NaN;

def PivH = if HH > 0 then HH else Double.NaN;
def PivL = if LL > 0 then LL else Double.NaN;

rec dir = compoundValue(1, if !isNaN(PivL) then 1 else if !isNaN(PivH) then -1 else dir[1], 0);

plot Up = dir crosses above 0;
Up.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
Up.SetLineWeight(3);
Up.SetDefaultColor(Color.WHITE);

plot Dn = dir crosses below 0;
Dn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
Dn.SetLineWeight(3);
Dn.SetDefaultColor(Color.ORANGE);

def LocH = (high + (tickSize() * ticks)) * volume;
def LocL = (low - (tickSize() * ticks)) * volume;
rec PH;
rec VH;
rec PL;
rec VL;

if Dn {
    PH = LocH;
    VH = volume;
} else {
    PH = compoundValue(1, LocH + PH[1], Double.NaN);
    VH = compoundValue(1, volume + VH[1], Double.NaN);
}
if Up  {
    PL = LocL;
    VL = volume;
} else {
    PL = compoundValue(1, LocL + PL[1], Double.NaN);
    VL = compoundValue(1, volume + VL[1], Double.NaN);
}

plot VwapH = if Dn then Double.NaN else PH / VH;
plot VwapL = if Up then Double.NaN else PL / VL;

VwapH.SetDefaultColor(Color.DARK_RED);
VwapL.SetDefaultColor(Color.DARK_GREEN);
#END STUDY
#Note: /ES 5m chart of the Anchored_VWAP_STOPS study.

def slow = if up then 1 else if slow[1]==1 and !dn then 1 else 0;
assignpriceColor(if slow==1 then color.green else color.red);
 

8Nick8

Active member
2019 Donor
VIP
@Xhrx
Try this...just change the anchorDate to your desired candle, preferably those with High Vol.

input anchorDate = 20210122;
input anchorTime = 930;

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];

plot anchoredVWAP = TotalSum(if postAnchorDate and postAnchorTime then ((high+low+close)/3)*(volume) else 0)/TotalSum(if postAnchorDate and postAnchorTime then volume else 0);

anchoredVWAP.setStyle(Curve.Firm);
anchoredVWAP.SetLineWeight(1);
anchoredVWAP.SetDefaultColor(Color.Cyan);
 

QUIKTDR1

Member
VIP
Merry,

Is there a way to add a code of price greater than Anchored VWAP to a strategy?

Thank you in advance!
 
Last edited by a moderator:

MerryDay

Administrative
Staff member
Staff
VIP
@QUIKTDR1
Add the following code to your study and past the study into the strategy tab:
Code:
AddOrder(OrderType.BUY_AUTO, UP > 0);
AddOrder(OrderType.Sell_AUTO, DN < 0);
 

mr.sinist3r

New member
Code:
# VWAP Standard Deviation Bands
# lar
# 12.12.2015

# V1.0 - 12.12.2015 - lar     - Initial release of VWAP Standard Deviation bands
# V1.1 - 12.17.2019 - tomsk   - Minor edits

input timeFrame = {Day, Week, Month, default Year, Chart, "March"};
input BandType = {default Standard, "1/4 Day Range", "3x Avg Bar Range", ThinkScripter, None};
input ShowCloud = yes;
input HideSdLines = no;

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 seconds = SecondsFromTime(0);
def month = GetYear() * 12 + GetMonth();
def year = GetYear();
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);
def periodIndx;

switch (timeFrame) {
case Chart:
    periodIndx = 0;

case Day:
    periodIndx = CountTradingDays(Min(First(yyyyMmDd), yyyyMmDd), yyyyMmDd) - 1;

case Week:
    periodIndx = Floor(day_number / 7);

case Month:
    periodIndx = Floor(month - First(month));

case Year:
    periodIndx = Floor(year - First(year));

case "March":
    periodIndx = Floor(2020 - First(2020));
}
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;
switch (BandType) {

case Standard:
    deviation = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(price), 0));

case "1/4 Day Range":
    deviation = Sqrt(AbsValue(high(Period = timeFrame) - low(Period = timeFrame)) * .25);

case "3x Avg Bar Range":
    deviation = Sqrt(Average(TrueRange(high,  close,  low),  20) * 3);

case ThinkScripter:
    deviation = Sqrt(TotalSum(Sqr(((open + high + low + close) / 4) - price) * volume) / TotalSum(volume));

case None:
    deviation = Double.NaN;
}

plot VWAP = price;
VWAP.AssignValueColor(if VWAP > VWAP[1] then Color.Cyan
                      else if VWAP < VWAP[1] then Color.Red
                      else Color.Yellow);
VWAP.SetStyle(Curve.SHORT_DASH);
VWAP.SetLineWeight(2);

# TS_CHART_VWAP_SD_BANDS
# http://www.thinkscripter.com
# [email protected]
# Last Update 03 APR 2010

input VWAPStdev1 = 1.0;
input VWAPStdev2 = 2.0;
input VWAPStdev3 = 3.0;

plot r1 = VWAP + VWAPStdev1 * deviation;
plot s1 = VWAP - VWAPStdev1 * deviation;
plot r2 = VWAP + VWAPStdev2 * deviation;
plot s2 = VWAP - VWAPStdev2 * deviation;
plot r3 = VWAP + VWAPStdev3 * deviation;
plot s3 = VWAP - VWAPStdev3 * deviation;

DefineGlobalColor("sDev1", (CreateColor(40, 40, 40)));
DefineGlobalColor("sDev2", (CreateColor(128, 128, 128)));
DefineGlobalColor("sDev3", (CreateColor(100, 100, 100)));

r1.SetDefaultColor(GlobalColor("sDev1"));
s1.SetDefaultColor(GlobalColor("sDev1"));
r2.SetDefaultColor(GlobalColor("sDev2"));
s2.SetDefaultColor(GlobalColor("sDev2"));
r3.SetDefaultColor(GlobalColor("sDev2"));
s3.SetDefaultColor(GlobalColor("sDev2"));

r1.SetStyle(Curve.SHORT_DASH);
r2.SetStyle(Curve.SHORT_DASH);
r3.SetStyle(Curve.SHORT_DASH);
s1.SetStyle(Curve.SHORT_DASH);
s2.SetStyle(Curve.SHORT_DASH);
s3.SetStyle(Curve.SHORT_DASH);

VWAP.HideBubble();
r1.HideBubble();
r2.HideBubble();
r3.HideBubble();
s1.HideBubble();
s2.HideBubble();
s3.HideBubble();

r1.SetHiding(HideSdLines);
r2.SetHiding(HideSdLines);
r3.SetHiding(HideSdLines);
s1.SetHiding(HideSdLines);
s2.SetHiding(HideSdLines);
s3.SetHiding(HideSdLines);

# CLOUD ##########################
def CloudR1 = if ShowCloud then r1 else Double.NaN;
def CloudR2 = if ShowCloud then r2 else Double.NaN;
def CloudR3 = if ShowCloud then r3 else Double.NaN;
def CloudS1 = if ShowCloud then s1 else Double.NaN;
def CloudS2 = if ShowCloud then s2 else Double.NaN;
def CloudS3 = if ShowCloud then s3 else Double.NaN;
AddCloud(CloudR1, CloudR2, GlobalColor("sDev1"), GlobalColor("sDev1"));
AddCloud(CloudS1, CloudS2, GlobalColor("sDev1"), GlobalColor("sDev1"));
AddCloud(CloudR2, CloudR3, GlobalColor("sDev2"), GlobalColor("sDev2"));
AddCloud(CloudS2, CloudS3, GlobalColor("sDev2"), GlobalColor("sDev2"));
# End VWAP Standard Deviation Bands
My title may not make a lot of sense, but let me explain it.

As you know, with the anchored VWAP, you can anchor it at any date you want. I've been trying to translate this specific date code over to a VWAP w/ Standard Deviation Bands indicator I found on this website, but to no avail. The VWAP w/ SD Bands indicator only allows me to choose between: year, day, week and quarter. I don't want any of those, I want to pick the date where the bands and VWAP start, similar to an anchored VWAP. For example, I want the date to start at the bottom of the COVID March crash, March 23, 2020. Any ideas?

EDIT
https://www.tradingview.com/script/h8mk3PRk-Anchored-VWAP-w-STD-bands/

Here's an indicator in tradingview, with the idea I'm talking about, where it lets you anchor it at whatever date you want.
This is exactly what I am looking for as well. Any possible updates? Custom Input for date and time with standard deviation bands similar to the TV indicator. Thank you all.
 

SleepyZ

Well-known member
This is exactly what I am looking for as well. Any possible updates? Custom Input for date and time with standard deviation bands similar to the TV indicator. Thank you all.

This is something I did awhile ago that might work. You can either choose to have the vwap plotted using the current chart agg or a higher agg. Input a date that is on the chart. If the date is not on the chart, it will plot the VWAP for the whole chart displayed.
Capture.jpg

Ruby:
#VWAP Anchored to Date/Time

input usehigher = {default current, higher};
input agg       = AggregationPeriod.FIFTEEN_MIN;
input startdate = 20211015;
input starttime = 0930;
def bn          = BarNumber();
def c           = close;
def v           = volume(period = agg);
def vw          = vwap(period = agg);
def anchor;
def volumesum;
def volumevwapsum;
def volumevwap2sum;
def price;
def deviation;

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));
}

plot VWAP      = price;
VWAP.SetDefaultColor(GetColor(0));

input showbands = yes;
input numDev1  = 1.0;
input numDev2  = 2.0;
input numDev3  = 3.0;

plot UpperBand1  = if !showbands then Double.NaN else VWAP + numDev1 * deviation;
plot LowerBand1  = if !showbands then Double.NaN else VWAP - numDev1 * deviation;
plot UpperBand2  = if !showbands then Double.NaN else VWAP + numDev2 * deviation;
plot LowerBand2  = if !showbands then Double.NaN else VWAP - numDev2 * deviation;
plot UpperBand3  = if !showbands then Double.NaN else VWAP + numDev3 * deviation;
plot LowerBand3  = if !showbands then Double.NaN else VWAP - numDev3 * deviation;

VWAP.SetDefaultColor(Color.CYAN);
UpperBand1.SetDefaultColor(Color.GREEN);
LowerBand1.SetDefaultColor(Color.RED);
UpperBand2.SetDefaultColor(Color.GREEN);
LowerBand2.SetDefaultColor(Color.RED);
UpperBand3.SetDefaultColor(Color.GREEN);
LowerBand3.SetDefaultColor(Color.RED);
VWAP.HideBubble();
UpperBand1.HideBubble();
LowerBand1.HideBubble();
UpperBand2.HideBubble();
LowerBand2.HideBubble();
UpperBand3.HideBubble();
LowerBand3.HideBubble();

input showclouds = yes;
AddCloud(if showclouds then UpperBand3 else Double.NaN, UpperBand2, Color.LIGHT_GREEN, Color.LIGHT_GREEN);
AddCloud(if showclouds then LowerBand3 else Double.NaN, LowerBand2, Color.LIGHT_RED, Color.LIGHT_RED);


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;

AddChartBubble(showbubblesline and IsNaN(c[n]) and !IsNaN(c[n1]) ,
               price[n1] ,
              "V:\n" + Round(price[n1] , 2),
               Color.ORANGE);
AddChartBubble(showbubblesline and IsNaN(c[n]) and !IsNaN(c[n1]) ,
               price[n1] + numDev1 * deviation[n1] ,
              "V1:\n" + Round((price[n1] + numDev1 * deviation[n1]), 2),
               Color.GREEN);
AddChartBubble(showbubblesline and IsNaN(c[n]) and !IsNaN(c[n1]) ,
               price[n1] + numDev2 * deviation[n1] ,
              "V2:\n" + Round(price[n1] + numDev2 * deviation[n1], 2),
               Color.GREEN);
AddChartBubble(showbubblesline and IsNaN(c[n]) and !IsNaN(c[n1]) ,
               price[n1] + numDev3 * deviation[n1] ,
              "V3:\n" + Round(price[n1] + numDev3 * deviation[n1], 2),
               Color.GREEN);
AddChartBubble(showbubblesline and IsNaN(c[n]) and !IsNaN(c[n1]) ,
               price[n1] - numDev1 * deviation[n1] ,
              "V1:\n" + Round(price[n1] - numDev1 * deviation[n1], 2),
               Color.RED, no);
AddChartBubble(showbubblesline and IsNaN(c[n]) and !IsNaN(c[n1]) ,
               price[n1] - numDev2 * deviation[n1] ,
              "V2:\n" + Round(price[n1] - numDev2 * deviation[n1] , 2),
               Color.RED, no);
AddChartBubble(showbubblesline and IsNaN(c[n]) and !IsNaN(c[n1]) ,
               price[n1] - numDev3 * deviation[n1] ,
              "V3:\n" + Round(price[n1] - numDev3 * deviation[n1] , 2),
               Color.RED, no);


input showbubblescurrSTD = no;
input bubblemoverVSTD = 1;#Hint bubblemoverVSTD: Number of Spaces VSTD bubble offset in expansion
def p = bubblemoverVSTD;
def p1 = p + 1;

AddChartBubble(showbubblescurrSTD and IsNaN(c[p]) and !IsNaN(c[p1]) ,
               c[p1],
               Round(((c[p1] - price[p1]) / deviation[p1]), 1) +
              "\n" + Round(c[p1], 2) ,
               if c[p1] > VWAP[p1]
               then Color.GREEN
               else Color.RED,
               if c[p1] > VWAP[p1]
               then yes else no );
 

Similar threads

Top