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
@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
@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
@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?
 
G

greentonic

New member
ive had repainting and delay issues. pretty much not usable for intra day trading.
 
BenTen

BenTen

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

yman

New member
Can any1 help with a scanner for stocks touching previous days vwap
Last=previousVWAP
thanks
 
T

tomsk

Well-known member
VIP
Can any1 help with a scanner for stocks touching previous days vwap
Last=previousVWAP
thanks

Here is a scan for stocks that crosses ABOVE the previous day's VWAP. Place this code in the scanner and run this on a daily aggregation. I just ran this on the S&P 100 (Daily) and obtained 46 results

Code:
# Scan for crosses above previous day's VWAP
# tomsk
# 1.21.2020

# Run on a daily aggregation

def cap = getAggregationPeriod();
def yyyyMmDd = getYyyyMmDd();
def periodIndx = yyyyMmDd;
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 VWAP = price;

plot scan = close crosses above VWAP[1];
# End Scan for crosses above previous day's VWAP
 
Y

yman

New member
@tomsk I have tried to scan it on daily doesnt show any results is there any other parameters to add
 
M

mike151

New member
I have just taken the original VWAP script from the OP and viewed it on my 5 minute chart. I there anyone with experience in this time frame with that script that can tell me if the Up and Down arrows print and then remove if the price action and volume changes? I have another script that does that so It's not very accurate on the 5 minute.
Does this one work out of the box on the 5 minute or is there something someone can suggest to find something or tweak this one to best suit my needs.
If this script paints the arrows where they currently appear on my charts for the 5 min, then this is awesome! I try to keep it simple and this just might be it. Going to test it tomorrow.
 
TraderJoe

TraderJoe

New member
Thank you BenTen for the Anchored VWAP, loving the setup and signals generated. I'm not a coder, is it difficult to have an alert with sound and text? That would be just awesome! Thanks again...
 
B

bobomatic

New member
One of my favorite indicators but unfortunately, over the past couple of weeks of testing, I could not get it to work without consistently delayed signals. I played with it on a 5 and 10 minute chart on the ES and CL, played with the variables, but with no luck. It was good to confirm my other signals in hindsight,, but as a trading signal, too delayed to be useful.
 
TraderJoe

TraderJoe

New member
Hello, I refresh the TOS chart, and draw the AVWAP. Also on my phone, as I open each futures symbol it shows properly. If you don't refresh, then it's delayed...
 
Thread starter Similar threads Forum Replies Date
L Anchored VWAP Questions 6
J Very Intriguing VWAP Indicator Questions 0
T Custom Scan - VWAP + Momentum Oscillator Questions 3
BenTen Triple VWAP Indicator for ThinkorSwim Custom 0
zeek Create VWAP deviation band price label Questions 2
Top