Anchored VWAP Indicator for ThinkorSwim

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

Alright, I have to admit, getting into this event offset stuff makes my brain hurt. How do I get 4 anchored vwaps plotted off the last 4 qtrs of earnings? The day of earnings if its pre-market release and the next day if its after market. What is it about the current code posted by Sleepyz that's making it reset the plot for each earnings date instead of continuing to the current bar? I'm really surprised this hasn't been cranked out by somebody already. It seems all the avwap indicators out there are based solely on highs/lows or dates and never both or event/earnings related related. My concentration is no way near good enough to get my head around it atm. Any help would be GREATLY appreciated to get this rolling.

This will plot up to the last 4 Earning's Date Anchored VWAP's, extended to the last bar. The VWAPs will start either the day before or after based upon whether the earnings are before or after market hours respectively.

Screenshot-2023-04-22-111940.png
Code:
# Anchored_VWAP_Earnings_Extended
# using: VM_MIDAS_StandardDeviationBands by growex, modified to capture barnumbers at earnings to anchor VWAP

script avw {
    input count = 1;
    def bn = BarNumber();
    def ymd      = HasEarnings();
    def candles  = !IsNaN(close);
    def capture  = candles and ymd ;
    def dayCount = CompoundValue(1, if capture then dayCount[1] + 1 else dayCount[1], 0);
    def thisDay  = (HighestAll(dayCount) - dayCount) + 1;

    def he = if HasEarnings() then he[1] + 1 else he[1];

    def Number_Of_Bar = HighestAll(if thisDay[1] == count + 1 and thisDay == count then bn else Double.NaN) + if HasEarnings(type = EarningTime.AFTER_MARKET) then 1 else - 1;
    input price    = close;
    input numDevUp = 1.00;
    input numDevDn = 1.00;
    def bar =  bn >= Number_Of_Bar;
    def pv  = if bar then pv[1] + price * volume else 0;
    def cumvolume = if bar then cumvolume[1] + volume else 0;

    plot vw = pv / cumvolume;

    def volume2Sum = if bar then volume2Sum[1] + volume * Sqr(price) else 0;
    def volumeSum  = volumeSum[1] + volume;
    def deviation  = Sqrt(Max(volume2Sum / cumvolume - Sqr(vw), 0));

    plot UpperBand  = vw + numDevUp * deviation;
    plot LowerBand  = vw - numDevDn * deviation;
    plot UpperBand2 = vw + 2 * deviation;
    plot LowerBand2 = vw - 2 * deviation;
    plot UpperBand3 = vw + 3 * deviation;
    plot LowerBand3 = vw - 3 * deviation;
}

input showlast_x_vwaps = 4;
input showdev1 = no;
input showdev2 = yes;
input showdev3 = no;

plot vw1   = if showlast_x_vwaps < 1 then Double.NaN else avw();
plot u1vw1 = if showlast_x_vwaps < 1 then Double.NaN else avw().UpperBand;
plot u2vw1 = if showlast_x_vwaps < 1 then Double.NaN else avw().UpperBand2;
plot u3vw1 = if showlast_x_vwaps < 1 then Double.NaN else avw().UpperBand3;

plot vw2   = if showlast_x_vwaps < 2 then Double.NaN else avw(2);
plot u1vw2 = if showlast_x_vwaps < 2 then Double.NaN else avw(2).UpperBand;
plot u2vw2 = if showlast_x_vwaps < 2 then Double.NaN else avw(2).UpperBand2;
plot u3vw2 = if showlast_x_vwaps < 2 then Double.NaN else avw(2).UpperBand3;

plot vw3   = if showlast_x_vwaps < 3 then Double.NaN else avw(3);
plot u1vw3 = if showlast_x_vwaps < 3 then Double.NaN else avw(3).UpperBand;
plot u2vw3 = if showlast_x_vwaps < 3 then Double.NaN else avw(3).UpperBand2;
plot u3vw3 = if showlast_x_vwaps < 3 then Double.NaN else avw(3).UpperBand3;

