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.

LabelStudyexcerpt
AddLabel(yes,
    "ROC: " + AsText(roc,
        NumberFormat.TWO_DECIMAL_PLACES),
    if roc > 0
    then Color.GREEN
    else Color.RED);
AddLabel
AsText
NumberFormat
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
# 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 is the visibility condition. Use yes to always show the label, or a condition to show it selectively.

AsText with NumberFormat constants

AsText
# 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 converts a number to a formatted string. Use NumberFormat constants or a custom format string like "%1$.4f".

AddLabel with optional parameters

AddLabel
# 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

ParameterDefaultDescription
visiblerequiredCondition that controls whether the label is shown.
textrequiredThe text to display. Concatenate strings and values with +.
colorColor.REDLabel color. Use Color constants or CreateColor.
locationLocation.TOP_LEFTChart position. Use Location constants.
sizeFontSize.SMALLFont size. Use FontSize constants.
row ownershipnoIf yes, no other label can share this row.

Frequently asked questions

Generate chart labels with AI

Describe the labels and formatting you need in plain English. The Pineify thinkorswim AI Coding Agent generates a complete thinkScript file with AddLabel, AsText, and the correct NumberFormat and Color constants. Review the source, then test it in the Editor.

Open the thinkScript AI agent

This page is for educational and informational purposes. It does not provide investment advice, predict future prices, or guarantee trading results.