Above VWAP label

JakeD

New member
The following script places a label stating the % of the 11 S&P sectors currently above the VWAP
It is only accurate when I display it on a daily chart, I wish I could make it appear as a constant no matter what TF my current chart is.
Possible?

Additionally, does anyone know how I could automatically replace this list of 11 sectors with a list of the 500 companies in the S&P, ie a program to re-write the code using the same format.
Also. any improvements on this code are appreciated.

Code:
input XLE = "XLE";
input XLU = "XLU";
input XLRE = "XLRE";
input XLB = "XLB";
input XLP = "XLP";
input XLV = "XLV";
input XLI = "XLI";
input XLF = "XLF";
input XLY = "XLY";
input XLK = "XLK";
input XLC = "XLC";


input NumberofTickers = 11;
def condition1 = close(symbol = XLE) > VWAP(symbol = XLE);
def condition2 =  close(symbol = XLU) > VWAP(symbol = XLU);
def condition3 =  close(symbol = XLRE) > VWAP(symbol = XLRE);
def condition4 =  close(symbol = XLB) > VWAP(symbol = XLB);
def condition5 =  close(symbol = XLP) > VWAP(symbol = XLP);
def condition6 =  close(symbol = XLV) > VWAP(symbol = XLV);
def condition7 =  close(symbol = XLI) > VWAP(symbol = XLI);
def condition8 =  close(symbol = XLF) > VWAP(symbol = XLF);
def condition9 =  close(symbol = XLY) > VWAP(symbol = XLY);
def condition10 =  close(symbol = XLK) > VWAP(symbol = XLK);
def condition11 =  close(symbol = XLC) > VWAP(symbol = XLC);


def conditionCount = condition1 + condition2 +condition3+condition4+condition5+condition6+condition7+ condition8+condition9+condition10+condition11;
def percentabove = (conditionCount / NumberofTickers)*100;
AddLabel(visible = Yes, text = "% of S&P Sectors above VWAP: " + percentabove + "%");
 
According to Mobius:

The code would be revised for a secondary symbol.

In every call to the data point VWAP or Volume it would need to be replaced with the secondary symbol. That's easiest to be done by making a new input for it.

Example: input symb = "SPY"; def vwap_ = VWAP(symbol = symb); def vol = Volume(symbol = symb); then call those two variables as replacements for the iData calls in the rest of the study

This is the VWAP code with a secondary symbol by Mobius. If you use the code below, it should work across all timeframes.

Code:
# Mobius
input numDevDn = -2.0;
input numDevUp = 2.0;
input timeFrame = {default DAY, WEEK, MONTH};
input symb = "SPY";

def vwap = VWAP(Symbol = symb);
def vol = Volume(symbol = symb);
def c = close(symbol = symb);
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 = vol;
    volumeVwapSum = vol * vwap;
    volumeVwap2Sum = vol * Sqr(vwap);
} else {
    volumeSum = compoundValue(1, volumeSum[1] + vol, vol);
    volumeVwapSum = compoundValue(1, volumeVwapSum[1] + vol * vwap, vol * vwap);
    volumeVwap2Sum = compoundValue(1, volumeVwap2Sum[1] + vol * Sqr(vwap), vol * Sqr(vwap_));
}
def price = volumeVwapSum / volumeSum;
def deviation = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(price), 0));

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

VWAP.setDefaultColor(getColor(0));
UpperBand.setDefaultColor(getColor(2));
LowerBand.setDefaultColor(getColor(4));
 
  • Love
Reactions: IPA

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