thinkScript text output
AddLabel and AsText
AddLabel displays text labels on a thinkorswim chart. AsText formats numbers with NumberFormat constants. Together they let you show calculated values, trend states, and formatted readings directly on the chart.
AddLabel(yes,
"ROC: " + AsText(roc,
NumberFormat.TWO_DECIMAL_PLACES),
if roc > 0
then Color.GREEN
else Color.RED);- AddLabel
- Displays text on the chart
- AsText
- Formats numbers as strings
- NumberFormat
- Controls decimal precision
Direct answer
How AddLabel and AsText work together
AddLabel takes a visibility condition, text, and an optional color. The text parameter accepts a string or a concatenation of strings and values using the plus sign. When you need to format a number, wrap it in AsText with a NumberFormat constant to control the decimal places.
The label appears in the upper left corner of the chart by default. You can change the location with Location constants, the font size with FontSize constants, and claim an exclusive row with the row ownership parameter.
Code examples
Three ways to use AddLabel and AsText
Basic AddLabel with condition and color
# Basic AddLabel with condition and color
def avg = Average(close, 20);
def isUp = close > avg;
AddLabel(yes, "SMA: " + avg, Color.CYAN);
AddLabel(yes,
if isUp then "Uptrend" else "Downtrend",
if isUp then Color.GREEN else Color.RED);The first argument controls visibility. AddLabel uses values from the last real bar rather than creating a historical label on every bar.
AsText with NumberFormat constants
# AsText with NumberFormat constants
input length = 9;
def roc = (close - close[length]) / close[length];
AddLabel(yes,
"ROC: " + AsText(roc, NumberFormat.TWO_DECIMAL_PLACES),
if roc > 0 then Color.GREEN else Color.RED);
AddLabel(yes,
"Price: " + AsText(close, "%1$.4f"),
Color.WHITE);AsText formats a number; it does not convert a ratio into a percentage. Here 0.05 denotes a 5% change. Check history availability and a nonzero reference price.
AddLabel with optional parameters
# AddLabel with optional parameters
def avgPrice = GetAveragePrice();
def trendUp = close > Average(close);
AddLabel(yes,
"Avg Price: " + avgPrice,
if trendUp then Color.GREEN else Color.RED,
location = Location.TOP_LEFT,
size = FontSize.SMALL,
"row ownership" = yes);Control location, font size, and row ownership. Row ownership prevents other labels from sharing the same row.
Function reference
AddLabel parameters
| Parameter | Default | Description |
|---|---|---|
| visible | required | Condition that controls whether the label is shown. |
| text | required | The text to display. Concatenate strings and values with +. |
| color | Color.RED | Label color. Use Color constants or CreateColor. |
| location | Location.TOP_LEFT | Chart position. Use Location constants. |
| size | FontSize.SMALL | Font size. Use FontSize constants. |
| row ownership | no | If yes, no other label can share this row. |
Primary sources
Official thinkScript references used here
Frequently asked questions
What does AddLabel do in thinkScript?
AddLabel adds a text label to the chart. It takes a visibility condition, the text to display, and optional color, location, font size, and row ownership parameters. The label appears in the upper left corner by default.
How do I format a number in a thinkScript label?
Use AsText to convert a number to a formatted string. Pass the value and a NumberFormat constant. For example, AsText(roc, NumberFormat.TWO_DECIMAL_PLACES) shows the value with two decimal places. You can also use a format string like "%1$.4f" for four decimal places.
What NumberFormat constants are available?
The NumberFormat constants include TWO_DECIMAL_PLACES and DOLLAR. You can also pass a custom format string like "%1$.4f" to AsText for other precision levels. See the NumberFormat reference page in the Learning Center for the full list.
How do I change the color of a label based on a condition?
Pass an if-then-else expression as the color parameter. For example, AddLabel(yes, "Trend", if close > avg then Color.GREEN else Color.RED) shows green when close is above the average and red otherwise.
Can I change the location and font size of a label?
Yes. AddLabel accepts optional location and size parameters. Use Location constants like Location.TOP_LEFT or Location.TOP_RIGHT, and FontSize constants like FontSize.SMALL or FontSize.X_LARGE. You can also set "row ownership" to yes to claim an exclusive row.
Can Pineify generate thinkScript with AddLabel and AsText?
Yes. Describe the labels and formatting you need in plain English. The Pineify thinkorswim AI Coding Agent can draft a thinkScript file with labels and formatting. Generated code can contain errors; Pineify does not compile or execute thinkScript in thinkorswim. Review the source and test it in the platform Editor.
Generate chart labels with AI
Describe the labels and formatting you need in plain English. The Pineify thinkorswim AI Coding Agent can draft the source. Check its functions, compile it in thinkorswim, and inspect the chart output; generation does not establish correctness.
This page is for educational and informational purposes. It does not provide investment advice, predict future prices, or guarantee trading results.