Auto Anchored VWAP based on high/low with the Anchored period For ThinkOrSwim

JakeD

New member
Is it possible to create an Anchored VWAP script which automatically anchors at the charts High and/or Low ?
 

SleepyZ

Well-known member
VIP
Lifetime
Is it possible to create an Anchored VWAP script which automatically anchors at the charts High and/or Low ?

This will plot the vwap from the highestall/lowestall high/low of the chart.

Capture.jpg
Ruby:
def bn          = BarNumber();
def c           = close;
def v           = volume;
def vw          = vwap;
def anchorh;
def volumesumh;
def volumevwapsumh;
def volumevwap2sumh;
def priceh;
def deviationh;

#Highest High of Chart
anchorh = if high == HighestAll(high)
             then bn else anchorh[1];

volumesumh      = if bn >= HighestAll(anchorh)  then volumesumh[1] + volume else 0;
volumevwapsumh  = if bn >= HighestAll(anchorh)  then volumevwapsumh[1] + volume * vwap else 0;
volumevwap2sumh = if bn >= HighestAll(anchorh)  then volumevwap2sumh[1] + volume * Sqr(vwap) else 0;
priceh          = volumevwapsumh / volumesumh;
deviationh      = Sqrt(Max(volumevwap2sumh / volumesumh - Sqr(priceh), 0));


plot VWAPh      = priceh;
VWAPh.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 vwaph + numDev1 * deviationh;
plot LowerBand1  = if !showbands then Double.NaN else vwaph - numDev1 * deviationh;
plot UpperBand2  = if !showbands then Double.NaN else vwaph + numDev2 * deviationh;
plot LowerBand2  = if !showbands then Double.NaN else vwaph - numDev2 * deviationh;
plot UpperBand3  = if !showbands then Double.NaN else vwaph + numDev3 * deviationh;
plot LowerBand3  = if !showbands then Double.NaN else vwaph - numDev3 * deviationh;

vwaph.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);
vwaph.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);

#Lowest Low of Chart
def anchor1;
def volumesum1;
def volumevwapsum1;
def volumevwap2sum1;
def price1;
def deviation1;

anchor1 = if low == LowestAll(low)
         then bn else anchor1[1];
volumesum1      = if bn >= HighestAll(anchor1)  then volumesum1[1] + volume else 0;
volumevwapsum1  = if bn >= HighestAll(anchor1)  then volumevwapsum1[1] + volume * vwap else 0;
volumevwap2sum1 = if bn >= HighestAll(anchor1)  then volumevwap2sum1[1] + volume * Sqr(vwap) else 0;
price1          = volumevwapsum1 / volumesum1;
deviation1      = Sqrt(Max(volumevwap2sum1 / volumesum1 - Sqr(price1), 0));


plot VWAP1      = price1;
VWAP1.SetDefaultColor(GetColor(0));

plot UpperBand11  = if !showbands then Double.NaN else VWAP1 + numDev1 * deviation1;
plot LowerBand11  = if !showbands then Double.NaN else VWAP1 - numDev1 * deviation1;
plot UpperBand21  = if !showbands then Double.NaN else VWAP1 + numDev2 * deviation1;
plot LowerBand21  = if !showbands then Double.NaN else VWAP1 - numDev2 * deviation1;
plot UpperBand31  = if !showbands then Double.NaN else VWAP1 + numDev3 * deviation1;
plot LowerBand31  = if !showbands then Double.NaN else VWAP1 - numDev3 * deviation1;

VWAP1.SetDefaultColor(Color.CYAN);
UpperBand11.SetDefaultColor(Color.GREEN);
LowerBand11.SetDefaultColor(Color.RED);
UpperBand21.SetDefaultColor(Color.GREEN);
LowerBand21.SetDefaultColor(Color.RED);
UpperBand31.SetDefaultColor(Color.GREEN);
LowerBand31.SetDefaultColor(Color.RED);
VWAP1.HideBubble();
UpperBand11.HideBubble();
LowerBand11.HideBubble();
UpperBand21.HideBubble();
LowerBand21.HideBubble();
UpperBand31.HideBubble();
LowerBand31.HideBubble();


