Hourly Vwap

Status
Not open for further replies.
is there any way to do an hourly vwap similar to the one in the bookmap?
look at this on a 5 minute chart so, its looking back 12 5 minute candles which is an hour

Code:
# Identify - Input Bars Back 

# Mobius 
#German BURRITO


 

input BarsBack = 12; 

 

def currentBar = if isnan(close[-1]) and !isnan(close)   

then barnumber()   

else currentBar[1]; 

def barCount = if barnumber()== 1   

then highestall(currentBar)   

else if barCount[1] > 1   

then barCount[1] - 1   

else 0; 

plot count = if barCount == BarsBack then barCount else double.nan; 

count.SetPaintingStrategy(PaintingStrategy.Values_Below); 

plot Number1 = if barCount == barsBack then low - (3* TickSize()) else double.nan; 

Number1.SetStyle(Curve.Points); 

Number1.SetLineWeight(5); 

Number1.SetDefaultColor(Color.Pink); 



def LocH = (hl2) * volume;
def LocL = (hl2) * volume;
rec PH;
rec VH;


if count {
    PH = LocH;
    VH = volume;
} else {
    PH = compoundValue(1, LocH + PH[1], Double.NaN);
    VH = compoundValue(1, volume + VH[1], Double.NaN);
}

plot Vw =  PH / VH;
 
look at this on a 5 minute chart so, its looking back 12 5 minute candles which is an hour

Code:
# Identify - Input Bars Back

# Mobius
#German BURRITO




input BarsBack = 12;



def currentBar = if isnan(close[-1]) and !isnan(close)  

then barnumber()  

else currentBar[1];

def barCount = if barnumber()== 1  

then highestall(currentBar)  

else if barCount[1] > 1  

then barCount[1] - 1  

else 0;

plot count = if barCount == BarsBack then barCount else double.nan;

count.SetPaintingStrategy(PaintingStrategy.Values_Below);

plot Number1 = if barCount == barsBack then low - (3* TickSize()) else double.nan;

Number1.SetStyle(Curve.Points);

Number1.SetLineWeight(5);

Number1.SetDefaultColor(Color.Pink);



def LocH = (hl2) * volume;
def LocL = (hl2) * volume;
rec PH;
rec VH;


if count {
    PH = LocH;
    VH = volume;
} else {
    PH = compoundValue(1, LocH + PH[1], Double.NaN);
    VH = compoundValue(1, volume + VH[1], Double.NaN);
}

plot Vw =  PH / VH;
Thanks for posting the script, is this line or a label? I copied the script but nothing showed up?
 
is there any way to do an hourly vwap similar to the one in the bookmap?

Here is something I did quite awhile ago that may help. There are a lot of options or your can remove what you do not wan added limited lines and full lines # instead of normal curves
Code:
#####################################################################
#VWAP_Limited_Full_Lines 2011.11.12 14:45pst zzz added limited lines and full lines #     instead of normal curves
#VWAP Study: thinkorswim, inc. (c) 2011
# 2012-02-06  duplicated the thinkorswim code
#             modified some thinkorswim variable names
#             modified default colors for plotting
#             and added code for 2nd deviation
#
input num1stDevUp = 1.0;
input num2ndDevUp = 2.0;
input num3rdDevUp = 3.0;
def num1stDevDn   = -num1stDevUp;
def num2ndDevDn   = -num2ndDevUp;
def num3rdDevDn   = -num3rdDevUp;
input multiplier  = 1.0;#Hint multiplier: Change to +/- number of "Days", "Weeks", etc
input lines       = {default limited, horizontal, lines};

input timeFrame = {MINUTE, MIN2, MIN3, MIN5, MIN10, MIN15, MIN20, MIN30, default HOUR, TWOHOUR, FOURHOUR, DAY, WEEK, MONTH, CHART};

def cap = GetAggregationPeriod();
def errorInAggregation =
    timeFrame == timeFrame.DAY and cap >= AggregationPeriod.WEEK or
    timeFrame == timeFrame.WEEK and cap >= AggregationPeriod.MONTH;
Assert(!errorInAggregation, "vwap timeFrame should be not less than current chart aggregation period");

