# Help with SMA and VWAP distance labels

#### Jimmy

##### New member
Looking to put together a watch column/chart label that shows the following:
1) distance from the 9 sma both in percentage and actual price difference
2) distance from VWAP both in percentage and actual price difference.
Then label with percentage and actual price difference with the following conditions.
If price is above both the 9sma and VWAP label is green.
If price is below the 9 but above VWAP label is yellow.
If price is below VWAP but above the 9 label is orange
If price is below both the 9sma and VWAP label is red.
Would like to use on multiple time frames.
Example... daily, hourly, 10 min.

I can make individual labels showing percentage away from the 9 and VWAP but don't know the input for actual price or how to combine all the info into one label.
Thanks in advance for any insight,
Jimmy

#### tomsk

##### Well-known member
VIP
@Jimmy Here is your COMBINED label to display as a chart label. Note that because you are displaying multiple pieces of info, you won't be able to contain all this in a single watchlist UNLESS you simplify the display. This should give you some ideas how to best structure what you'd eventually like to implement

Code:
``````# Distance from SMA and VWAP
# tomsk
# 12.12.2019

input length = 9;

def sma = Average(close, length);
def DistanceSMA = (close - sma);
def vwapValue = VWAP();
def DistanceVWAP = (close - vwapValue);

AddLabel(1, "Distance SMA(" + length + ") = " + AsText(DistanceSMA) + " (" + AsPercent(close/sma-1) + ") " + "VWAP = " + AsText(DistanceVWAP) + " (" + AsPercent(close/vwapValue-1) + ")",
if close > sma and close > vwapValue then Color.GREEN else if close < sma and close < vwapValue then Color.RED else if close < sma and close > vwapValue then Color.YELLOW else if close

> sma and close < vwapValue then Color.ORANGE else Color.WHITE);
# End Distance from SMA and VWAP``````

#### Jimmy

##### New member
@tomsk Thank you,

I've been working on this for a while, you're right there's a lot of info for a column... probably just go with leaving the price distance off the column and just go with the conditions...if price above both sma and VWAP color green and so on.

Again thank you for your time and insight. Much appreciated

#### tomsk

##### Well-known member
VIP
Simplification is the key. The watchlists already have very limited real estate to display info, what you're suggesting can be challenging.
If you really must have the info on your watchlist, then you may consider separate watchlist columns
A third alternative is just to color code your watchlist and dispense with the other info like distance, percentage, etc
Keep it simple

#### Jimmy

##### New member
Yes, you are right.
Your only allowed so much data per list..(1500 I believe) unless you pay for extra data. So it would only be able to handle a couple of stocks.
I would like to add distance from 9 to VWAP to the label as well.. working on it now.
I can't thank you enough for this script.
I scalp trade during the day from the average price line(which is basically the 9) to VWAP and this gives me a really good visual of the actual price move. The percentage distance I will use as a scan once I can determine through logging a consistent range before price re-traces.
Thank you again

#### Jimmy

##### New member
Yes, you are right.
Your only allowed so much data per list..(1500 I believe) unless you pay for extra data. So it would only be able to handle a couple of stocks.
I would like to add distance from 9 to VWAP to the label as well.. working on it now.
I can't thank you enough for this script.
I scalp trade during the day from the average price line(which is basically the 9) to VWAP and this gives me a really good visual of the actual price move. The percentage distance I will use as a scan once I can determine through logging a consistent range before price re-traces.
Thank you again
Simplification is the key. The watchlists already have very limited real estate to display info, what you're suggesting can be challenging.
If you really must have the info on your watchlist, then you may consider separate watchlist columns
A third alternative is just to color code your watchlist and dispense with the other info like distance, percentage, etc
Keep it simple
For some reason the values are off on my chart... VWAP on the label doesn't match the VWAP line on the chart... Any ideas on what I need to do to fix this

#### tomsk

##### Well-known member
VIP
For some reason the values are off on my chart... VWAP on the label doesn't match the VWAP line on the chart... Any ideas on what I need to do to fix this

Check your aggregations. Only way to truly debug this is to throw a couple of AddLabel statements in your code to confirm the numbers
Other than that I can't think of anything else

Last edited:

#### Jimmy

##### New member
For some reason the values are off on my chart... VWAP on the label doesn't match the VWAP line on the chart... Any ideas on what I need to do to fix this
My labels seem to be off by a couple of cents.
Check your aggregations. Other than that I can't think of anything else
I'll give it a run through in the morning.. my VWAP was off by a penny on the futures which it's been off by a couple of cents on stocks. Has to be something with the aggregation because all of my labels are off by a few cents

#### Jimmy

##### New member
My labels seem to be off by a couple of cents.

I'll give it a run through in the morning.. my VWAP was off by a penny on the futures which it's been off by a couple of cents on stocks. Has to be something with the aggregation because all of my labels are off by a few cents
Thanks for everything again, much appreciated.

