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!

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

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
376 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