def yyyyMmDd = GetYYYYMMDD();
def periodIndx;
def seconds = SecondsFromTime(0);
def minutes;
def day_number = DaysFromDate(First(yyyyMmDd)) + GetDayOfWeek(First(yyyyMmDd));
switch (timeFrame) {
case MINUTE:
    periodIndx = Floor(seconds / 60 + day_number * 24 * 60);
    minutes    = 1;
case MIN2:
    periodIndx = Floor(seconds / 120 + day_number * 24);
    minutes    = 2;
case MIN3:
    periodIndx = Floor(seconds / 180 + day_number * 24);
    minutes    = 3;
case MIN5:
    periodIndx = Floor(seconds / 300 + day_number * 24);
    minutes    = 5;
case MIN10:
    periodIndx = Floor(seconds / 600 + day_number * 24);
    minutes    = 10;
case MIN15:
    periodIndx = Floor(seconds / 900 + day_number * 24);
    minutes    = 15;
case MIN20:
    periodIndx = Floor(seconds / 1200 + day_number * 24);
    minutes    = 20;
case MIN30:
    periodIndx = Floor(seconds / 1800 + day_number * 24);
    minutes    = 30;
case HOUR:
    periodIndx = Floor(seconds / 3600 + day_number * 24);
    minutes    = 60;
case TWOHOUR:
    periodIndx = Floor(seconds / 7200 + day_number * 24);
    minutes    = 120;
case FOURHOUR:
    periodIndx = Floor(seconds / 14400 + day_number * 24);
    minutes    = 240;
case DAY:
    periodIndx = CountTradingDays(Min(First(yyyyMmDd), yyyyMmDd), yyyyMmDd) - 1;
    minutes    = 720;
case WEEK:
    periodIndx = Floor((DaysFromDate(First(yyyyMmDd)) + GetDayOfWeek(First(yyyyMmDd))) / 7);
    minutes    = 3600;
case MONTH:
    periodIndx = RoundDown(yyyyMmDd / 100, 0);
    minutes    = 15120;
case CHART:
    periodIndx = 0;
    minutes    = 0;
}
def countindx = CompoundValue(1, if periodIndx != periodIndx[1] then (countindx[1] + periodIndx - periodIndx[1]) % multiplier else countindx[1], 0);
def isPeriodRolled = countindx < countindx[1] + periodIndx - periodIndx[1];
#def isPeriodRolled = CompoundValue(1, periodIndx != periodIndx[1], yes);

input num_vwap_displayed = 2000;
def Count = num_vwap_displayed;
def cond = if isPeriodRolled
           then 1
           else Double.NaN;
def dataCount = CompoundValue(1, if !IsNaN(cond)
                                 then dataCount[1] + 1
                                 else dataCount[1], 0);

rec volumeSum;
rec volumeVwapSum;
rec 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 = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(price), 0));

