Anchored VWAP Indicator for ThinkorSwim

D

dolomick

Member
If I am Pacific Time, I assume I should change the code to define start and end times as 6:30 and 13:00, correct?
 
BenTen

BenTen

Administrative
Staff
VIP
Warehouse
@dolomick ThinkorSwim uses Eastern Time. So you would still have to use 9:30.
 
T

Talochka

Member
please let me know, if it works on tick charts?
 
Last edited by a moderator:
BenTen

BenTen

Administrative
Staff
VIP
Warehouse
@Talochka You will know as soon as you add it to a Tick chart ;)
 
J

joenors

New member
I'm trying to create an anchored VWAP indicator that would work for futures extended hours. The idea is to specify an input anchor time using 24-hour clock notation in the EST timezone. The VWAP would start plotting each day at the anchor time, which may or may not be on the previous calendar day, and may be in either the extended or regular hours session. The VWAP would stop plotting each day at the time specified by the RegularTradingEnd method. This should work no matter the specific futures contract being plotted nor the trading hours of the contract.

For example, for the /ES contract I believe the extended hours of a trading day starts at 6:00 pm the previous calendar day and runs through 9:30 am, and the regular hours session runs from 9:30 am to 5:00 pm. So, an anchor time anywhere from 1800 (previous calendar day) to 1659 would plot until 5:00 pm (1700), and then the VWAP would start over the next day again anchored at the same anchor time for the next day.

The problem I'm having is identifying the correct bars on which to calculate the VWAP.

Does anyone have any tips on how this can be accomplished?
 
Last edited:
J

joenors

New member
This seems to work reasonably well...
Code:
declare hide_on_daily;

input numDevDn = -2.0;
input numDevUp = 2.0;

input shouldPlotDevDn = yes;
input shouldPlotDevUp = yes;

# The default is TOS' vwap, but could use CLOSE, OHLC4, or some other price.
input vwap_Type = vwap;

input anchorTimeEastern = 2100;

# This specifies whether the current time is after the anchor time in
# the current calendar day (not after the anchor time within
# the current trading day).
def afterAnchorTime = SecondsFromTime(anchorTimeEastern) >= 0;

# Counts the number of bars since the previous trading day.
# The first bar of the new day will equal 1.
# Iterates from the current bar backward up to 1600 bars (more
# than then number of minutes in a day) counting each bar until
# the current bar is a different trading day.
def barCountFromPreviousTradingDay =
 fold relativeBarIndex = 0 to 1600
  with barCount = 1
   while GetValue(GetYYYYMMDD(), relativeBarIndex) == GetValue(GetYYYYMMDD(), relativeBarIndex + 1)
   do barCount + 1;

# Counts the number of bars since the anchor time.
# The first bar whose start time is greater than or equal
# to the anchor time will equal zero.
def barCountFromAnchorTime =
 fold relativeBarIndex_at = 0 to 1600
  with barCount_fromAnchorTime
   while GetValue(SecondsFromTime(anchorTimeEastern), relativeBarIndex_at) > 0
   do barCount_fromAnchorTime + 1;

# Since afterAnchorTime only checks to see if the current bar is after the anchor
# time of the current calendar day, this variable is needed to track whether after
# the anchor time within the current trading day (which includes regular and
# extended hours). To be true, the bar has to be after the anchor time of the current
# calendar day and that anchor time cannot be in the previous trading day.
# To account for anchor times in the previous calendar day that are not in the
# previous trading day (i.e. valid to plot), the second line will continue plotting
# once started until the trading day changes.
def shouldPlot_VWAP =
 (afterAnchorTime and (barCountFromPreviousTradingDay > barCountFromAnchorTime)) or
 (shouldPlot_VWAP[1] and barCountFromPreviousTradingDay != 1);

# If previous bar didn't plot VWAP, but current bar should, then this is the
# first bar after a valid anchor time. The OR conditional is needed to handle
# the situation where the anchor time is the first bar of the trading day,
# because the previous bar would have been plotted as well in that scenario.
def isFirstBarAfterAnchorTime =
 CompoundValue(
  1,
  (shouldPlot_VWAP != shouldPlot_VWAP[1]) or (shouldPlot_VWAP and barCountFromPreviousTradingDay == 1),
  yes);

def volumeSum;
def volumeVwapSum;
def volumeVwap2Sum;

# Basically TOS' VWAP study calculation below

if (isFirstBarAfterAnchorTime) {
    volumeSum = volume;
    volumeVwapSum = volume * vwap_Type;
    volumeVwap2Sum = volume * Sqr(vwap_Type);
} else {
    volumeSum = CompoundValue(1, volumeSum[1] + volume, volume);
    volumeVwapSum = CompoundValue(1, volumeVwapSum[1] + volume * vwap_Type, volume * vwap_Type);
    volumeVwap2Sum = CompoundValue(1, volumeVwap2Sum[1] + volume * Sqr(vwap_Type), volume * Sqr(vwap_Type));
}

plot VWAP = if shouldPlot_VWAP then (volumeVwapSum / volumeSum) else Double.NaN;
def deviation = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(VWAP), 0));

#plot VWAP = price;
plot UpperBand = if shouldPlotDevUp then VWAP + numDevUp * deviation else Double.NaN;
plot LowerBand = if shouldPlotDevDn then VWAP + numDevDn * deviation else Double.NaN;

VWAP.SetDefaultColor(Color.CYAN);
UpperBand.SetDefaultColor(Color.GRAY);
LowerBand.SetDefaultColor(Color.GRAY);

VWAP.SetLineWeight(3);
UpperBand.SetLineWeight(1);
LowerBand.SetLineWeight(1);
 
M

mike01

New member
Hi I am looking for the code for TOS so that I can add a 1 hour vWAP line to my daily charts.
 
markos

markos

Well-known member
VIP
Warehouse
@mike01 Welcome to useThinkScript! TOS is an amazing platform and this site has worldwide readership.
This site also has loads of day traders so you're in the right place. (y) Look at the bottom of the page for matches.
As you are new here, this is what we suggest: Look at the bottom of the page for matches.
Go to the Tutorials section here > https://usethinkscript.com/forums/tutorials.4/
Also type VWAP in our great SEARCH engine above. There are many available between these three sources. Best wishes!
 
Y

yamiyugi

New member
Tried it but the indicator only shows when market opens? Is there an Anchored VWAP to use on futures where it shows not only during market opens?
 
BenTen

BenTen

Administrative
Staff
VIP
Warehouse
@greentonic Are you talking about the signals (arrows)? If so, those are delayed. But as far as the VWAP, I don't think so.
 

Top