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

#### korygill

##### Well-known 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!

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:
what exactly does this do.. i dont no how to put things in the watchlist

Search is your friend.

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

this is really nice and beautiful, thanks for sharing @korygill

@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.

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

Very clever bit of coding, korygill. Thanks for sharing.

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.

rgb color blender
https://meyerweb.com/eric/tools/color-blend/#:::hex
i use this to generate a series of rgb color numbers, between 2 colors.
say i want to transition from red to green, and i have 7 possible values from a variable, i would look for 5 mid points.
after using this, i can see the different rgb numbers and come up with formulas to generate them, or use the above codes.

@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?

Thread starter Similar threads Forum Replies Date
Recursion: store a value / signal in ThinkOrSwim Tutorials 1

### Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
354 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• 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?