rec v  = if IsNaN(vwap) then v[1]  else price;
rec v1 = if IsNaN(vwap) then v1[1] else price + num1stDevUp * deviation;
rec v2 = if IsNaN(vwap) then v2[1] else price - num1stDevUp * deviation;
rec v3 = if IsNaN(vwap) then v3[1] else price + num2ndDevUp * deviation;
rec v4 = if IsNaN(vwap) then v4[1] else price - num2ndDevUp * deviation;
rec v5 = if IsNaN(vwap) then v5[1] else price + num3rdDevUp * deviation;
rec v6 = if IsNaN(vwap) then v6[1] else price - num3rdDevUp * deviation;
plot VWAP;
plot FirstUpperBand;
plot FirstLowerBand;
plot SecondUpperBand;
plot SecondLowerBand;
plot ThirdUpperBand;
plot ThirdLowerBand;
input showlineinchartlength = 5;
input showlines = yes;
if (!errorInAggregation) and
    lines == lines.limited {
    VWAP = if IsNaN(close)
                      then Double.NaN
                      else if IsNaN(close[-showlineinchartlength]) and
                              HighestAll(dataCount) - dataCount <= Count - 1
                      then v
                      else Double.NaN ;
    FirstUpperBand  = if IsNaN(close[-showlineinchartlength]) and
                         HighestAll(dataCount) - dataCount <= Count - 1
                      then v1
                      else Double.NaN ;
    FirstLowerBand  = if IsNaN(close[-showlineinchartlength]) and
                         HighestAll(dataCount) - dataCount <= Count - 1
                      then v2
                      else Double.NaN ;
    SecondUpperBand = if IsNaN(close[-showlineinchartlength]) and
                         HighestAll(dataCount) - dataCount <= Count - 1
                      then v3
                      else Double.NaN ;
    SecondLowerBand = if IsNaN(close[-showlineinchartlength]) and
                         HighestAll(dataCount) - dataCount <= Count - 1
                      then v4
                      else Double.NaN ;
    ThirdUpperBand  = if IsNaN(close[-showlineinchartlength]) and
                         HighestAll(dataCount) - dataCount <= Count - 1
                      then v5
                      else Double.NaN ;
    ThirdLowerBand  = if IsNaN(close[-showlineinchartlength]) and
                         HighestAll(dataCount) - dataCount <= Count - 1
                      then v6
                      else Double.NaN ;
} else if (!errorInAggregation) and
           lines == lines.horizontal  and
           HighestAll(dataCount) - dataCount <= Count - 1 {
    VWAP = HighestAll(if IsNaN(close) then v else Double.NaN );
    FirstUpperBand  = HighestAll(if IsNaN(close) then v1 else Double.NaN );
    FirstLowerBand  = HighestAll(if IsNaN(close) then v2 else Double.NaN );
    SecondUpperBand = HighestAll(if IsNaN(close) then v3 else Double.NaN );
    SecondLowerBand = HighestAll(if IsNaN(close) then v4 else Double.NaN );
    ThirdUpperBand  = HighestAll(if IsNaN(close) then v5 else Double.NaN );
    ThirdLowerBand  = HighestAll(if IsNaN(close) then v6 else Double.NaN );
} else if (!errorInAggregation) and
           lines == lines.lines  and
           HighestAll(dataCount) - dataCount <= Count - 1 {
    VWAP            = price;
    FirstUpperBand  = price + num1stDevUp * deviation;
    FirstLowerBand  = price - num1stDevUp * deviation;
    SecondUpperBand = price + num2ndDevUp * deviation;
    SecondLowerBand = price - num2ndDevUp * deviation;
    ThirdUpperBand  = price + num3rdDevUp * deviation;
    ThirdLowerBand  = price - num3rdDevUp * deviation;
} else {
    VWAP            = Double.NaN;
    FirstUpperBand  = Double.NaN;
    FirstLowerBand  = Double.NaN;
    SecondUpperBand = Double.NaN;
    SecondLowerBand = Double.NaN;
    ThirdUpperBand  = Double.NaN;
    ThirdLowerBand  = Double.NaN;
}

#VWAP.SetDefaultColor(Color.BLUE);
VWAP.AssignValueColor(if VWAP > VWAP[1]
                      then Color.GREEN
                      else Color.RED);
FirstUpperBand.SetDefaultColor(Color.BLUE);
FirstLowerBand.SetDefaultColor(Color.BLUE);
SecondUpperBand.SetDefaultColor(Color.BLUE);
SecondLowerBand.SetDefaultColor(Color.BLUE);
ThirdUpperBand.SetDefaultColor(Color.BLUE);
ThirdLowerBand.SetDefaultColor(Color.BLUE);

VWAP.SetPaintingStrategy(PaintingStrategy.LINE);
FirstUpperBand.SetStyle(Curve.SHORT_DASH);
FirstLowerBand.SetStyle(Curve.SHORT_DASH);
SecondUpperBand.SetStyle(Curve.SHORT_DASH);
SecondLowerBand.SetStyle(Curve.SHORT_DASH);
ThirdUpperBand.SetStyle(Curve.SHORT_DASH);
ThirdLowerBand.SetStyle(Curve.SHORT_DASH);

VWAP.SetLineWeight(2);
FirstUpperBand.SetLineWeight(1);
FirstLowerBand.SetLineWeight(1);
SecondUpperBand.SetLineWeight(1);
SecondLowerBand.SetLineWeight(1);
ThirdUpperBand.SetLineWeight(1);
ThirdLowerBand.SetLineWeight(1);

VWAP.HideBubble();
FirstUpperBand.HideBubble();
FirstLowerBand.HideBubble();
SecondUpperBand.HideBubble();
SecondLowerBand.HideBubble();
ThirdUpperBand.HideBubble();
ThirdLowerBand.HideBubble();

FirstUpperBand.SetHiding(!showlines);
FirstLowerBand.SetHiding(!showlines);
SecondUpperBand.SetHiding(!showlines);
SecondLowerBand.SetHiding(!showlines);
ThirdUpperBand.SetHiding(!showlines);
ThirdLowerBand.SetHiding(!showlines);

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

