Anchored VWAP Indicator for ThinkorSwim

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.
Hey SleepyZ/everyone, thanks for this. Wondering if you can help clarify what/how the aggregation period is used. I tried it, and it doesn't look different than regular vwap. I was looking for an update to have the VWAP anchored automatically to a specific time interval. Similar to the High/Low, but in my case, every 30m/hour, etc.
 
Hey SleepyZ/everyone, thanks for this. Wondering if you can help clarify what/how the aggregation period is used. I tried it, and it doesn't look different than regular vwap. I was looking for an update to have the VWAP anchored automatically to a specific time interval. Similar to the High/Low, but in my case, every 30m/hour, etc.
An ANCHORED Vwap is ANCHORED to a specific date. The script in the top post is ANCHORED to: April 22nd 2020.
You need to changed it to the period that you want it anchored to.
 
An ANCHORED Vwap is ANCHORED to a specific date. The script in the top post is ANCHORED to: April 22nd 2020.
You need to changed it to the period that you want it anchored to.
Hey Merry, Yes, got that. I was referring to this script: https://usethinkscript.com/threads/anchored-vwap-indicator-for-thinkorswim.171/post-79795

it shows an aggregation period of 15m, i was wondering why that would be since the vwap is focused on day unless time is entered.

Also, i wanted to see if the script can be updated to have it set the time automatically to a specific interval (ie 30m/hour) instead of manual input.

thanks.
 
Hey Merry, Yes, got that. I was referring to this script: https://usethinkscript.com/threads/anchored-vwap-indicator-for-thinkorswim.171/post-79795

it shows an aggregation period of 15m, i was wondering why that would be since the vwap is focused on day unless time is entered.

Also, i wanted to see if the script can be updated to have it set the time automatically to a specific interval (ie 30m/hour) instead of manual input.

thanks.
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. No workaround for the manually entering the anchor date or using the whole chart
 
Last edited:
I'm trying to combine 2 scripts to make an automated Anchored VWAP at the Futures Pre-market high and low points. I primarily use a 2000-Tick chart to trade either ES or NQ. I understand may need to make 2 separate indicators or add code to what I have. I want Line 33 (Pre-market low point) to be the input answer for Line 36, where an Anchored VWAP would plot for that day. And then I want to get Line 28 to be the input for an Anchored VWAP at Pre-market high point.
 
Last edited by a moderator:
Anchored VWAP indicator can useful for intraday trading. It can also be used on any timeframe including hourly, daily, and weekly. VWAP can be a great tool for analyzing the market, especially for day traders.

Here we have different Anchored VWAP indicators for ThinkorSwim. Feel free to test them out and use any that fits your trading style.

NfAwrZz.png


Anchored VWAP with adjustable date and time

Code:
input anchorDate = 20200422;
input anchorTime = 2030;

def tradeStartEST = 0930;
def tradeEndEST = 1600;
def inPeriod = if SecondsTillTime(tradeStartEST) <= 0 and SecondsTillTime(tradeEndEST) > 0 then 1 else 0;

def revisedDate = if SecondsTillTime(anchorTime)<=0 and !inPeriod then anchorDate+1 else if SecondsTillTime(anchorTime)<=0 and inPeriod then anchorDate else anchorDate;

def postAnchorDate = if GetYYYYMMDD() >= revisedDate then 1 else 0;
def postAnchorTime = if SecondsTillTime(anchorTime) <= 0 then 1 else 0;

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(3);
anchoredVWAP.SetDefaultColor(Color.Cyan);

#AddChartBubble(yes,close, revisedDate, color.yellow);

Intraday Anchored VWAP

Code:
#yakBro intraday anchoredVWAP excluding extended hours volume 2019

declare hide_on_daily;

def anchorTime = 0930;
def anchorEnd = 1600;

input ShowTodayOnly = yes;
def Today = if GetDay() == GetLastDay() then 1 else 0;
def postAnchorTime = if SecondsFromTime(anchorTime) >= 0 then 1 else 0;
def endAchorTime = if SecondsTillTime(anchorEnd) >= 0 then 1 else 0;

#plot anchorVWAP for intraday
def  volumeSum = compoundValue(1, if postAnchorTime and endAchorTime then volumeSum[1] + volume else 0, volume);
def  volumeVwapSum = compoundValue(1, if postAnchorTime and endAchorTime then volumeVwapSum[1] + volume * vwap else 0, volume * vwap);