plot vw4   = if showlast_x_vwaps < 4 then Double.NaN else avw(4);
plot u1vw4 = if showlast_x_vwaps < 4 then Double.NaN else avw(4).UpperBand;
plot u2vw4 = if showlast_x_vwaps < 4 then Double.NaN else avw(4).UpperBand2;
plot u3vw4 = if showlast_x_vwaps < 4 then Double.NaN else avw(4).UpperBand3;

u1vw1.SetHiding(!showdev1);
u2vw1.SetHiding(!showdev2);
u3vw1.SetHiding(!showdev3);
u1vw2.SetHiding(!showdev1);
u2vw2.SetHiding(!showdev2);
u3vw2.SetHiding(!showdev3);
u1vw3.SetHiding(!showdev1);
u2vw3.SetHiding(!showdev2);
u3vw3.SetHiding(!showdev3);
u1vw4.SetHiding(!showdev1);
u2vw4.SetHiding(!showdev2);
u3vw4.SetHiding(!showdev3);

DefineGlobalColor("V", Color.CYAN);
DefineGlobalColor("U", Color.RED);
DefineGlobalColor("L", Color.GREEN);

input vw_lineweight = 2;
vw1.SetDefaultColor(GlobalColor("V"));
vw2.SetDefaultColor(GlobalColor("V"));
vw3.SetDefaultColor(GlobalColor("V"));
vw4.SetDefaultColor(GlobalColor("V"));
vw1.SetLineWeight(vw_lineweight);
vw2.SetLineWeight(vw_lineweight);
vw3.SetLineWeight(vw_lineweight);
vw4.SetLineWeight(vw_lineweight);
u1vw1.SetDefaultColor(GlobalColor("U"));
u2vw1.SetDefaultColor(GlobalColor("U"));
u3vw1.SetDefaultColor(GlobalColor("U"));
u1vw2.SetDefaultColor(GlobalColor("U"));
u2vw2.SetDefaultColor(GlobalColor("U"));
u3vw2.SetDefaultColor(GlobalColor("U"));
u1vw3.SetDefaultColor(GlobalColor("U"));
u2vw3.SetDefaultColor(GlobalColor("U"));
u3vw3.SetDefaultColor(GlobalColor("U"));
u1vw4.SetDefaultColor(GlobalColor("U"));
u2vw4.SetDefaultColor(GlobalColor("U"));
u3vw4.SetDefaultColor(GlobalColor("U"));


plot l1vw1 = if showlast_x_vwaps < 1 then Double.NaN else avw().LowerBand;
plot l2vw1 = if showlast_x_vwaps < 1 then Double.NaN else avw().LowerBand2;
plot l3vw1 = if showlast_x_vwaps < 1 then Double.NaN else avw().LowerBand3;

plot l1vw2 = if showlast_x_vwaps < 2 then Double.NaN else avw(2).LowerBand;
plot l2vw2 = if showlast_x_vwaps < 2 then Double.NaN else avw(2).LowerBand2;
plot l3vw2 = if showlast_x_vwaps < 2 then Double.NaN else avw(2).LowerBand3;


plot l1vw3 = if showlast_x_vwaps < 3 then Double.NaN else avw(3).LowerBand;
plot l2vw3 = if showlast_x_vwaps < 3 then Double.NaN else avw(3).LowerBand2;
plot l3vw3 = if showlast_x_vwaps < 3 then Double.NaN else avw(3).LowerBand3;

plot l1vw4 = if showlast_x_vwaps < 4 then Double.NaN else avw(4).LowerBand;
plot l2vw4 = if showlast_x_vwaps < 4 then Double.NaN else avw(4).LowerBand2;
plot l3vw4 = if showlast_x_vwaps < 4 then Double.NaN else avw(4).LowerBand3;