AddChartBubble(showbubblesVSTD and IsNaN(close[p]) and !IsNaN(close[p1]) ,
               close[p1],
              "VSTD" + "\n" + AsPercent((close[p1] - price[p1]) / deviation[p1] / 100) +
              "\n" + Round(close[p1], 2) ,
               if close[p1] > VWAP[p1]
               then if close[p1] > close[p1 + 1]
                    then Color.GREEN
                    else Color.LIGHT_GREEN
               else if close[p1] > close[p1 + 1]
                    then Color.LIGHT_RED
               else Color.RED );

input showclouds = yes;
def green = if low >= VWAP and close > open
            then 1
            else if green[1] == 1 and high >= VWAP
            then 1
            else if green[1] == 1 and close > open   
            then 1
            else 0;

def g = !isnan(green);

AddCloud(if showclouds and g
         then ThirdUpperBand
         else Double.NaN,
         VWAP,
         Color.LIGHT_GRAY, Color.LIGHT_GRAY);
AddCloud(if showclouds and !g
         then VWAP
         else Double.NaN,
         ThirdLowerBand,
         Color.PINK, Color.PINK);
plot x = if showclouds
        then if g
             then ThirdUpperBand
             else ThirdLowerBand
        else Double.NaN;
x.AssignValueColor(if g
                   then Color.GREEN
                   else Color.RED);
input showbubblescurrent = no;
input bubblemovercurrent = 5;#Hint bubblemoverVSTD: Number of Spaces Above/Below VWAP bubble offset in expansion
def q = bubblemovercurrent;
def q1 = q + 1;

AddChartBubble(showbubblescurrent and IsNaN(close[q]) and !IsNaN(close[q1]) ,
               close[q1],
              "V" ,
               if g[q1]
               then Color.GREEN
               else Color.RED );

input usealerts = yes;
Alert(usealerts and g[1] != g, "VWAP - " + if g == 1 then "Green" else "Red", Alert.BAR, Sound.Chimes);
# end of VWAP Study
#Vertical Line @VWAP timeframe
input showverticalline = yes;
input begin   = 0930;
input end     = 1600;
def start     = if (timeFrame == timeFrame.TWOHOUR or timeFrame == timeFrame.FOURHOUR)
                then SecondsTillTime(0800) == 0 and
                     SecondsFromTime(0800) == 0
                else if timeFrame == timeFrame.HOUR
                then SecondsTillTime(0900) == 0 and
                     SecondsFromTime(0900) == 0
                else SecondsTillTime(0930) == 0 and
                     SecondsFromTime(0930) == 0;
def bar       = if start
                then 0
                else bar[1] + 1;

AddVerticalLine(if showverticalline and GetDay() == GetLastDay() and SecondsFromTime(begin) >= 0
                then bar % ((Ceil(minutes) * multiplier) / (GetAggregationPeriod() / 60000)) == 0
                else Double.NaN,
                color = Color.BLUE, stroke = Curve.FIRM);
Capture.jpg
 
Last edited:
How to use Compound here.. trying to generate VWAP for last few bars ... instead of the whole day..

def volsum = volume + volume[1]+ volume[2]+ volume[3]+ volume[4]+ volume[5];
def vwapsum = volume*close + volume[1]*close[1] + volume[2]*close[2] + volume[3]*close[3] + volume[4]*close[4] + volume[5]*close[5];
plot myvwap = vwapsum/volsum;
 
Last edited:
How to we use Compound here.. trying to generate VWAP for last few bars ... instead of the whole day..

def volsum = volume + volume[1]+ volume[2]+ volume[3]+ volume[4]+ volume[5];
def vwapsum = volume*close + volume[1]*close[1] + volume[2]*close[2] + volume[3]*close[3] + volume[4]*close[4] + volume[5]*close[5];
plot myvwap = vwapsum/volsum;
hmmm... interesting we can definitely explore this idea in other ways. Good idea!
 
How to we use Compound here.. trying to generate VWAP for last few bars ... instead of the whole day..

def volsum = volume + volume[1]+ volume[2]+ volume[3]+ volume[4]+ volume[5];
def vwapsum = volume*close + volume[1]*close[1] + volume[2]*close[2] + volume[3]*close[3] + volume[4]*close[4] + volume[5]*close[5];
plot myvwap = vwapsum/volsum;
One way is using my code above, which you can copy and use to limit the plot as you suggest. There is an input option 'lines', which I set to limit and 'showlinesinchartlength' to '5'. You can turn off the vertical lines at the input also if you want. Here is the result using the above inputs
Capture.jpg
 