plot anchorVWAP = if ShowTodayOnly and !Today then Double.NaN else if anchorTime then volumeVwapSum / volumeSum else Double.NaN;
anchorVWAP.setStyle(Curve.Firm);
anchorVWAP.setDefaultColor(Color.light_ORANGE);
anchorVWAP.setlineWeight(2);

Anchored VWAP Stops

Rich (BB code):
#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.Cyan);
VwapL.SetDefaultColor(Color.YellowEN);
#END STUDY
#Note: /ES 5m chart of the Anchored_VWAP_STOPS study.

Shareable Link: https://tos.mx/ICxmA7

AiLP8II.png


VWAP Anchored_v02

Rich (BB code):
#START STUDY
#Anchored_VWAP2
#linus, 2014-03-10, v0.1

#10:24 linus: it carries over the previous pivot's lines for high, low and close. (it plots vwaps of the high, low and close that are reset each time a new pivot is found.)
#10:25 linus: i wrote it to experiment with vwap as stops. (the high and low vwaps that can be offset by the ticks input.)
#10:25 linus: but it should serve as an example of how to reset the vwaps based on a signal.
#10:35 linus: #hint: VWAP stops anchored off  fractalTrader pivots.
#10:37 linus: the code calculates the pivots as PivH and PivL, and then restarts the high, low and close vwaps when it finds a new pivot.  Otherwise it continues to calculate the high, low and close vwaps.
#10:37 linus: the dashed vwap plots are the saved from the previous pivot, and the solid vwap plots are since the last pivot.

#hint: VWAP stops anchored off  fractalTrader pivots.

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

#hint ticks: Offset High/Low 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;

plot Up = !isNaN(PivL);
Up.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
Up.SetLineWeight(3);
Up.SetDefaultColor(Color.WHITE);

plot Dn = !isNaN(PivH);
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;
def LocC = close * volume;

rec PC;
rec VC;
rec PC2;
rec VC2;
rec PH;
rec VH;
rec PL;
rec VL;
rec PH2;
rec VH2;
rec PL2;
rec VL2;

if Dn or Up {
    PC = LocC;
    VC = volume;
    PC2 = PC[1];
    VC2 = VC[1];
} else {
    PC = compoundValue(1, LocC + PC[1], Double.NaN);
    VC = compoundValue(1, volume + VC[1], Double.NaN);
    PC2 = compoundValue(1, LocC + PC2[1], Double.NaN);
    VC2 = compoundValue(1, volume + VC2[1], Double.NaN);
}

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

plot VwapC = if Dn or Up then Double.NaN else PC / VC;
plot VwapC2 = if Dn or Up then Double.NaN else PC2 / VC2;
plot VwapH = if Dn then Double.NaN else PH / VH;
plot VwapL = if Up then Double.NaN else PL / VL;
plot VwapH2 = if Dn then Double.NaN else PH2 / VH2;
plot VwapL2 = if Up then Double.NaN else PL2 / VL2;

VwapC.SetDefaultColor(Color.YELLOW);
VwapC.SetLineWeight(2);
VwapC.HideBubble();

VwapC2.SetDefaultColor(Color.YELLOW);
VwapC2.SetLineWeight(2);
VwapC2.SetStyle(Curve.SHORT_DASH);
VwapC2.HideBubble();

VwapH.SetDefaultColor(Color.DARK_RED);
VwapH.HideBubble();

VwapL.SetDefaultColor(Color.DARK_GREEN);
VwapL.HideBubble();

VwapH2.SetDefaultColor(Color.DARK_RED);
VwapH2.SetStyle(Curve.SHORT_DASH);
VwapH2.HideBubble();

VwapL2.SetDefaultColor(Color.DARK_GREEN);
VwapL2.SetStyle(Curve.SHORT_DASH);
VwapL2.HideBubble();
#END STUDY

Shareable Link: https://tos.mx/s17BmB

Video Tutorial


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.
Is it possible to AVWAP just attach to LOD and HOD and not keep changing to pivots, if not in this code may be another one, Intraday just 2 lines one attached to LOD and other to HOD. Thank you
 
I was wanting the same thing.
@saak99
Here is a mod to the script I posted above to have an option to anchor at the HOD or LOD. It will only plot on the last day. Almost all of the optional plots of deviations, clouds, etc were set to no in the following image.

Capture.jpg
Ruby:
#VWAP Anchored to Date/Time

input startdateselection = {default Daily, Custom};
input starttimeselection = {default HOD, LOD, RTH, PRE, Custom};
input showtodayonly      = yes;
input colorvwap          = yes;