AddCloud(if showclouds then UpperBand31 else Double.NaN, UpperBand21, Color.LIGHT_GREEN, Color.LIGHT_GREEN);
AddCloud(if showclouds then LowerBand31 else Double.NaN, LowerBand21, Color.LIGHT_RED, Color.LIGHT_RED);
 

samer800

Well-known member
I just modified the code to show automated anchored VWAP based on the selected number of days. if select "0" days, will anchor the max high/low within the aggregation period. More over, I included options to show pervious HL, OC, premarket...etc.

haI2Bka.png



CSS:
# Auto Anchored VWAP based on high/low with the Anchored period
# [email protected] - 10/2022

declare upper;
declare hide_on_daily;

input ShowLabel = yes;
input DaysAgo   = 4;        # hint DaysAgo: Excludes today
input AnchorHigh  = yes;
input AnchorLow   = yes;
input DailyOpenRange = no;
input ORBegin = 0900;
input OREnd   = 0925;
input DailyOpen   = no;
input PreMarketHL = no;
input PreviousHL  = no;
input PreviousOC  = no;
input ShowTodayOnly = yes;
input showband1   = no;
input showband2   = no;
input showclouds  = yes;
input numDev1     = 1.0;
input numDev2     = 2.0;

def na = Double.NaN;
def bar = BarNumber();

def AdjDaysAgo = DaysAgo + 1; # Adjusted to match a true LastDate which includes today
def day = GetDay();
def lastDay = GetLastDay();
def year = GetYear();
def lastYear = GetLastYear();
def yyyymmdd = GetYYYYMMDD();
def lastDate = HighestAll( if day == lastDay and year == lastYear then yyyymmdd else na );
def currentDate = if yyyymmdd < lastDate then yyyymmdd else lastDate;
def tradingDaysAgo = CountTradingDays(currentDate, lastDate);
def previousDayPlus = tradingDaysAgo <= AdjDaysAgo;
def today = lastDate == currentDate;

def RTH =  SecondsFromTime(0930) >= 0 and SecondsTillTime(1600) >= 0;
def todayOnly = if !ShowTodayOnly or day == lastDay then 1 else 0;

def src = hlc3;
def vw  = vwap;
def o   = open;
def h   = high;
def l   = low;
def c   = close;
def v   = volume;
def Dayhigh  = high(period = "DAY");
def DayLow   = low(period = "DAY");
def DayClose = close(period = "DAY");
def DayOpen  = open(period = "DAY");

#### Support Resistance

########## Previous day High/Low Open/Close
#previousPlot(today, Condistion, source)=>
script previousPlot {
    input today = yes;
    input previousDayPlus = yes;
    input data = high;
    plot previousPlot = if previousDayPlus then
                    HighestAll(if today then data else Double.NaN)
                    else Double.NaN;
}
def pHigh  = previousPlot(today, PreviousHL and previousDayPlus, Dayhigh[DaysAgo]);
def pLow   = previousPlot(today, PreviousHL and previousDayPlus, DayLow[DaysAgo]);
def pClose = previousPlot(today, PreviousOC and previousDayPlus, DayClose[DaysAgo]);
def pOpen  = previousPlot(today, PreviousOC and previousDayPlus, DayOpen[DaysAgo]);

plot previousHigh  = If(IsNaN(c), na, pHigh);
plot PreviousLow   = If(IsNaN(c), na, pLow);
plot PreviousClose = If(IsNaN(c), na, pClose);
plot PreviousOpen  = If(IsNaN(c), na, pOpen);

previousHigh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
previousHigh.SetDefaultColor(Color.GREEN);

PreviousLow.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PreviousLow.SetDefaultColor(Color.RED);

PreviousClose.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PreviousClose.SetDefaultColor(Color.ORANGE);

PreviousOpen.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PreviousOpen.SetDefaultColor(Color.GRAY);

#=========== Pre/After Market ===================

def GlobeX = GetTime() < RegularTradingStart(GetYYYYMMDD());

def vol = if GlobeX and !GlobeX[1] then v else
          if GlobeX then vol[1] + v else na;

def GlobeX_Volume = vol;

def ONhigh = if GlobeX and !GlobeX[1] then h else
             if GlobeX and h > ONhigh[1] then h else ONhigh[1];

def ONhighBar = if GlobeX and h == ONhigh then bar else na;

