VWAP Crosses, Format, Watchlist, Label, Scan for ThinkorSwim

Can anyone guide me . In this watchlist script if 9ema cross then green , cross below then red but no gray. I want it to stay green or red once cross up or down. Thanks in advance
that would nullify the within 3 bars part of the code. That would pretty much imply green over vwap and red under vwap and there is a study for that already somewhere on this forum.
 
@SonsOfRagnar
EzxOLPh.png

https://usethinkscript.com/threads/ema-and-vwap-crossover-indicator-for-thinkorswim.320/
 
Hi, is there a script available or can one be created to plot yesterday's end of day vwap price on todays chart. For example if the vwap at the close was 200, the script would draw a line on todays intraday chart at the 200 level. Thanks
 
@TCB
Code:
declare hide_on_daily;

def marketclose = secondstillTime(1600) == 0;
def vw = if marketclose then reference vwap() else vw[1];
plot vwap = vw;
I have tried over 5 scripts and cannot get this study to look how I want. Do you know how I can get the study to only show 2day or 3day VWAP and not have those ugly lines connecting days together?


Code:
declare hide_on_daily;

input DaysBack = 1;

def VWAP_StartDate = GetYYYYMMDD();
def BeyondStartDate = VWAP_StartDate >= GetYYYYMMDD()[DaysBack];
AddLabel(yes, BeyondStartDate, Color.WHITE);
def marketclose = SecondsTillTime(1600) == 0;
AddLabel(yes, marketclose, Color.WHITE);
def vw = if marketclose then reference vwap()[DaysBack] else vw[1];
plot vwap = vw;

vwap.SetDefaultColor(Color.ORANGE);
vwap.SetStyle(Curve.FIRM);
vwap.SetLineWeight(3);
 
@mngolfer123
Code:
declare hide_on_daily;

input DaysBack = 2;

def days = getday() >= getlastday() - daysback;

def marketclose = SecondsTillTime(1600) == 0;

def vw = if marketclose then reference vwap() else vw[1];
plot vwap = if days then vw else double.nan;
vwap.setpaintingstrategy(paintingstrategy.horizontal);
 
@mngolfer123
Code:
declare hide_on_daily;

input DaysBack = 2;

def days = getday() >= getlastday() - daysback;

def marketclose = SecondsTillTime(1600) == 0;

def vw = if marketclose then reference vwap() else vw[1];
plot vwap = if days then vw else double.nan;
vwap.setpaintingstrategy(paintingstrategy.horizontal);
@generic Thank you so much for this! I really appreciate it. I have been searching and digging through code for probably two weeks now. I have written a decent amount of ThinkScripts but I'm not on your level.

I have tried to change the code above to show only 1 VWAP per indicator, but I can't seem to figure that out either.

I wasn't sure how the "vw[1]" part of your code works. I get everything else, but it seems like I have to add in some more lines to get it so that it shows VWAP from the DaysBack candle at marketclose and extends it forward through the entire next trading day.

Thats what you have, but I'm wondering how I could drop the line from two days back or at least make it start at the 1600 marketclose time instead of the end of day time 1800?

Code:
declare hide_on_daily;

input DaysBack = 1;

def days = (getday() >= getlastday() - daysback);# and (GetDay() - daysback) and getday() <= getlastday();

def marketclose = SecondsTillTime(1600) == 0;

def vw = if marketclose then reference vwap() else vw[1];
plot vwap = if days then vw else double.nan;

#To verify values
AddLabel(Yes, days, color.yellow);
AddLabel(Yes, getday(), color.yellow);
AddLabel(Yes, getlastday(), color.yellow);
AddLabel(Yes, getlastday() - daysback, color.yellow);
AddLabel(Yes, marketclose, color.yellow);

vwap.setpaintingstrategy(paintingstrategy.horizontal);
vwap.SetDefaultColor(Color.ORANGE);
#vwap.SetStyle(Curve.FIRM);
vwap.SetLineWeight(3);
 
@mngolfer123 Sorry, I don't really understand the question. Could you post a ss and I'll look at it tmr?
The code already checks for 1600 and plots vwap into next day.
The vw[1] just says the vwap value until it checks for market close.
 
@mngolfer123 Sorry, I don't really understand the question. Could you post a ss and I'll look at it tmr?
The code already checks for 1600 and plots vwap into next day.
The vw[1] just says the vwap value until it checks for market close.
@generic Hey Generic! So it may be that I am using the show extended hours feature on my chart, which I need for my trading style. The VWAP from 2 days ago end at the end of day time of 1800 instead of the 1600, so essentially it is not the correct VWAP value from close.


Then Also wondering how I would extend the VWAP line from 2 days ago all the way to the end of today?
 
The vw[1] just says the vwap value until it checks for market close.
Okay, so I see that it plots the vwap value, but since it is in the else statement, it doesn't seem to use the "reference VWAP()" function at all?

def vw = if marketclose then reference vwap() else vw[1]; Not sure how it knows to use VWAP since the "reference VWAP()" is inside of the first IF/THEN statement.
plot vwap = if days then vw else double.nan;
 