input startdate = 20211130;
input starttime = 0930;
def hday = if GetYYYYMMDD() != GetYYYYMMDD()[1] then high else if high > hday[1] then high else hday[1];
def hbar = if high == hday then barnumber() else double.nan;
def lday = if GetYYYYMMDD() != GetYYYYMMDD()[1] then low else if low < lday[1] then low else lday[1];
def lbar = if low == lday then BarNumber() else Double.NaN;
def time = if starttimeselection == starttimeselection.RTH
           then 0930
           else if starttimeselection == starttimeselection.PRE
           then 0400
           else starttime;

def ymd         = GetYYYYMMDD();
def bn          = BarNumber();
def c           = close;
def v           = volume;
def vw          = vwap;

def volumesum;
def volumevwapsum;
def volumevwap2sum;
def price;
def deviation;


def anchor        =  if startdateselection == startdateselection.Daily and ymd != ymd[1] or                      startdateselection == startdateselection.Custom and GetYYYYMMDD() < startdate
                     then 0
                     else if starttimeselection == starttimeselection.HOD
                     and BarNumber() == HighestAll(hbar)
                     then 1
                     else if starttimeselection == starttimeselection.LOD
                     and BarNumber() == HighestAll(lbar)
                     then 1
                     else if starttimeselection == starttimeselection.RTH
                     and GetTime() crosses above RegularTradingStart(GetYYYYMMDD())      
                     or starttimeselection == starttimeselection.PRE
                     and GetTime() crosses below RegularTradingEnd(GetYYYYMMDD()[1])
                     or starttimeselection == starttimeselection.Custom
                     and SecondsFromTime(time)[1] <= 0 and
                         SecondsFromTime(time) >= 0
                     then 1
                     else anchor[1];


volumesum      = if  (anchor)  then volumesum[1] + volume else 0;
volumevwapsum  = if  (anchor)  then volumevwapsum[1] + volume * vwap else 0;
volumevwap2sum = if  (anchor)  then volumevwap2sum[1] + volume * Sqr(vwap) else 0;
price          = volumevwapsum / volumesum;
deviation      = Sqrt(Max(volumevwap2sum / volumesum - Sqr(price), 0));

;

plot VWAP      = if showtodayonly and startdateselection == startdateselection.Daily and !IsNaN(close(period = AggregationPeriod.DAY)[-1]) then Double.NaN else price;
VWAP.AssignValueColor(if colorvwap and close[1] < VWAP then Color.RED else Color.CYAN);

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 );
 
Last edited by a moderator:
1. Define your logic map of where to plot the line
If condition 1 then x else y​
if condition 2 then a else b etc..​
2. Insert your defined logic into one of the horizontal line scripts found:
This is the #Anchored_VWAP2 indicator.
We have 3 types of lines vwaps, which are drawn from the high (VwapH), from the low (VwapL) and from the close (VwapC). The length is set to " Lookback period for finding swing highs, lows.", I have 100 on my screen.
Lines are reset each time a new pivot is found.
There is also VwapH2, VwapL2, VwapC2. Dashed lines that continue to be calculated until the next pivot and are also reset.
Is it possible to somehow correct the code so that they continue to calculate further until one more turn (as in the figure)? I named it VwapC3.



#START STUDY
#Anchored_VWAP2
#linus, 2014-03-10, v0.1

#10:24 linus: it carries over the previous pivot's lines for high, low and close. (it plots vwaps of the high, low and close that are reset each time a new pivot is found.)
#10:25 linus: i wrote it to experiment with vwap as stops. (the high and low vwaps that can be offset by the ticks input.)
#10:25 linus: but it should serve as an example of how to reset the vwaps based on a signal.
#10:35 linus: #hint: VWAP stops anchored off fractalTrader pivots.
#10:37 linus: the code calculates the pivots as PivH and PivL, and then restarts the high, low and close vwaps when it finds a new pivot. Otherwise it continues to calculate the high, low and close vwaps.
#10:37 linus: the dashed vwap plots are the saved from the previous pivot, and the solid vwap plots are since the last pivot.

#hint: VWAP stops anchored off fractalTrader pivots.

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

#hint ticks: Offset High/Low VWAP lines by this number of ticks.
input ticks = 0.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;

plot Up = !isNaN(PivL);
Up.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
Up.SetLineWeight(3);
Up.SetDefaultColor(Color.WHITE);

plot Dn = !isNaN(PivH);
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;
def LocC = close * volume;

