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

Hi

@BenTen @tomsk , or anyone else available to help, I was wondering if I could get some help to finish up this TOS code for watch-list that I was trying to complete, to show green when 9 ema crosses above vwap within last 3 bars, customizable for timeframes, and opposite as well, if 9 ema crosses below vwap within last 3 bars; Here is what I have gotten started:

Code:
input maLengthOne = 9;
input maType=AverageType, EXPONENTIAL
input timeFrame = {default DAY, WEEK, MONTH};
def vwapValue = reference VWAP(-2, 2, timeFrame)."VWAP";
def ma = MovingAverage(maType, vwapValue, maLength);
inputbarsAfterCross = 3;
def crossAbove = maLengthOne > vwap
def crossBelow = maLengthOne < vwap
diff.AssignValueColor(if ma == vwap then Color.CURRENT else Color.BLACK);
AssignBackgroundColor(if diff > 0 then Color. GREEN else if diff < 0 then Color.RED else Color.CURRENT);

Thank you

Papa

Just a follow up, as I previously did not state the issues I was having:
I am getting the messages invalid statements at the certain lines 2, 7 and 10.
If anyone could advise would be great, I am not a coder, but attempted to piece together what I could;
Thanks in advance @BenTen @tomsk @horserider @netarchitech or anyone else who can help me with this!

Code:
input maLengthOne = 9;
input maType=AverageType, EXPONENTIAL
input timeFrame = {default DAY, WEEK, MONTH};
def vwapValue = reference VWAP(-2, 2, timeFrame)."VWAP";
def ma = MovingAverage(maType, vwapValue, maLength);
inputbarsAfterCross = 3;
def crossAbove = maLengthOne > vwap
def crossBelow = maLengthOne < vwap
diff.AssignValueColor(if ma == vwap then Color.CURRENT else Color.BLACK);
AssignBackgroundColor(if diff > 0 then Color. GREEN else if diff < 0 then Color.RED else Color.CURRENT)

Invalid statement: input at 2:1
Invalid statement: def at 7:1
Invalid statement: AssignBackgroundC... at 10:1
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

@PapaBear10 Your code had several syntax errors. Here is my version of a VWAP Watchlist that is painted green when 9 ema crosses above vwap within last 3 bars. It is painted red when 9 ema crosses below vwap within last 3 bars. Remember to select the aggregation period you are interested in when configuring this watchlist

Code:
# VWAP Watchlist
# tomsk
# 1.25.2020

# Watchlist that is painted green when 9 ema crosses above vwap within last 3 bars
# It is painted red when 9 ema crosses below vwap within last 3 bars

input length = 9;

def ema = ExpAverage(close, length);
def vwapValue = reference VWAP();
def crossUp = ema crosses above vwapValue within 3 bars;
def crossDn = ema crosses below vwapValue within 3 bars;
AddLabel(1, if crossUp then "X Up" else if crossDn then "X Down" else " ", Color.Black);
AssignBackgroundColor(if ema crosses above vwapValue then Color.GREEN
                      else if ema crosses below vwapValue then Color.RED
                      else Color.Gray);
# End VWAP Watchlist
 
Last edited:
Your code had several syntax errors. Here is my version of a VWAP Watchlist that is painted green when 9 ema crosses above vwap within last 3 bars.
Tom, what would I change here to track price crossing? I've got the following; how to I blend them together? I really like the 3-bars approach, so that it will disappear after the cross happens. I've tried swapping out the input length, etc.

plot vwap = vwap();
AssignBackgroundColor(if close > vwap then Color.DARK_GREEN else if close < vwap then Color.DARK_RED else Color.Dark_ORANGE);
 
Hey @horserider thank you for writing this code, i am struggling with the code where i would like to paint an up( Buy Signal )/down( Sell Signal) arrow when the action takes place. Would you be able to guide on the same. Thank You

I used the below code to setup alerts

Code:
def Bull = avgexp > vwap;
def Bear = avgexp < vwap;

Alert(Bull, "Buy", Alert.Bar, Sound.Chimes);
Alert(Bear, "Sell", Alert.bar, Sound.Chimes);
 
@mohitdas

Redid original to include new label and arrows and alerts.

Code:
# EMA and VWAP cross. Modified 2 ToS studies and added  labels for crosses. By Horserider 7/21/2019
# Redid label and added cross arrows.


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 VWAP = price;