l1vw1.SetHiding(!showdev1);
l2vw1.SetHiding(!showdev2);
l3vw1.SetHiding(!showdev3);
l1vw2.SetHiding(!showdev1);
l2vw2.SetHiding(!showdev2);
l3vw2.SetHiding(!showdev3);
l1vw3.SetHiding(!showdev1);
l2vw3.SetHiding(!showdev2);
l3vw3.SetHiding(!showdev3);
l1vw4.SetHiding(!showdev1);
l2vw4.SetHiding(!showdev2);
l3vw4.SetHiding(!showdev3);

l1vw1.SetDefaultColor(GlobalColor("L"));
l2vw1.SetDefaultColor(GlobalColor("L"));
l3vw1.SetDefaultColor(GlobalColor("L"));
l1vw2.SetDefaultColor(GlobalColor("L"));
l2vw2.SetDefaultColor(GlobalColor("L"));
l3vw2.SetDefaultColor(GlobalColor("L"));
l1vw3.SetDefaultColor(GlobalColor("L"));
l2vw3.SetDefaultColor(GlobalColor("L"));
l3vw3.SetDefaultColor(GlobalColor("L"));
l1vw4.SetDefaultColor(GlobalColor("L"));
l2vw4.SetDefaultColor(GlobalColor("L"));
l3vw4.SetDefaultColor(GlobalColor("L"));

#
 
Last edited:
Hi SleepyZ, Can Anchored VWAP be scanned in real time or is it delayed? can you please show how to scan anchored VWAP?

Yes, after modifying the script by renaming all of the plots as defs and defining a plot scan. As you did not indicate what you wanted in a scan, see an example in code below for one of many that can be done.

Code:
#START STUDY
#Anchored_VWAPv3
#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.
#20220708 Sleepyz used missing logic from Mobius fractal pivots
#20230315 Sleepyz modified the VwapH and VwapL off the high/low respective of PivotH/PivotL
#20230317 Sleepyz added optional bubbles vs arrows at VwapH/VwapL

#hint: VWAP stops anchored off  fractalTrader pivots.

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

#hint ticks: Offset High/Low VWAP lines by this number of ticks.
input ticks  = 0.0;
def bn       = BarNumber();
def na       = Double.NaN;
def bnOK     = bn > 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 na;

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

def PHBar    = if !IsNaN(HH)
               then bn
               else PHBar[1];

def PLBar    = if !IsNaN(LL)
               then bn
               else PLBar[1];

def PHL     = if !IsNaN(HH)
              then HH
              else PHL[1];

def PLL     = if !IsNaN(LL)
              then LL
              else PLL[1];

def priorPHBar = if PHL != PHL[1]
                 then PHBar[1]
                 else priorPHBar[1];

def priorPLBar = if PLL != PLL[1]
                 then PLBar[1]
                 else priorPLBar[1];

def HighPivots = bn >= HighestAll(priorPHBar);
def LowPivots  = bn >= HighestAll(priorPLBar);

def PivH       = if !IsNaN(HH) > 0 then HighPivots else na;
def PivL       = if !IsNaN(LL) > 0 then LowPivots  else na;

def Up        = bn == PLBar;

AddChartBubble(showbubbles and bn == PLBar, low, AsText(low), Color.GREEN, no);

def Dn        = bn == PHBar;

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], na);
    VC = CompoundValue(1, volume + VC[1], na);
    PC2 = CompoundValue(1, LocC + PC2[1], na);
    VC2 = CompoundValue(1, volume + VC2[1], na);
}

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

def VwapC = if Dn[-1] or Up[-1] then na else PC / VC;
def VwapC2 = if Dn[-1] or Up[-1] then na else PC2 / VC2;
def VwapH = if Dn[-1] then na else PH / VH;
def VwapL = if Up[-1] then na else PL / VL;
def VwapH2 = if Dn[-1] then na else PH2 / VH2;
def VwapL2 = if Up[-1] then na else PL2 / VL2;

plot scan = close crosses below VwapC;
 
Last edited:
This will plot up to the last 4 Earning's Date Anchored VWAP's, extended to the last bar. The VWAPs will start either the day before or after based upon whether the earnings are before or after market hours respectively.
Holy crap, thank you so much dude. I was going full on derp mode trying to figure this out without repeated errors.
 