rec PC;
rec VC;
rec PC2;
rec VC2;
rec PH;
rec VH;
rec PL;
rec VL;
rec PH2;
rec VH2;
rec PL2;
rec VL2;

if Dn or Up {
PC = LocC;
VC = volume;
PC2 = PC[1];
VC2 = VC[1];
} else {
PC = compoundValue(1, LocC + PC[1], Double.NaN);
VC = compoundValue(1, volume + VC[1], Double.NaN);
PC2 = compoundValue(1, LocC + PC2[1], Double.NaN);
VC2 = compoundValue(1, volume + VC2[1], Double.NaN);
}

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

plot VwapC = if Dn or Up then Double.NaN else PC / VC;
plot VwapC2 = if Dn or Up then Double.NaN else PC2 / VC2;
plot VwapH = if Dn then Double.NaN else PH / VH;
plot VwapL = if Up then Double.NaN else PL / VL;
plot VwapH2 = if Dn then Double.NaN else PH2 / VH2;
plot VwapL2 = if Up then Double.NaN else PL2 / VL2;


VwapC.SetDefaultColor(Color.YELLOW);
VwapC.SetLineWeight(2);
VwapC.HideBubble();

VwapC2.SetDefaultColor(Color.YELLOW);
VwapC2.SetLineWeight(2);
VwapC2.SetStyle(Curve.SHORT_DASH);
VwapC2.HideBubble();

VwapH.SetDefaultColor(Color.DARK_RED);
VwapH.HideBubble();

VwapL.SetDefaultColor(Color.DARK_GREEN);
VwapL.HideBubble();

VwapH2.SetDefaultColor(Color.DARK_RED);
VwapH2.SetStyle(Curve.SHORT_DASH);
VwapH2.HideBubble();

VwapL2.SetDefaultColor(Color.DARK_GREEN);
VwapL2.SetStyle(Curve.SHORT_DASH);
VwapL2.HideBubble();
#END STUDY
 
Can the Anchored Vwap stops code be adjusted to only plot the last anchored vwap ?
Anchored VWAP Stops
 
Last edited by a moderator:
Hi all,

I have the following code for anchored VWAP;

input AnchorTime = 0930;
input AnchorEndTime = 1600;
input ShowTodayOnly = yes;

def Today = if GetDay() == GetLastDay() then 1 else 0;
def PostAnchorTime = if SecondsFromTime(AnchorTime) >= 0 then 1 else 0;
def EndAnchorTime = if SecondsTillTime(AnchorEndTime) >= 0 then 1 else 0;

def VolumeSum = CompoundValue(1, if PostAnchorTime and EndAnchorTime then VolumeSum[1] + Volume else 0, Volume);
def VolumeVWAPSum = CompoundValue(1, if PostAnchorTime and EndAnchorTime then VolumeVWAPSum[1] + Volume * VWAP else 0, Volume * VWAP);

plot AnchorVWAP = if ShowTodayOnly and !Today then Double.NaN else if AnchorTime then VolumeVWAPSum / VolumeSum else Double.NaN;
AnchorVWAP.setstyle(curve.firm);
AnchorVWAP.setdefaultColor(color.Magenta);
AnchorVWAP.setlineWeight(2);

Does anyone know if there is a way to dynamically make the start time the "High of Day" (or "Low of Day") instead of manually changing the time?

This would be a phenomenal study as it would work on any and all charts. Otherwise all other Anchored VWAP studies that I have ever seen, you have to manually modify the time for every single chart.

Regards,
PT_Scalper
 
Last edited:
Hi all,

I have the following code for anchored VWAP;



Does anyone know if there is a way to dynamically make the start time the "High of Day" (or "Low of Day") instead of manually changing the time?

This would be a phenomenal study as it would work on any and all charts. Otherwise all other Anchored VWAP studies that I have ever seen, you have to manually modify the time for every single chart.

Regards,
PT_Scalper
See if this helps
https://usethinkscript.com/threads/anchored-vwap-indicator-for-thinkorswim.171/post-88102
 
You're welcome! Currently it starts looking in Pre-Market.
Hi @SleepyZ,

What do I need to change in the code to look back to 2 days for the anchored VWAP? Looking for a 2 day anchored vwap option instead of the 1 day.

I tried to change the GetYYYYMMDD()[1] to GetYYYYMMDD()[2] throughout the code but that didn't work.

Regards,
PT_Scalper
 
How to plot standard deviations for anchored intraday VWAP?
 
Last edited by a moderator:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
202 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

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.

What are the benefits of VIP Membership?
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.
Back
Top