def ONlow = if GlobeX and !GlobeX[1] then l else
            if GlobeX and l < ONlow[1] then l else ONlow[1];

def ONlowBar = if GlobeX and l == ONlow then bar else na;

def OverNightHigh = if bar == HighestAll(ONhighBar) then ONhigh else OverNightHigh[1];

def OverNightLow = if bar == HighestAll(ONlowBar) then ONlow else OverNightLow[1];

#
plot PreHigh = if OverNightHigh and PreMarketHL then If(IsNaN(c), na, OverNightHigh) else na;

PreHigh.SetPaintingStrategy(PaintingStrategy.DASHES);
PreHigh.SetDefaultColor(Color.LIGHT_GREEN);

#
plot PreLow = if OverNightLow and PreMarketHL then If(IsNaN(c), na, OverNightLow) else na;

PreLow.SetPaintingStrategy(PaintingStrategy.DASHES);
PreLow.SetDefaultColor(Color.LIGHT_RED);

# Daily Open High/Low

plot PlotDailyOpen = if RTH and todayOnly then DayOpen else na;
PlotDailyOpen.SetHiding(!DailyOpen);
PlotDailyOpen.SetDefaultColor(Color.WHITE);
PlotDailyOpen.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

################# premarket average ====

def ORActive = if SecondsTillTime(OREnd) >= 0 and SecondsFromTime(ORBegin) >= 0 then 1 else 0;

def SMAH = SimpleMovingAvg(h, 3);
def SMAM = SimpleMovingAvg(src,3);
def SMAL = SimpleMovingAvg(l, 3);

rec ORH = if !ORH[1] or !ORActive[1] and ORActive then SMAH else
          if ORActive and SMAH > ORH[1] then SMAH else ORH[1];
rec ORM = if !ORM[1] or !ORActive[1] and ORActive then SMAM else
          if ORActive and SMAM > ORM[1] then SMAM else ORM[1];
rec ORL = if !ORL[1] or !ORActive[1] and ORActive then SMAL else
          if ORActive and SMAL > ORL[1] then SMAL else ORL[1];

plot ORMedLine = if RTH and todayOnly then if(DailyOpenRange,ORM,na) else na;
def ORHHigh    = if RTH and todayOnly then ORH else na;
def ORHLow     = if RTH and todayOnly then ORL else na;

AddCloud(if DailyOpenRange then ORHHigh else na, ORHLow, Color.GRAY);
ORMedLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ORMedLine.SetDefaultColor(Color.CYAN);
ORMedLine.SetStyle(Curve.SHORT_DASH);

#--Anchored VWAP-----------------------
def anchorh;
def volumesumh;
def volumevwapsumh;
def volumevwap2sumh;
def priceh;
def deviationh;

def vHigh  = if DaysAgo <= 0 then HighestAll(h) else
             HighestAll(If(previousDayPlus and !today, h[DaysAgo], na));
def vLow   = if DaysAgo <= 0 then LowestAll(l) else
             LowestAll(If(previousDayPlus and !today, l[DaysAgo + 1], na));
#Highest High of Chart
anchorh = if h == vHigh then bar else anchorh[1];

volumesumh      = if bar >= HighestAll(anchorh)  then volumesumh[1] + v else 0;
volumevwapsumh  = if bar >= HighestAll(anchorh)  then volumevwapsumh[1] + v * vw else 0;
volumevwap2sumh = if bar >= HighestAll(anchorh)  then volumevwap2sumh[1] + v * Sqr(vw) else 0;
priceh          = volumevwapsumh / volumesumh;
deviationh      = Sqrt(Max(volumevwap2sumh / volumesumh - Sqr(priceh), 0));

plot VWAPh      = if(AnchorHigh,priceh,na);
VWAPh.SetDefaultColor(Color.LIME);
def UpBand1  = VWAPh + numDev1 * deviationh;
def LoBand1  = VWAPh - numDev1 * deviationh;
def UpBand2  = VWAPh + numDev2 * deviationh;
def LoBand2  = VWAPh - numDev2 * deviationh;

plot UpperBand1  = if !showband1 then na else UpBand1;
plot LowerBand1  = if !showband1 then na else LoBand1;
plot UpperBand2  = if !showband2 then na else UpBand2;
plot LowerBand2  = if !showband2 then na else LoBand2;

