How to make a GRADIENT from a percentage value/indicator/study

korygill

Active member
VIP
I created a watchlist column that calculates a percentage. For your own testing, maybe try Bollinger Band % or something similar to test the idea. The main thing I'd like to show in this article, is math involved and how you use CreateColor() to create a gradient background where 0 is more Green and 100 is more Red.

This picture is long, so be sure to scroll to see the code below!

Ed11PSt.png


Assume in the code below that X is your percentage value in your column/study/indicator. We ensure the percentage is between 0-100. Colors are 0-255, so we apply the % to Red and 1-% to Green, and leave Blue as 0.

Code:
def x = <your indicator % value here>

# ensure x is between pMin and pMax and store in c1
def pMin = 0;
def pMax = 100;
def range = pMax - pMin;
def c1 = Min(pMax,Max(pMin,x));
# percent of range
def por = (c1-pMin)/range;
def red = Round(255*por,0);
def green = Round(255-255*por,0);

AssignBackgroundColor(CreateColor(red, green, 0));

plot pct = x;

pct.AssignValueColor(if por>0.5 then Color.White else Color.Black);

EDIT and additional example. What if your %Change is -100% to 100%? No worries. Just tweak a few values and your are back in business!

Code:
def x = <your indicator % value here>

# ensure x is between pMin and pMax and store in c1
def pMin = -100;
def pMax = 100;
def range = pMax - pMin;
def c1 = Min(pMax,Max(pMin,x));
# percent of range
def por = (c1-pMin)/range;
def red = Round(255*por,0);
def green = Round(255-255*por,0);

AssignBackgroundColor(CreateColor(red, green, 0));

plot pct = x;

pct.AssignValueColor(if por>0.75 then Color.White else Color.Black);
 
Last edited:

Alex

Active member
VIP
@korygill That's pretty neat I must admit. Would you be able to help me incorporate that into a watchlist column script I have that shows the percentage a stock is up / down on the day.

Code:
plot pop = Round("number" = ((ask + bid) / 2 -close[1]) / close[1] * 100, "numberOfDigits" = 1);
pop.assignvalueColor(if pop >0 then color.green else color.red);

assignBackgroundColor(if pop >0 then Color.dark_green else Color.dark_red);
 

korygill

Active member
VIP
@Alex great question. See my EDIT in original post where I made this more generic so you can define the range. Values outside the range take on the Color of the range extreme (max red or max green). Math is FUN! Enjoy.
 

Alex

Active member
VIP
@korygill Thanks for the help and your knowledge! Seems to be working fine.
Code:
def x = Round("number" = ((ask + bid) / 2 -close[1]) / close[1] * 100, "numberOfDigits" = 1);

# ensure x is between pMin and pMax and store in c1
def pMin = -100;
def pMax = 100;
def range = pMax - pMin;
def c1 = Min(pMax,Max(pMin,x));
# percent of range
def por = (c1-pMin)/range;
def red = Round(255*por,0);
def green = Round(255-255*por,0);

AssignBackgroundColor(CreateColor(green, red, 0));

plot pct = x;

pct.AssignValueColor(if por>0.75 then Color.Black else Color.Black);
 

korygill

Active member
VIP
Well, as usual, TOS is quirky or broken or both, or my other screens are impacting this, but you need to do 3 things (see image).
1) I have seen bid or ask return NaN so the % change is like half what it should be. Not reliable. Can use close.
2) You have to use a faster aggregation like 1m or 5m, but then you need to use AggregationPeriod>DAY to get prev day close
3) Also see using close/close with priceType.BID/etc.

But...something like this should work.

NOTE: watchlist columns run at an indeterminate time on the server backend. Could be 1 minute, could be 30 minutes!

No clue why the other rows have NaN for this study. Maybe debug this more, remove code, test with a column with just close or BidAsk/2, etc and find out why TOS is not working here.

Hr3HrMR.png
 

majidg

Member
@korygill Thanks for the help and your knowledge! Seems to be working fine.
Code:
def x = Round("number" = ((ask + bid) / 2 -close[1]) / close[1] * 100, "numberOfDigits" = 1);

# ensure x is between pMin and pMax and store in c1
def pMin = -100;
def pMax = 100;
def range = pMax - pMin;
def c1 = Min(pMax,Max(pMin,x));
# percent of range
def por = (c1-pMin)/range;
def red = Round(255*por,0);
def green = Round(255-255*por,0);

AssignBackgroundColor(CreateColor(green, red, 0));

plot pct = x;

pct.AssignValueColor(if por>0.75 then Color.Black else Color.Black);
when I enter this, the error message says no such variable as Ask or Bid, is there a way to fix this?
 

Similar threads

Top