Yes, after modifying the script by renaming all of the plots as defs and defining a plot scan. As you did not indicate what you wanted in a scan, see an example in code bellow for one of many that can be done.
Thanks SleepyZ, it scans , but it seems to be delayed. It does not find current pivot low or pivot high
 
EDIT: removed the question about adding deviation bands to one of the VWAP indicators posted here. This is the more important question:

I saw NextSignals comment about the "excursion" to the deviation bands being +/-10 points. Here is the post image:

HIg4jcU.png


It wasn't clear to me how he's defining and plotting the excursions, it looks like each time a touch happens against a deviation band different than the previously touched one, the excursion count is re-started. It's an interesting idea and was wondering if anyone has ideas for how to code it. It doesn't seem like it would be that complicated.
 
Last edited:
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.DARK_RED);
VwapL.SetDefaultColor(Color.DARK_GREEN);
#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

Maybe I missed it in the thread... However, I have been trying to add a label to create a column in a watchlist to no avail. I'm using the adjustable date script. Any help would be appreciated!
 
Thanks SleepyZ, it scans , but it seems to be delayed. It does not find current pivot low or pivot high

The scan example I used of close crosses below VwapC works in real time interacting with the Anchored Vwap portion of the script.

A scan of pivots would be delayed by the input n. As it is defaulted to 20, it needs 20 bars on each side of the pivot before it is identified. There are numerous pivot codes that can be searched to find one that would be useful in scanning for pivots.
 
Hi. New to usethinkscript. I may have missed it but is there an AVWAP for use on intraday SPX? I have looked through this thread and tried a few things but can’t get to work. Any help would be appreciated.
 
Hi. New to usethinkscript. I may have missed it but is there an AVWAP for use on intraday SPX? I have looked through this thread and tried a few things but can’t get to work. Any help would be appreciated.

SPX does not have Volume. TOS does not seem willing to allow studies with Volume to appear on a SPX based chart.

A possible workaround to this is to use an /ES based chart, which mostly mirrors SPX and either use it instead or as a lower grid chart along
Screenshot 2023-05-28 111409.png
with the SPX chart.
 
Is it possible to get the anchored VWAP to begin plotting when LongEntry signals occurs and loop until LongExit?

declare hide_on_daily;

input anchorTime = 0930;
input anchorEnd = 1600;

plot AMA12 = AdaptiveEMA(length = 12);
plot AMA24 = AdaptiveEMA(length = 24);

def LongEntry = AMA12 crosses above AMA24;
def LongExit = AMA12 crosses below AMA24;

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);
 
The anchored vwap is based on the date and time of the candle.

I would use the GetTime/GetDate functions to retrieve that info for each of the signals and then connect it to the anchored vwap.
 
Here is optional price bubbles vs arrows at VwapH/VwapL
Sorry, I need help with understanding how to use this indicator.
It is greatly lagging with Buy vs Sell recommendations. Is it an expected behavior ?
I have created strategy and tested it during the market hours.
The trades are happening in the past! ...after 8+ bars on 5min chart
Am I doing something wrong ?
 
Sorry, I need help with understanding how to use this indicator.
It is greatly lagging with Buy vs Sell recommendations. Is it an expected behavior ?
I have created strategy and tested it during the market hours.
The trades are happening in the past! ...after 8+ bars on 5min chart
Am I doing something wrong ?

The vwap is anchored to a swing high/low pivot script. These type of scripts repaint as they require x number of bars before and after the pivot before it will plot. These are not usable as buy/sell recommendations or for back testing. The script just shows how to anchor the vwap on these pivots.
 
Hi. New to usethinkscript. I may have missed it but is there an AVWAP for use on intraday SPX? I have looked through this thread and tried a few things but can’t get to work. Any help would be appreciated.
TOS does not allow SPX volume data on it. I use what you are looking for on another platform with my TOS application scans. Unless the mods on here have changed allowing the posting of other platform names, no one can mention a competing platform on here. That seems kind of restrictive, but I can see both sides. One can use /ES volume as a workaround on TOS as mentioned with anchored vwap as a workaround.
 
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.