UpperBand1.SetDefaultColor(CreateColor(15, 51, 37));
LowerBand1.SetDefaultColor(CreateColor(65, 0, 0));
UpperBand2.SetDefaultColor(Color.DARK_GREEN);
LowerBand2.SetDefaultColor(CreateColor(95, 0, 0));

AddCloud(if showclouds and showband2 then UpBand2 else na, UpBand1, CreateColor(15, 51, 37), CreateColor(15, 51, 37));
AddCloud(if showclouds and showband2 then LoBand2 else na, LoBand1, CreateColor(65, 0, 0), CreateColor(65, 0, 0));

#Lowest Low of Chart
def anchor1;
def volumesum1;
def volumevwapsum1;
def volumevwap2sum1;
def price1;
def deviation1;

anchor1 = if l == vLow then bar else anchor1[1];
volumesum1      = if bar >= HighestAll(anchor1) then volumesum1[1] + v else 0;
volumevwapsum1  = if bar >= HighestAll(anchor1) then volumevwapsum1[1] + v * vw else 0;
volumevwap2sum1 = if bar >= HighestAll(anchor1) then volumevwap2sum1[1] + v * Sqr(vw) else 0;
price1          = volumevwapsum1 / volumesum1;
deviation1      = Sqrt(Max(volumevwap2sum1 / volumesum1 - Sqr(price1), 0));

plot VWAP1      = if(AnchorLow,price1,na);
VWAP1.SetDefaultColor(Color.PINK);

def UpBand11  = VWAP1 + numDev1 * deviation1;
def LoBand11  = VWAP1 - numDev1 * deviation1;
def UpBand21  = VWAP1 + numDev2 * deviation1;
def LoBand21  = VWAP1 - numDev2 * deviation1;

plot UpperBand11  = if !showband1 then na else UpBand11;
plot LowerBand11  = if !showband1 then na else LoBand11;
plot UpperBand21  = if !showband2 then na else UpBand21;
plot LowerBand21  = if !showband2 then na else LoBand21;

#VWAP1.SetDefaultColor(Color.CYAN);
UpperBand11.SetDefaultColor(CreateColor(15, 51, 37));
LowerBand11.SetDefaultColor(CreateColor(65, 0, 0));
UpperBand21.SetDefaultColor(Color.DARK_GREEN);
LowerBand21.SetDefaultColor(CreateColor(95, 0, 0));

AddCloud(if showclouds and showband2 then UpBand21 else na, UpBand11, CreateColor(15, 51, 37), CreateColor(15, 51, 37));
AddCloud(if showclouds and showband2 then LoBand21 else na, LoBand11, CreateColor(65, 0, 0), CreateColor(65, 0, 0));

AddLabel(ShowLabel, "Anchor days(" + DaysAgo + ")", Color.WHITE);

#---- End Code
 

HedgeFundOfOne

