Help Modify VWAP Label

EarlyAMTrader

New member
Hello again,

I'm using a VWAP label for easy visual on my chart and wanted to see if someone can help me modify the code.

The current code turns green if Price > VWAP and turns red if Price < VWAP.

I would like to see if this code can be modified to show the following;

If Price > VWAP and moving up -> Green
If Price > VWAP and moving down -> Yellow
If Price < VWAP and moving down -> Red
If Price < VWAP and moving up -> Cyan

Code:
#VWAP_Label
input numDevDn = -2.0;
input numDevUp = 2.0;
input timeFrame = {default DAY, WEEK, MONTH};
input time = yes;
input time_color = {default "magenta", "green", "pink", "cyan", "orange", "red", "blue", "gray", "violet"};
input VWAP_label = yes;
input VWAP_Color = {default "magenta", "green", "pink", "cyan", "orange", "red", "blue", "gray", "violet"};
input Upperbands_label = Yes;
input UpperBand_Color = {default "magenta", "green", "pink", "cyan", "orange", "red", "blue", "gray", "violet"};
input Lowerbands_label = Yes;
input LowerBand_color = {default "magenta", "green", "pink", "cyan", "orange", "red", "blue", "gray", "violet"};

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;
UpperBand.hide();
plot LowerBand = price + numDevDn * deviation;
LowerBand.hide();

AddLabel(VWAP_label, " VWAP: " + Round(VWAP, 2), if vwap<close then color.green else color.red);

Thanks in Advance!
 
Last edited:

EarlyAMTrader

New member
OK so tried it again and this time I didn't get an error with the following code;

Code:
#AddLabel(VWAP_label, " VWAP: " + Round(VWAP, 2), if vwap<close then color.green else color.red);

AddLabel(yes, "VWAP: " +round (VWAP,2),
#Green is over VWAP and going up
  if VWAP > close  and VWAP>VWAP[1] then Color.Green else
#Yellow if over VWAP and going down
  if VWAP > close  and VWAP<VWAP[1] then color.yellow else
#Red if under VWAP and going down
  if VWAP < close  and VWAP<VWAP[1] then Color.red else
#Cyan if under VWAP and going up
   if VWAP < close  and VWAP>VWAP[1] then Color.cyan else  Color.Gray);

But checking a few tickers, not sure it's working. Looking at AMC, current price is 43.74, VWAP is 51.12 and the label is "Yellow". I would think it should be either "Red" or "Cyan" since the price is under VWAP.

Maybe I need to check it during trading when the numbers are moving?

Thanks in advance if you can provide some guidance.
 
Last edited:

MerryDay

Well-known member
VIP
@EarlyAMTrader Did you see the message that TOS is undergoing maintenance this morning? I have had things go very squirrelly during maintenance periods. Have even managed to corrupt my app and have had to re-install. Your code, looks great! Congrats!
Wait until maintenance is done before testing any further.

PS: Have you seen the </> symbol along the top when you are creating a post? Could you please click on that and paste whatever code you are including in your post into the box that pop ups? It makes it much easier to read through posts that way.
 
Last edited:

EarlyAMTrader

New member
@EarlyAMTrader Did you see the message that TOS is undergoing maintenance this morning? I have had things go very squirrelly during maintenance periods. Have even managed to corrupt my app and have had to re-install. Your code, looks great! Congrats!
Wait until maintenance is done before testing any further.

PS: Have you seen the </> symbol along the top when you are creating a post? Could you please click on that and paste whatever code you are including in your post into the box that pop ups? It makes it much easier to read through posts that way.
Awesome! Thank you @MerryDay. Newbie to this site and appreciate the guidance. I made the suggested changes. Have a great weekend!
 

EarlyAMTrader

New member
I've been trying to code with some success.

Change the last part to this and the colors change... but I'd prefer if the background color chanced and not the font color;

Code:
AddLabel(yes, round (VWAP,2),
#Green is over VWAP and going up
  if VWAP < close  and VWAP>VWAP[1] then Color.Green else
#Yellow if over VWAP and going down
  if VWAP < close  and VWAP<VWAP[1] then color.yellow else
#Red if under VWAP and going down
  if VWAP > close  and VWAP<VWAP[1] then Color.red else
#Cyan if under VWAP and going up
   if VWAP > close  and VWAP>VWAP[1] then Color.cyan else  Color.Gray);
 
Last edited:

Ananas

New member
Loose the
Code:
input time_color = {default "magenta", "green", "pink", "cyan", "orange", "red", "blue", "gray", "violet"};
input VWAP_Color = {default "magenta", "green", "pink", "cyan", "orange", "red", "blue", "gray", "violet"};
input UpperBand_Color = {default "magenta", "green", "pink", "cyan", "orange", "red", "blue", "gray", "violet"};
input LowerBand_color = {default "magenta", "green", "pink", "cyan", "orange", "red", "blue", "gray", "violet"};

It's making the script slower and the code less efficient
 

Ananas

New member
So no watchlist option, huh? ...dang.....sux ;(
delete the AddLabel lines and replace with
Code:
assignBackgroundColor(
  if VWAP > close  and VWAP>VWAP[1] then Color.green else
  if VWAP > close  and VWAP<VWAP[1] then color.yellow else
  if VWAP < close  and VWAP<VWAP[1] then Color.red else
  if VWAP < close  and VWAP>VWAP[1] then Color.cyan else Color.black);
 

Similar threads

Top