Label that shows % of 9ema from the VWAP.

TJ74

New member
HI Guys,

I need a label that shows the distance of 9ema from the VWAP in percentage.
Label name: % VWAP
Less than 30%, Label White, Greater than 30% then Label Green.
label shows only two digit decimals to save space.
I want to use it on all time frames.


Thank you for your assistance.
 
HI Guys,

I need a label that shows the distance of 9ema from the VWAP in percentage.
Label name: % VWAP
Less than 30%, Label White, Greater than 30% then Label Green.
label shows only two digit decimals to save space.
I want to use it on all time frames.


Thank you for your assistance.

here is something that compares 2 signals.
i don't think they will ever be 30% apart, maybe 1%....
i set the default % at 1%

Code:
# ema_vwap_dist

#https://usethinkscript.com/threads/label-that-shows-of-9ema-from-the-vwap.15263/
#Label that shows % of 9ema from the VWAP.

def na = Double.NaN;
def bn = BarNumber();

#def lastbar = !isnan(close[0]) and isnan(close[-1]);

#--------------------

def price2 = close;

input ma1_len = 9;
input ma1_type =  AverageType.EXPONENTIAL;
def ma1 = MovingAverage(ma1_type, price2, ma1_len);

input show_lines = yes;

plot z1 = if show_lines then ma1 else na;
z1.SetDefaultColor(color.cyan);
#z1.setlineweight(1);
z1.HideBubble();

addlabel(show_lines, "AVG", z1.TakeValueColor());

#--------------------

# VWAP
# TD Ameritrade IP Company, Inc. (c) 2011-2023

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

plot VWAP = price;
input show_vwap_bands = no;
plot UpperBand = if show_vwap_bands then (price + numDevUp * deviation) else na;
plot LowerBand = if show_vwap_bands then (price + numDevDn * deviation) else na;

VWAP.SetDefaultColor(color.yellow);
UpperBand.SetDefaultColor(GetColor(2));
LowerBand.SetDefaultColor(GetColor(4));


addlabel(1, "VWAP", vwap.TakeValueColor());

#--------------------

def diff = ma1 - vwap;
def diff_per = round(100*diff/vwap,2);

input percent_trip = 1.0;
addlabel(1, diff_per + " %", (if diff_per < percent_trip then color.white else color.green));
addlabel(1, "set % " +  percent_trip , color.yellow);

#--------------------
#
 

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