#
#
input price2 = close;
input length = 5;
input displace = 0;

def AvgExp = ExpAverage(price2[-displace], length);

AddLabel(yes, "VWAP EXP Cross", if avgexp > vwap then Color.GREEN else color.RED);

input showBreakoutSignals = no;
plot UpSignal = avgexp crosses above vwap;
plot DownSignal = avgexp crosses below vwap;

UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);


UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

Alert(UpSignal, "Buy", Alert.Bar, Sound.Chimes);
Alert(DownSignal, "Sell", Alert.bar, Sound.Chimes);
 
Hello - Has anyone seen a scanner that shows the list of stocks that satisfy the condition where the 9 SMA has crossed VWAP x mins ago (x can be configured or default to 5 mins)??
 
You can use the default Scan tab in ThinkorSwim for this. During the scan editor, just select close for one side, crosses - crosses above - crosses below in the middle option, and select VWAP on the right side.

Almost there, on the left column, select the dropdown > Price > close. Select the timeframe that you want. And yes, you would need to create 2 scans for this.
 
I am new to the forum so wanted to say Hello to everyone. I have a question and hope to get some help. I wonder if there is a script for knowing when the 9 EMA, 21 EMA and 55 EMA are within a certain range (maybe 5 or 10 cents for example) of the VWAP. This is similar to the Golden cross over but including VWAP. Attaching 3 samples of the way this signal helps with understanding the trend. Any help will be appreciated.

DOtEiag.jpg


These do come close to each other frequently and what I have found is when the 3 ema's and vwap cross over either ascending or descending is a very good signal for taking a position (9 ema crossing over 21 ema and 21 ema crossing over 55 ema....when vwap crosses these three as close possible is a very good indicator). So an arrow showing when this happens or some other type of alert, or maybe if we can set this as a scanner.
 
@smvatkar Here is a template indicator I have involving an EMA and VWAP in one https://tos.mx/OWEY8Di . I would probably say that the addition you would try to tack on to this code would need to be in however an alert (or maybe a scan/market watch alert to make it easier to see across multiple assets) where the math looks like: EMA > (VWAP - .2).... to put EMA within 20cents range of VWAP for example. I am not a professional coder, nor have I created any alerts or scanners for my indicators so I cannot assist more than this at the moment. Maybe someone wants to take the idea and run with it
 
@smvatkar Here is your template basically. Now just have to add scanning or alerts/labels to identify specifically what you are looking for
Code:
# VWAP w/ MovingAvgCrossover

#Created by TradebyDay
#

input price1 = close;

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

plot VWAP = price;

input length = 50;
input length2 = 50;
input length3 = 50;
input displace = 0;

assert(length > 0, "'length' must be positive: " + length);

def EMA = compoundValue(1, EMA[1] + 2 / (length + 1) * (price1[-displace] - EMA[1]), price1[-displace]);
def EMA2 = compoundValue(1, EMA2[1] + 2 / (length2 + 1) * (price1[-displace] - EMA2[1]), price1[-displace]);
def EMA3 = compoundValue(1, EMA3[1] + 2 / (length3 + 1) * (price1[-displace] - EMA3[1]), price1[-displace]);


plot LegacyEMA = EMA;
plot LegacyEMA2 = EMA2;
plot LegacyEMA3 = EMA3;

VWAP.setDefaultColor(getColor(0));
LegacyEMA.SetDefaultColor(GetColor(1));
LegacyEMA2.SetDefaultColor(GetColor(2));
LegacyEMA3.SetDefaultColor(GetColor(3));
# End Code
 
@tradebyday Thanks a lot. I added your code to TOS and have the 3 emas and vwap as shown in the image below.
Now the real challenge is to add the code that can define the proximity of the 3 ema's and vwap within a given range in $ and use this defined range to trigger an alert (or auto refresh scanner).

EBQjE5K.jpg
 
Hello I would like some help creating a scan when the weekly VWAP crosses above or below the monthly VWAP either a column or scan.
 
@Piper2808t You can already do that without a script. Just head over to the Stock Hacker > Add new Study filter > and setup a condition like this.

4YOn69J.png
 

Attachments

  • 4YOn69J.png
    4YOn69J.png
    93.8 KB · Views: 172

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
403 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