hmmm... interesting we can definitely explore this idea in other ways. Good idea!

If you want a simpler way to limit the plot of the TOS standard vwap then this might help

Code:
input xbars = 5;
def lastbar = HighestAll(if !IsNaN(close) and IsNaN(close[-1]) then BarNumber() else Double.NaN);
plot vwap   = if BarNumber() < lastbar - xbars
              then Double.NaN
              else reference VWAP();
plot vwapu  = if BarNumber() < lastbar - xbars
              then Double.NaN
              else reference VWAP().UpperBand;
plot vwapl  = if BarNumber() < lastbar - xbars
              then Double.NaN
              else reference VWAP().LowerBand;
 
Is there any way to do a market profile (TPO) like how this vwap is setup? (i know you can do it by time segments)
Code:
# Identify - Input Bars Back

# Mobius
#German BURRITO


 

input BarsBack = 12;

 

def currentBar = if isnan(close[-1]) and !isnan(close)  

then barnumber()  

else currentBar[1];

def barCount = if barnumber()== 1  

then highestall(currentBar)  

else if barCount[1] > 1  

then barCount[1] - 1  

else 0;

plot count = if barCount == BarsBack then barCount else double.nan;

count.SetPaintingStrategy(PaintingStrategy.Values_Below);

plot Number1 = if barCount == barsBack then low - (3* TickSize()) else double.nan;

Number1.SetStyle(Curve.Points);

Number1.SetLineWeight(5);

Number1.SetDefaultColor(Color.Pink);



def LocH = (hl2) * volume;
def LocL = (hl2) * volume;
rec PH;
rec VH;


if count {
    PH = LocH;
    VH = volume;
} else {
    PH = compoundValue(1, LocH + PH[1], Double.NaN);
    VH = compoundValue(1, volume + VH[1], Double.NaN);
}

plot Vw =  PH / VH;
 
Is there any way to do a market profile (TPO) like how this vwap is setup? (i know you can do it by time segments)

See if this helps

Code:
input xbars = 12;
def lastbar = HighestAll(if !IsNaN(close) and IsNaN(close[-1]) then BarNumber() else Double.NaN);
plot vwap   = if BarNumber() < lastbar - xbars
              then Double.NaN
              else reference TPOProfile("time per profile" = "HOUR", "on expansion" = No);
plot vwapu  = if BarNumber() < lastbar - xbars
              then Double.NaN
              else reference TPOProfile("time per profile" = "HOUR", "on expansion" = No).VAHigh;
plot vwapl  = if BarNumber() < lastbar - xbars
              then Double.NaN
              else reference TPOProfile("time per profile" = "HOUR", "on expansion" = No).VALow;
 
TonXas said:
Is there any way to do a market profile (TPO) like how this vwap is setup? (i know you can do it by time segments)

Also try this. I have not determined whether it produces correct results, but it plots in the direction of the trend on the one's I checked

Code:
# Identify - Input Bars Back

# Mobius
#German BURRITO
#Sleepyz - mod changing tpoprofile for volume

 

input BarsBack = 12;



def currentBar = if IsNaN(close[-1]) and !IsNaN(close) 

then BarNumber() 

else currentBar[1];

def barCount = if BarNumber() == 1 

then HighestAll(currentBar) 

else if barCount[1] > 1 

then barCount[1] - 1 

else 0;

plot count = if barCount == BarsBack then barCount else Double.NaN;

count.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);

plot Number1 = if barCount == BarsBack then low - (3 * TickSize()) else Double.NaN;

Number1.SetStyle(Curve.POINTS);

Number1.SetLineWeight(5);

Number1.SetDefaultColor(Color.PINK);


input timePerProfile = {default CHART, MINUTE, HOUR, DAY, WEEK, MONTH, "OPT EXP", BAR};
def tpo = reference TPOProfile("time per profile" = timeperprofile, "on expansion" = no);

def LocH = (hl2) * tpo ;
def LocL = (hl2) * tpo;
rec PH;
rec VH;


if count {
    PH = LocH;
    VH = tpo;
} else {
    PH = CompoundValue(1, LocH + PH[1], Double.NaN);
    VH = CompoundValue(1, tpo + VH[1], Double.NaN);
}

plot Vw =  PH / VH;
 
Status
Not open for further replies.

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
339 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