##### Member
@tomsk Is there anyway we can just have the difference between price and EMA distance as a chart label only..no vwap or percentage? Thinkscript Code or Workspace Please? Thank you so much!!!!!..

#### tomsk

##### Well-known member
VIP
@tomsk Is there anyway we can just have the difference between price and EMA distance as a chart label only..no vwap or percentage? Thinkscript Code or Workspace Please? Thank you so much!!!!!..

@tradeking313 Here's the code you wanted, real clean with EMA 50 defined. As you can see it's all math.
Enjoy

Code:
``````# Moving Average Distance
# tomsk
# 12.13.2019

input price = close;
input length = 50;

plot avg = ExpAverage(price, length);
AddLabel(1, "Distance from " + length + " EMA = " + AsDollars(price - avg), Color.YELLOW);
# End Moving Average Distance``````

##### Member
@tomsk thank you soooooooooooooooo much!

#### Jimmy

##### New member
@tomsk thank you soooooooooooooooo much!
@tomsk
I've been trying to to add distance from the 9 to VWAP to the label and I can't seem to get it right. I'm stumped, if you have time could you write it so I can see where I'm wrong.
Thanks

#### tomsk

##### Well-known member
VIP
@tomsk
I've been trying to to add distance from the 9 to VWAP to the label and I can't seem to get it right. I'm stumped, if you have time could you write it so I can see where I'm wrong.
Thanks

Distance from the SMA(9) to the VWAP can easily be computed, it's all math.

Code:
``````input length = 9;

def sma = Average(close, length);
def vwapValue = VWAP();
def distance = sma - vwapValue;
AddLabel(1, "Distance SMA(" + length + ") to VWAP = " + AbsValue(distance), Color.YELLOW);``````

You can get fancy with this and add bells and whsitles as may be necessary.

#### Jimmy

##### New member
@tomsk
AddLabel(1, "Distance SMA(" + length + ") = " + AsText(DistanceSMA) + " (" + AsPercent(close/sma-1) + ") " + "VWAP = " + AsText(DistanceVWAP) + " (" + AsPercent(close/vwapValue-1) + ")",

I was trying to add it to the end of this.
I can write scripts to get the distance between price and an moving average but seem to be having trouble between moving average to moving average.

#### Jimmy

##### New member
@tomsk
AddLabel(1, "Distance SMA(" + length + ") = " + AsText(DistanceSMA) + " (" + AsPercent(close/sma-1) + ") " + "VWAP = " + AsText(DistanceVWAP) + " (" + AsPercent(close/vwapValue-1) + ")",

I was trying to add it to the end of this.
I can write scripts to get the distance between price and an moving average but seem to be having trouble between moving average to moving average.
@tomsk
I was writing and posting as your response came through.
Thank you

#### tomsk

##### Well-known member
VIP
Let's get real specific shall we? Not sure I understand what you're really looking for

From the above convo, I gather you'll like to modify post #2 and add in that single label statement the distance from SMA(9) to VWAP?
Or did you mean something else? Please be as explicit as you can. When in doubt provide as much detail as possible so there won't be any misunderstandings.

This forum, unlike a typical chatroom may not be the best medium for a two way interactive exchange, so best to be real explicit so we can help you

##### Member
I was up all night trying to create a code which will display the distance amount between my entry price and the 9 ema line on both 5 min chart and 1 minute chart and could not find a way to do that. Any help will be much appreciated..thanks everyone!!!

#### Jimmy

##### New member
I was up all night trying to create a code which will display the distance amount between my entry price and the 9 ema line on both 5 min chart and 1 minute chart and could not find a way to do that. Any help will be much appreciated..thanks everyone!!!
Is your entry price pre determined or is it real time?

#### Jimmy

##### New member
Let's get real specific shall we? Not sure I understand what you're really looking for

From the above convo, I gather you'll like to modify post #2 and add in that single label statement the distance from SMA(9) to VWAP?
Or did you mean something else? Please be as explicit as you can. When in doubt provide as much detail as possible so there won't be any misunderstandings.

This forum, unlike a typical chatroom may not be the best medium for a two way interactive exchange, so best to be real explicit so we can help you
@tomsk
Good afternoon,
Yes, you are correct I am going to add this to post #2.
I was wanting one label that shows:
1) distance from price to 9
2) distance from price to VWAP
3) distance from 9 to VWAP
I will use this label for a couple of different things.
1) most important is, I scalp trade from a break of the 9 to VWAP on lower times frames using options. So having a visual of actual price helps me save calculation time.
2) having the percentages added and distances of each one individual, hopefully later( after some extensive logging) I'll be able to create a scan alert that will allow me some time to catch a setup early so that I don't miss a move.
Thank you so much for all your help
Jimmy

Last edited: