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
This picture is long, so be sure to scroll to see the code below!
Assume in the code below that
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!
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: