• Get $30 off VIP with code SUMMER30. Ends July 27. Happy trading!

Changing Text Label Color in thinkScript

Buckbull

Active member
How would I change the color of the text To Black what do I have to change or add ? Thank You

Code:
# creates custom watch list column that says "bullish" in green when both price > 50sma AND 50sma > 200sma
# or that says "bearish" in red when both price < 50sma AND 50sma < 200sma

# note the two risky or uncertain times are currently blank cells, both critera have been defined so that risky50over200 and risky200over50 could be used to create labels and/or color-coding for those conditions as well (via editing the AddLabel and/or the AssignBackgroundColor lines of code)

def x = SimpleMovingAvg("length" = 50);
def y = SimpleMovingAvg("length" = 200);

def risky50over200 = x > y AND close < x;
def risky200over50 = x < y AND close > x;

def bullish = close > x AND x > y;
def bearish = close < x AND x < y;

AddLabel (yes, if bearish then "bearish  " else if bullish then "bullish  " else " ");

AssignBackgroundColor (if bearish then color.RED else if bullish then color.GREEN else color.LIGHT_GRAY);
 
Last edited by a moderator:

tomsk

Well-known member
VIP
@Buckbull To change the text to black, just replace your AddLabel statement with the following

Code:
AddLabel (yes, if bearish then "bearish " else if bullish then "bullish " else " ", Color.BLACK);
 

Buckbull

Active member
@tomsk Could I trouble for one more thing ? If possible How would I add Average Daily Volume to this

Code:
#HINT: This study plots a chart label for Net Change and Percent Change from prior day's (regardless of time period of the chart). \n\n Label color is red if today's price is lower than yesterday's close.  \n\n And label color is green if today's price is higher than yesterday's close.  \n\n Label color is gray if today's price is identcal to yesterday's close.

input show_label = yes;
input show_bubble = no;


def period_Type = AggregationPeriod.DAY;

def begin = close(period = period_Type)[1];
def end = close(period = period_Type);
def NetChg = end - begin;
def PctChg = (end / begin) - 1;
def DayVolume =  volume(period = "DAY");

AddLabel(show_label, "Last: " + close + "  " + "Net Change:  " + AsDollars(NetChg) + "  " + "Percent Change: " + AsPercent(PctChg) + "  " + "Total Volume for the Day:  " + DayVolume , if NetChg > 0 then Color.GREEN else if NetChg < 0 then Color.RED else Color.LIGHT_GRAY);


def bar = if IsNaN(close)
             then if yes
                     then bar[1]
                     else Double.NaN
             else BarNumber();
def ThisBar = HighestAll(bar);
def barCount   = if bar == ThisBar
                 then close
                 else Double.NaN;

AddChartBubble(if show_bubble and bar == ThisBar then yes else no, if NetChg > 0 then high * 1.10 else low * 0.90, " Last: " + close + "  " + "\n Net Change:  " + AsDollars(NetChg) + "  " + "\n Percent Change: " + AsPercent(PctChg), if NetChg > 0 then Color.GREEN else if NetChg < 0 then Color.RED else Color.LIGHT_GRAY,  if NetChg > 0 then yes else no) ;
 
Last edited by a moderator:

tomsk

Well-known member
VIP
@Buckbull Sure, I have added the average daily volume over 20 days in the label. You can adjust the number of periods you wish via the input variable "periods_AvgDailyVolume" via the user interface

Code:
#HINT: This study plots a chart label for Net Change and Percent Change from prior day's (regardless of time period of the chart). \n\n Label color is red if today's price is lower than yesterday's close.  \n\n And label color is green if today's price is higher than yesterday's close.  \n\n Label color is gray if today's price is identcal to yesterday's close.

input show_label = yes;
input show_bubble = no;
input periods_AvgDailyVolume = 20;

def period_Type = AggregationPeriod.DAY;

def begin = close(period = period_Type)[1];
def end = close(period = period_Type);
def NetChg = end - begin;
def PctChg = (end / begin) - 1;
def DayVolume =  volume(period = "DAY");
def AvgDailyVolume = Average(DayVolume, periods_AvgDailyVolume);

AddLabel(show_label, "Last: " + close + "  " + "Net Change:  " + AsDollars(NetChg) + "  " + "Percent Change: " + AsPercent(PctChg) + "  " + "Total Volume for the Day:  " + DayVolume + " Average Daily Volume: " + AvgDailyVolume, if NetChg > 0 then Color.GREEN else if NetChg < 0 then Color.RED else Color.LIGHT_GRAY);

def bar = if IsNaN(close)
             then if yes
                     then bar[1]
                     else Double.NaN
             else BarNumber();
def ThisBar = HighestAll(bar);
def barCount   = if bar == ThisBar
                 then close
                 else Double.NaN;

AddChartBubble(if show_bubble and bar == ThisBar then yes else no, if NetChg > 0 then high * 1.10 else low * 0.90, " Last: " + close + "  " + "\n Net Change:  " + AsDollars(NetChg) + "  " + "\n Percent Change: " + AsPercent(PctChg), if NetChg > 0 then Color.GREEN else if NetChg < 0 then Color.RED else Color.LIGHT_GRAY,  if NetChg > 0 then yes else no) ;
 