New member
Thank you for the code. I've been able to get the plots to appear on intraday charts but not on daily or weekly charts.
I tried specifying 130 days ago
Are there specific parameters that would place the plots on daily/weekly charts?
Does the hide_on_daily variable somehow specifically exclude daily/weekly plots? (I didn't see it referenced anywhere in the code after its def statment.)
 

samer800

Well-known member
Thank you for the code. I've been able to get the plots to appear on intraday charts but not on daily or weekly charts.
I tried specifying 130 days ago
Are there specific parameters that would place the plots on daily/weekly charts?
Does the hide_on_daily variable somehow specifically exclude daily/weekly plots? (I didn't see it referenced anywhere in the code after its def statment.)
above code for intraday and can't be used for daily aggregation. use below code if you want to use for any time frame but will be auomated based on highest price and lowest price.

CSS:
#AutoAnchored VWAP based on High/low
# Created by [email protected] - 10/2022
#--Anchored VWAP-----------------------
input AnchorHigh = yes;
input AnchorLow = yes;
input numDev1     = 1.0;
input numDev2     = 2.0;
input showclouds = no;
input showband1 = no;
input showband2 = no;

def na = Double.NaN;
def bar = BarNumber();

def vw  = vwap;
def o   = open;
def h   = high;
def l   = low;
def c   = close;
def v   = volume;

def anchorh;
def volumesumh;
def volumevwapsumh;
def volumevwap2sumh;
def priceh;
def deviationh;

def vHigh  = HighestAll(h);
def vLow   = LowestAll(l);
#Highest High of Chart
anchorh = if h == vHigh then bar else anchorh[1];

volumesumh      = if bar >= HighestAll(anchorh)  then volumesumh[1] + v else 0;
volumevwapsumh  = if bar >= HighestAll(anchorh)  then volumevwapsumh[1] + v * vw else 0;
volumevwap2sumh = if bar >= HighestAll(anchorh)  then volumevwap2sumh[1] + v * Sqr(vw) else 0;
priceh          = volumevwapsumh / volumesumh;
deviationh      = Sqrt(Max(volumevwap2sumh / volumesumh - Sqr(priceh), 0));

plot VWAPh      = If(AnchorHigh, priceh, na);
VWAPh.SetDefaultColor(Color.LIME);
def UpBand1  = VWAPh + numDev1 * deviationh;
def LoBand1  = VWAPh - numDev1 * deviationh;
def UpBand2  = VWAPh + numDev2 * deviationh;
def LoBand2  = VWAPh - numDev2 * deviationh;

plot UpperBand1  = if !showband1 then na else UpBand1;
plot LowerBand1  = if !showband1 then na else LoBand1;
plot UpperBand2  = if !showband2 then na else UpBand2;
plot LowerBand2  = if !showband2 then na else LoBand2;

UpperBand1.SetDefaultColor(CreateColor(15, 51, 37));
LowerBand1.SetDefaultColor(CreateColor(65, 0, 0));
UpperBand2.SetDefaultColor(Color.DARK_GREEN);
LowerBand2.SetDefaultColor(CreateColor(95, 0, 0));

AddCloud(if showclouds and showband2 then UpBand2 else na,   UpBand1,   CreateColor(15, 51, 37),   CreateColor(15, 51, 37));
AddCloud(if showclouds and showband2 then LoBand2 else na,   LoBand1,   CreateColor(65, 0, 0),   CreateColor(65, 0, 0));

#Lowest Low of Chart
def anchor1;
def volumesum1;
def volumevwapsum1;
def volumevwap2sum1;
def price1;
def deviation1;

anchor1 = if l == vLow then bar else anchor1[1];
volumesum1      = if bar >= HighestAll(anchor1) then volumesum1[1] + v else 0;
volumevwapsum1  = if bar >= HighestAll(anchor1) then volumevwapsum1[1] + v * vw else 0;
volumevwap2sum1 = if bar >= HighestAll(anchor1) then volumevwap2sum1[1] + v * Sqr(vw) else 0;
price1          = volumevwapsum1 / volumesum1;
deviation1      = Sqrt(Max(volumevwap2sum1 / volumesum1 - Sqr(price1), 0));

plot VWAP1      = If(AnchorLow, price1, na);
VWAP1.SetDefaultColor(Color.PINK);

def UpBand11  = VWAP1 + numDev1 * deviation1;
def LoBand11  = VWAP1 - numDev1 * deviation1;
def UpBand21  = VWAP1 + numDev2 * deviation1;
def LoBand21  = VWAP1 - numDev2 * deviation1;

plot UpperBand11  = if !showband1 then na else UpBand11;
plot LowerBand11  = if !showband1 then na else LoBand11;
plot UpperBand21  = if !showband2 then na else UpBand21;
plot LowerBand21  = if !showband2 then na else LoBand21;

#VWAP1.SetDefaultColor(Color.CYAN);
UpperBand11.SetDefaultColor(CreateColor(15, 51, 37));
LowerBand11.SetDefaultColor(CreateColor(65, 0, 0));
UpperBand21.SetDefaultColor(Color.DARK_GREEN);
LowerBand21.SetDefaultColor(CreateColor(95, 0, 0));

AddCloud(if showclouds and showband2 then UpBand21 else na,   UpBand11,   CreateColor(15, 51, 37),   CreateColor(15, 51, 37));
AddCloud(if showclouds and showband2 then LoBand21 else na,   LoBand11,   CreateColor(65, 0, 0),   CreateColor(65, 0, 0));

#---END CODE
 

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.
Top