CODESCRIPT

Text

23 entries. What each one takes, what it returns, and a working example.

color(hex, alpha=1)

Adds transparency to a color (alpha 0..100).

A color in #rrggbbaa form with the opacity applied.

hex
color
A color (#rgb or #rrggbb).
alpha
number
Opacity 0-1 (0 fully transparent, 1 fully opaque). Defaults to 1.

Note

alpha is clamped to 0-1. If an invalid hex is given the input is returned unchanged. Ideal for making fills and backgrounds semi-transparent.

CODESCRIPT
plot(close, color=color("#26a69a", 0.5))

Plots price in a half-transparent green (#26a69a80).

contains(text, search)

Whether the text contains the substring (true/false).

true if part occurs anywhere in the text, otherwise false.

text
string
The text to search within.
search
string
The substring being searched for.

Note

Case sensitive. For classifying text such as a symbol name or a session label.

CODESCRIPT
plot(iff(contains("BTCUSDT", "USD"), 1, 0))

Since "BTCUSDT" contains "USD", it plots 1.

endsWith(text, suffix)

Whether the text ends with the suffix. Result: True

true if the text ends with that suffix, otherwise false.

text
string
The text being checked.
suffix
string
The suffix searched for at the end.

Note

Case sensitive. Handy for spotting a quote currency (…USDT).

CODESCRIPT
plot(iff(endsWith("BTCUSDT", "USDT"), 1, 0))

Since "BTCUSDT" ends with "USDT", it plots 1.

format(template,...values)

Formats a template string ('{0}'/'{1}'/'{}'). Result: 1-2

Text with placeholders filled; behaves the same as text.

template
string
Text with {0} {1} or {} placeholders.
...değerler
any
Values to fill the placeholders.

Note

An alias of text. {0} fills by index, {} in order; a decimal is trimmed to at most 4 places.

CODESCRIPT
if barstate.islast {
  infoTable(format("O:{0} C:{1}", open, close))
}
plot(close)

Joins open and close on one line and writes it into the panel.

formatTime(time)

Converts epoch time to a 'YYYY-MM-DD HH:MM' (UTC) string.

A date-time text in "YYYY-MM-DD HH:MM" form (UTC); an empty text on invalid input.

time
number
The timestamp to format (seconds or milliseconds).

Note

Auto-detects a seconds or milliseconds stamp. Output is in UTC. For showing a time on a label/panel.

CODESCRIPT
if barstate.islast {
  label(close, formatTime(time), barIndex)
}
plot(close)

Writes the last bar's time onto a label as a readable date-time.

gradient(value, lo, hi, loColor, hiColor)

Color gradient between two colors by value.

A color blended between the two colors by mapping value across [lo,hi] (a color series if a series is given).

value
series
The value or series that selects the color.
lo
number
Lower bound (maps to loColor).
hi
number
Upper bound (maps to hiColor).
loColor
color
The color at the lower bound.
hiColor
color
The color at the upper bound.

Note

If value falls outside the range it is pinned to the ends. For heatmap-like coloring — e.g. showing RSI shifting color from oversold to overbought.

CODESCRIPT
plot(close, color=gradient(rsi(close, 14), 30, 70, "#ef5350", "#26a69a"))

Colors price red near RSI 30 and green near 70.

indexOf(text, search)

Gives the position where a piece starts inside a text, or -1 when it is not there. ⚠ Check for -1 before using the result, otherwise you cut from the wrong place when the piece is missing.

The position of the first occurrence of part (0-based), or -1 if not found.

text
string
The text to search within.
search
string
The substring whose position is sought.

Note

Positions count from 0. A not-found result is signalled by -1.

CODESCRIPT
plot(indexOf("hello", "h"))

Since "h" is at position 3 in "merhaba", it plots 3.

lower(text)

Converts to lowercase. Result: btc

The text with all letters converted to lowercase.

text
string
The text to lowercase.

Note

To compare case-insensitively, normalize both sides with lower.

CODESCRIPT
if barstate.islast {
  label(close, lower("MERHABA"), barIndex)
}
plot(close)

Places a label reading "merhaba" on the last bar.

regexMatch(text, pattern)

First match against the regex pattern (empty string if none). Result: 123

The FIRST substring matching the pattern, or an empty text if there is no match.

text
string
The text to search in.
pattern
string
The regular-expression pattern to search for.

Note

Returns only the first match. Can be paired with toNumber to extract a number or pattern from text.

CODESCRIPT
plot(toNumber(regexMatch("abc123def", "[0-9]+")))

The first digit block "123" is extracted, converted to a number, and plotted.

repeat(text, n)

Repeats the text n times. Result: ==========

The text written n times in a row.

text
string
The text to repeat.
n
number
The number of repetitions.

Note

If n is zero or negative it returns an empty text. Handy for a simple progress bar / separator strip.

CODESCRIPT
plot(strLength(repeat("ab", 3)))

"ab" is repeated 3 times ("ababab"); its length 6 is plotted.

replace(text, old, new)

Replaces the first match. Result: a+b-c

The text with only the FIRST match replaced.

text
string
The text to modify.
old
string
The substring searched for.
new
string
The replacement substring.

Note

Replaces only the first match; use replaceAll for every match.

CODESCRIPT
if barstate.islast {
  label(close, replace("aXbXc", "X", "-"), barIndex)
}
plot(close)

The first X is replaced; the label reads "a-bXc".

replaceAll(text, old, new)

Replaces every match inside a text with a new piece — all of them, not just the first. Used to change a decimal separator or to clean up a text.

The text with every match replaced.

text
string
The text to modify.
old
string
The substring searched for.
new
string
The replacement substring.

Note

Replaces all matches; use replace to change only the first.

CODESCRIPT
if barstate.islast {
  label(close, replaceAll("aXbXc", "X", "-"), barIndex)
}
plot(close)

All X's are replaced; the label reads "a-b-c".

startsWith(text, prefix)

Whether the text starts with the prefix. Result: True

true if the text starts with that prefix, otherwise false.

text
string
The text being checked.
prefix
string
The prefix searched for at the start.

Note

Case sensitive.

CODESCRIPT
plot(iff(startsWith("BTCUSDT", "BTC"), 1, 0))

Since "BTCUSDT" starts with "BTC", it plots 1.

str(value, format?)

Does the same job as `toString`, under a shorter name. Turns a number or a series into text; given a series it produces text for each bar.

The text form of the value; if a series is given, an element-wise text series. na → "NaN".

value
any
The number, boolean or series to convert to text.
format
string
Optional. A format: a pattern like "#.##", or "percent", "volume" or "mintick".

Note

Without a format it shows up to 10 decimals and trims trailing zeros. "percent" appends %, "volume" abbreviates large numbers with K/M/B/T, "mintick" rounds to the symbol's price step.

When to use

To write a numeric value onto a label, panel or table — to turn it into text before joining with fixed text.

CODESCRIPT
if barstate.islast {
  label(close, str(close, "#.##"), barIndex)
}
plot(close)

Converts close to text with two decimals and places it as a label on the last bar.

strFormat(template,...values)

Same as format().

Text with placeholders filled; behaves the same as text/format.

template
string
Text with {0} {1} or {} placeholders.
...değerler
any
Values to fill the placeholders.

Note

Another alias of text — the formatting behavior is identical.

CODESCRIPT
if barstate.islast {
  infoTable(strFormat("H:{0} L:{1}", high, low))
}
plot(close)

Joins high and low on one line and writes it into the panel.

strLength(text)

Gives the number of characters in a text. Used when splitting a text into pieces or checking whether it fits in a panel.

The number of characters in the text (a number).

text
string
The text whose length is requested.

Note

Returns 0 for an empty text.

CODESCRIPT
plot(strLength("hello"))

Since "merhaba" has 7 characters, it plots 7.

string(value)

Type conversion: text and na only (na is preserved). Use str.tostring to convert a number/bool to text.

The same text if the input is text, or the na text if it is na.

value
any
The value to convert to text (text or na only).

Note

Only accepts text/na; passing a number or boolean raises an error. Use str to turn a number/series into text.

Not for

Not for converting a number or boolean to text — use str (or toString) for that.

CODESCRIPT
plot(strLength(string("hello")))

The text input is returned unchanged; its length 7 is plotted.

substring(text, start, end?)

Cuts a piece out of a text, from a start position to an end position. Positions start at zero and the end position is not included. Used to take the first letters of a symbol name or to split a number.

The substring in the range [start, end).

text
string
The text to slice.
start
number
Start position (0-based).
end
number
Optional. End position (exclusive); if omitted, to the end.

Note

Positions are 0-based and end is excluded; if end is omitted it runs to the end of the text.

CODESCRIPT
plot(strLength(substring("hello", 0, 3)))

The first 3 characters ("mer") are taken; their length 3 is plotted.

text(template,...values)

Formats values into text (for labels/tables).

Text with the placeholders filled by the given values.

template
string
Text containing {0} {1} or sequential {} placeholders.
...değerler
any
Values to fill the placeholders (number/text/series).

Note

{0} fills by index, {} fills in order. A decimal is trimmed to at most 4 places; if a series is given its last valid value is written.

When to use

To embed a numeric value into a label, panel or table — for combining fixed text with a live value on one line.

CODESCRIPT
if barstate.islast {
  infoTable(text("Close: {0}", close))
}
plot(close)

On the last bar writes a "Kapanis: " line into the top-right panel.

CODESCRIPT
label(close, text("RSI {0}", rsi(close, 14)), 50)
plot(close)

Places a label at bar 50 showing the RSI value.

toNumber(text)

Converts text to a number. The WHOLE text must match number syntax (optional sign, decimal point, exponent: 42 / -2.5 /.5 / 1e3). Anything else returns an empty value: "1,5" (comma is not accepted as a decimal separator - use a dot), "12abc", "0x10", empty text. Check with na().

text
string
The text to convert to a number.

Note

Leading/trailing spaces are ignored. Invalid text does not raise an error, it returns na.

CODESCRIPT
plot(toNumber("3.14"))

Converts the text "3.14" to the number 3.14 and plots it.

toString(value, format?)

Turns a number into text, which you need to show it in a panel or a label. The second value sets the format (`"#.##"` for two decimals). ⚠ The format is not language aware: the decimal separator is a dot and no thousands separator is added — for local formatting you have to arrange the text yourself.

The text form of the value; behaves the same as str.

value
any
The number, boolean or series to convert to text.
format
string
Optional. A format pattern (same as str).

Note

An alias of str — it accepts the same format options. May be preferred for readability.

CODESCRIPT
if barstate.islast {
  label(close, toString(rsi(close, 14)), barIndex)
}
plot(close)

Converts the RSI value to text and writes it as a label on the last bar.

trim(text)

Trims leading/trailing whitespace.

The text with leading and trailing whitespace removed.

text
string
The text to trim.

Note

Only removes whitespace at the ends; interior spaces are kept.

CODESCRIPT
plot(strLength(trim("  ab  ")))

The edge spaces are stripped; the remaining "ab" has length 2, which is plotted.

upper(text)

Converts to uppercase. Result: BTC

The text with all letters converted to uppercase.

text
string
The text to uppercase.

Note

Handy for normalizing symbol names to a single form.

CODESCRIPT
if barstate.islast {
  label(close, upper("btc"), barIndex)
}
plot(close)

Places a label reading "BTC" on the last bar.

strPos(text, search)

Position of the substring, na if not found. Indexing starts at 0.

The position of the first occurrence of the part (0-based), or na if not found.

text
string
The text to search within.
search
string
The substring whose position is sought.

Note

Positions count from 0. The only difference from `indexOf` is the not-found case: `strPos` returns na, `indexOf` returns -1. Use `strPos` when you test the result with `na(...)`, because -1 is not na and that test would always be wrong.

CODESCRIPT
strPos("BTCUSDT", "USD")

Finds where "USD" starts inside "BTCUSDT"; it draws nothing and returns the position.

formatNumber(value, format?)

Turns a number into text using local writing: the thousands and decimal separators follow the user's language (`2,505.18` in English, `2.505,18` in Turkish). The second value does two jobs — pass text to pick the region (`"tr"` / `"en"`), or a number to set how many decimals to show. Pass nothing and the region comes from the user's language while the decimals come from the number itself. ⚠ `toString` does not do this: it always writes a dot decimal and no thousands separator.

The number as text, written the local way.

value
number
The number to turn into text. Given a series, each bar is converted separately.
format
string|number
Does two jobs: pass text to choose the region ("tr" / "en"), or a number to set how many decimals to show. Omit it and the region comes from the user's language while the decimals come from the number itself.

Note

⚠ `toString` does NOT do this and should not: it matches the reference language — always a dot decimal, no thousands separator. Use this one when you want a readable number in a panel or a label. Rounding is half-up: 2.675 to two decimals gives 2.68.

CODESCRIPT
price = 2505.18
alert(formatNumber(price))
alert(formatNumber(price, "en"))
alert(formatNumber(price, 4))

Formats a number for reading and sends it as an alert; the thousands separator and the number of decimals can be set.