DoQtorNo

Member
Howdy and good am,

Is there a way to change the text label color to white or bright white. Right now it comes up as dark gray and makes it hard to see on a gray background screen.

Code:
AddLabel(yes, "SPX: " + AsText(Spy_Close) + "  ( " + AsText(Spy_value_change) + "  / " + Round(Spy_percent_change, 2) + "%) ", (if Round(Spy_percent_change, 2) > 0 then Color.DARK_GREEN else Color.rED));
 

DoQtorNo

Member
Yeaph...thanks. I was pulling what little hair i had left out my head to figure it out LOL..

Ahhh, i thnk i see why...the label TEXT color is adopted from the background color of chart...
 

TCB

Member
Hi, is there anyway to change the foreground color of a text label? I changed the chart background to black and it is hard to see the black text of the label but it shows up well on a light background. This is the script for the label:
addLabel(yes, "Ch: " + chday, if pos then color.dark_green else color.dark_RED);

I tried to insert the images but it wouldn't accept it, so here is a link below

Darktext.png


light.png
 

TCB

Member
I have the background set for green for up net change or red for down net change so I can easily visually see it. If I were to change it to white it would defeat that purpose which is why I was hoping to be able to change the foreground color. Thanks
 

rad14733

Well-known member
VIP
I know the available colors but how do you change the foreground text color? White text with green background or white text with red background
No matter how many times members ask, we cannot manually change foreground (text) color, its line weight, etc... TOS does have limitations and this is one of them and we all have to learn to live with it until such time that the developers decide to add more Look and Feel features...
 

ripcityman

New member
@TCB, I feel your pain. I am not a coder, but learning bits and pieces. I wanted to have a simple watchlist column that wound display the "spread", or difference between the ask and the bid so I could quickly filter out stocks with BIG spreads. I found this example on the internet, sorry I can't give the author credit, as I can't find the URL for the video. You can change text color, per the following examples of code:

Code:
plot DiffSpread = (ask - bid);
DiffSpread.setDefaultColor (color.WHITE);
DiffSpread.DefineColor ("Above" , color.RED);
DiffSpread.DefineColor ("Below" , color.White);
DiffSpread. AssignValueColor (if DiffSpread < 0.05 then DiffSpread.color ("Below") else DiffSpread.color ("Above"));

You will get NaN for column results if using an intraday timeframe unless you check "Include Extended Hours Trading session". I believe this is due to the fact that ask and bid can not be used historically. Sometimes I find I get NaN for results if I set chart timeframe to DAY, which is confusing as that issue seems to be intermittent.

Below is another simple watchlist column, also borrowed from the internet that tracks Relative Volume, which is not available in TOS, however, RelativeVolumeStDev is a great study that is included.

Code:
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def vol = if isRollover and beforeStart then volume else if beforeStart then vol[1] + volume else Double.NaN;
def PMV = if IsNaN(vol) then PMV[1] else vol;
def AV = AggregationPeriod.DAY;
def x = Average(Volume(period=AV)[1],60);
def y1 = Round((PMV/x),2);
def L = Lg(y1);
def p = if L>=1 then 0 else if L>=0 then 1 else 2;
def y2 = Round(y1,p);
plot z = y2;
z.assignValueColor(if z>=7 then color.CYAN else if z>=1 then createcolor(255,153,153) else createcolor(0,215,0));

This code requires your chart timeframe be set to DAY.

This last example below, also borrowed but not credited to the author, plots Relative Volume %. If you run it you can see that the author picked colors that makes it extremely difficult to see the text in the box:

Code:
input offset = 1;
def rVol = volume(period = AggregationPeriod.Day) / Average(volume(period = AggregationPeriod.DAY), 63) [offset];
AddLabel(yes, Round(rVol, 2), Color.BLACK);
AssignBackgroundColor(if rVol > 10 then Color.BLUE
else if rVol > 8 then Color.DARK_GREEN
else if rVol > 6 then Color.GREEN
else if rVol > 4 then Color.LIGHT_GREEN
else if rVol > 2 then Color.LIME
else if rVol > 1 then Color.YELLOW
else if rVol < 1 then Color.PINK else Color.BLACK);
AddLabel(yes, asPercent(rVol));

If some generous coder out there could show me how to combine text and background colors, I would appreciate it. I want Green, Yellow ,and Red background for relative volume watchlist columns with text that would be visible. Yellow background with White text as an example just does not show.

I don't know if this helps you TCB?
Pat M
 

tomsey

Member
VIP
From a script perspective, is there any code that I can add to a script so the labels on the chart are smaller; say 25% to 50% smaller?
I have a 4x5 grid (20 charts) on my main monitor but when I add a study with labels, the labels are HUGE on the smaller charts, where real estate is at a premium....Have a great day!!!
 

Similar threads

Top