@mngolfer123 Okay, so the vwap value is correct but it gets cut off because of how the plot condition works. The plot condition will plot on the next day because it doesn't include AH from yesterday but the data for the plot is the vwap of yesterdays close. You should be able to verify this by adding vwap study and comparing the numbers.
If you look at the vw line of code then you'll see that vw is using reference vwap so vw[1], which saves the previous value of vw, will also use reference vwap.
Hope this makes sense.
 
I am trying to create a column on watchlist that shows if the close is in between VWAP upper band and vwap. But I found. the formula for calculating UpperBand = price + numDevUp * Deviation. The deviation is always = 0. Can anyone help me here thank you!

Code:
input numDevDn = -2.0;
input numDevUp = 2.0;
input timeFrame = {default DAY, WEEK, MONTH};

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

def yyyyMmDd = getYyyyMmDd();
def periodIndx;
switch (timeFrame) {
case DAY:
    periodIndx = yyyyMmDd;
case WEEK:
    periodIndx = Floor((daysFromDate(first(yyyyMmDd)) + getDayOfWeek(first(yyyyMmDd))) / 7);
case MONTH:
    periodIndx = roundDown(yyyyMmDd / 100, 0);
}
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 deviation = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(price), 0));

def VWAP = price;
def UpperBand = price + numDevUp * deviation;
def LowerBand = price + numDevDn * deviation;

def current = if ( close >= vwap and close < UpperBand)
              then 0
              else if close > UpperBand
              then 1
              else if close < vwap
              then -1
              else double.nan;

AddLabel(1, if current == 0
            then current
            else if current == 1
            then UpperBand
            else if current == -1
            then current
            else UpperBand,
            if current == 0
            then color.yellow
            else if current == 1
            then color.green
            else if current == -1
            then color.red
            else color.current);
AssignBackgroundColor( if current == 0

            then createColor(180,180,5)
            else if current == 1
            then createColor(51,102,0)
            else if current == -1
            then createColor(155,0,0)
            else color.current);
 
@dsme1234 You're overcomplicating the conditional logic code... You should be using 1, -1, and Double.NaN...

Edited to add: Have some free time so taking a deeper look into your code...

After further review: I have simplified the code to only render colored Custom Watchlist Column cells... Your code didn't accurately calculate a value to display so I left that value blank... I'm sure the code could be further simplified but that's a good start... Notice how I have made three conditions rather than a bunch of conditional gobbledygook... This shrinks the code a lot and makes it easier to comprehend...

Let me know if this works...

Ruby:
input numDevDn = -2.0;
input numDevUp = 2.0;
input timeFrame = {default DAY, WEEK, MONTH};

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

def yyyyMmDd = getYyyyMmDd();
def periodIndx;
switch (timeFrame) {
case DAY:
    periodIndx = yyyyMmDd;
case WEEK:
    periodIndx = Floor((daysFromDate(first(yyyyMmDd)) + getDayOfWeek(first(yyyyMmDd))) / 7);
case MONTH:
    periodIndx = roundDown(yyyyMmDd / 100, 0);
}
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 deviation = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(price), 0));

def VWAP = price;
def UpperBand = price + numDevUp * deviation;
def LowerBand = price + numDevDn * deviation;

def between = Between(close, vwap, UpperBand);
def above = close > UpperBand;
def below = close < vwap;

AddLabel(yes, " ",
  if between then createColor(180,180,5)
  else if above then createColor(51,102,0)
  else if below then createColor(155,0,0)
  else color.current
);

AssignBackgroundColor(
  if between then createColor(180,180,5)
  else if above then createColor(51,102,0)
  else if below then createColor(155,0,0)
  else color.current
);
 
Last edited:
@dsme1234 You're overcomplicating the conditional logic code... You should be using 1, -1, and Double.NaN...

Edited to add: Have some free time so taking a deeper look into your code...

After further review: I have simplified the code to only render colored Custom Watchlist Column cells... Your code didn't accurately calculate a value to display so I left that value blank... I'm sure the code could be further simplified but that's a good start... Notice how I have made three conditions rather than a bunch of conditional gobbledygook... This shrinks the code a lot and makes it easier to comprehend...

Let me know if this works...

Ruby:
input numDevDn = -2.0;
input numDevUp = 2.0;
input timeFrame = {default DAY, WEEK, MONTH};

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

def yyyyMmDd = getYyyyMmDd();
def periodIndx;
switch (timeFrame) {
case DAY:
    periodIndx = yyyyMmDd;
case WEEK:
    periodIndx = Floor((daysFromDate(first(yyyyMmDd)) + getDayOfWeek(first(yyyyMmDd))) / 7);
case MONTH:
    periodIndx = roundDown(yyyyMmDd / 100, 0);
}
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 deviation = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(price), 0));

def VWAP = price;
def UpperBand = price + numDevUp * deviation;
def LowerBand = price + numDevDn * deviation;

def between = Between(close, vwap, UpperBand);
def above = close > UpperBand;
def below = close < vwap;

AddLabel(yes, " ",
  if between then createColor(180,180,5)
  else if above then createColor(51,102,0)
  else if below then createColor(155,0,0)
  else color.current
);

AssignBackgroundColor(
  if between then createColor(180,180,5)
  else if above then createColor(51,102,0)
  else if below then createColor(155,0,0)
  else color.current
);
Thank you! It looks way better than what I wrote. I also find that the time frame needs to set to 5min then it will calculates the proper Upperband value.
 

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