A strong uptrend is signaled when the price consistently stays above the VWAP and the VWAP itself is sloping upward. In a downtrend, the price tends to stay below the VWAP, which is also sloping downward.

When the price strays too far from the VWAP, there is an expectation that it will eventually move back towards the average.

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

View attachment 4532

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

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

View attachment 4534

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

Ben, first thanks for all the scripts you have shared! I am a rookie with ThinkScript. I love the VWAP Anchored_v02 that you shared. What I would like to do with this study though is to only show the last plot or maybe last two plots. Thanks again for everything that you do for the community.
 
TOS does not allow SPX volume data on it. I use what you are looking for on another platform with my TOS application scans. Unless the mods on here have changed allowing the posting of other platform names, no one can mention a competing platform on here. That seems kind of restrictive, but I can see both sides. One can use /ES volume as a workaround on TOS as mentioned with anchored vwap as a workaround.
Try these symbols for the market cap segments of the S&P 1500-$SP500, $SP400, and $SP600. They are the cash indexes with volume.
 
Ben, first thanks for all the scripts you have shared! I am a rookie with ThinkScript. I love the VWAP Anchored_v02 that you shared. What I would like to do with this study though is to only show the last plot or maybe last two plots. Thanks again for everything that you do for the community.

This uses the script that fixes an updating of the plot in real time problem to Ben's v02.

It now includes your request to limit the plots to the last x input anchors.

Screenshot 2023-07-13 115323.png
Code:
#START STUDY
#Anchored_VWAPv3_20230713
#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.
#20220708 used missing logic from Mobius fractal pivots
#20230713 plot limited to last x input added

#hint: VWAP stops anchored off  fractalTrader pivots.

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

#hint ticks: Offset High/Low VWAP lines by this number of ticks.
input ticks  = 2.0;
def bn       = BarNumber();
def na       = Double.NaN;
def bnOK     = bn > 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 na;

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

def PHBar    = if !IsNaN(HH)
               then bn
               else PHBar[1];

def PLBar    = if !IsNaN(LL)
               then bn
               else PLBar[1];

def PHL     = if !IsNaN(HH)
              then HH
              else PHL[1];

def PLL     = if !IsNaN(LL)
              then LL
              else PLL[1];

def priorPHBar = if PHL != PHL[1]
                 then PHBar[1]
                 else priorPHBar[1];

def priorPLBar = if PLL != PLL[1]
                 then PLBar[1]
                 else priorPLBar[1];

def HighPivots = bn >= HighestAll(priorPHBar);
def LowPivots  = bn >= HighestAll(priorPLBar);

def PivH       = if !IsNaN(HH) > 0 then HighPivots else na;
def PivL       = if !IsNaN(LL) > 0 then LowPivots  else na;


def cond      = if bn == PLBar or bn == PHBar then 1 else Double.NaN;
def dataCount = CompoundValue(1, if !IsNaN(cond) then dataCount[1] + 1 else dataCount[1], 0);

plot Up        =  if HighestAll(dataCount) - dataCount <= plot_limited_to_last_x - 1 then bn == PLBar  else Double.NaN;
Up.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
Up.SetLineWeight(3);
Up.SetDefaultColor(Color.WHITE);

plot Dn        = if HighestAll(dataCount) - dataCount <= plot_limited_to_last_x - 1 then  bn == PHBar else Double.NaN;
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], na);
    VC = CompoundValue(1, volume + VC[1], na);
    PC2 = CompoundValue(1, LocC + PC2[1], na);
    VC2 = CompoundValue(1, volume + VC2[1], na);
}

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

plot VwapC = if Dn or Up then na else PC / VC;
plot VwapC2 = if Dn or Up then na else PC2 / VC2;
plot VwapH = if Dn then na else PH / VH;
plot VwapL = if Up then na else PL / VL;
plot VwapH2 = if Dn then na else PH2 / VH2;
plot VwapL2 = if Up then na 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
;
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
293 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