Percentage of Candle Above/Below VWAP

C4men

Member
I'm looking to create a script that does the following:

  • Allows me to enter a percent in the Input
  • Allows me to select 'Above' or 'Below'
  • Then it calculates using these two percentages in relation to the candle size vs VWAP line

So for example... the size of the candle is $10 (High $100 - Low $90 = Size $10)
And I select 60% (Input #1) and 'Above' (Input #2)
Then if $6 of that $10 candle is above VWAP, I get an arrow.
In other words, 60% of this current candle is above the VWAP line on my chart.

I know it's a weird ask, but I have noticed a pattern and want to see if this is possible.
 
Solution
I'm looking to create a script that does the following:

  • Allows me to enter a percent in the Input
  • Allows me to select 'Above' or 'Below'
  • Then it calculates using these two percentages in relation to the candle size vs VWAP line

So for example... the size of the candle is $10 (High $100 - Low $90 = Size $10)
And I select 60% (Input #1) and 'Above' (Input #2)
Then if $6 of that $10 candle is above VWAP, I get an arrow.
In other words, 60% of this current candle is above the VWAP line on my chart.

I know it's a weird ask, but I have noticed a pattern and want to see if this is possible.

this is a simple version of what you asked for. it doesn't ask for above/below

Code:
#-----------------------------...
I'm looking to create a script that does the following:

  • Allows me to enter a percent in the Input
  • Allows me to select 'Above' or 'Below'
  • Then it calculates using these two percentages in relation to the candle size vs VWAP line

So for example... the size of the candle is $10 (High $100 - Low $90 = Size $10)
And I select 60% (Input #1) and 'Above' (Input #2)
Then if $6 of that $10 candle is above VWAP, I get an arrow.
In other words, 60% of this current candle is above the VWAP line on my chart.

I know it's a weird ask, but I have noticed a pattern and want to see if this is possible.

this is a simple version of what you asked for. it doesn't ask for above/below

Code:
#-----------------------------

def na = double.nan;
def bn = barnumber();

input percent_of_candle_above_vwap = 60;

def isup = close > open;
def isdwn = close < open;


# 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;
#plot UpperBand = price + numDevUp * deviation;
#plot LowerBand = price + numDevDn * deviation;

VWAP.setDefaultColor(getColor(0));
#UpperBand.setDefaultColor(getColor(2));
#LowerBand.setDefaultColor(getColor(4));

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


def crossvwap = (high >= vwap and low <= vwap);
def rng = high - low;
def perabove = round(100*(high-vwap)/rng,1);


def isabove = (perabove >= percent_of_candle_above_vwap);

plot zup = if crossvwap and isabove then low*0.999 else na;
zup.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
zup.SetDefaultColor(Color.green);
zup.setlineweight(3);
zup.hidebubble();


input test_bubble = no;
addchartbubble(test_bubble, low,
perabove + "\n" +
percent_of_candle_above_vwap 
, (if crossvwap and isabove then color.green
else if crossvwap then color.yellow
else color.gray), no);

#
 
Solution

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