Best Pine Script V6 Tutorial

Master Pine Script V6 with our comprehensive TradingView guide. Start your trading journey today!

Combining strings with TradingView's addition operator

Learn how to combine strings in Pine Script using the + operator. Master string concatenation for creating dynamic chart labels, alerts, and trading logs with practical examples and best practices.

Last updated on June 21, 2025
 

Combining Strings with TradingView's Addition Operator

When you first started learning Pine Script, you probably thought of the + operator as something that just adds numbers together. But here's something that might surprise you - this same operator can also glue strings together, creating longer text from smaller pieces. This process is called string concatenation, and it's one of those fundamental skills that'll make your Pine Script coding life much easier.

Introduction to String Concatenation

Let's start with the basics. In Pine Script, operators are symbols that perform operations on operands (the values you're working with). The addition operator + serves double duty - it adds numbers when you're doing math, but it concatenates strings when you're working with text.
String concatenation is simply the process of joining multiple strings together to form one larger string. Think of it like connecting train cars - each string is a car, and the + operator is the coupling that links them together.
You'll work with two types of strings in Pine Script:
  • String literals: These are fixed text values enclosed in quotes, like "Hello World"
  • String variables: These store string values that can change, like myText = "Current Price"
The beauty of string concatenation is that it lets you build dynamic, informative text that can display real-time data on your charts.

Features and Requirements of String Addition

Basic Requirements

Here's the thing about string concatenation - it's pretty straightforward when you follow the rules. Both operands need to be strings for the concatenation to work properly. You can simply use the + operator to join them:
str1 = "Hello" + "World" str2 = "Hello" + "World" + "!"
But here's where many beginners run into trouble. If you try to mix a string with a number directly, Pine Script will throw an error at you. For example, this won't work:
str3 = "Current Close: " + close // This will cause an error!

Data Type Conversion

The solution is simple - you need to convert non-string values to strings first using the str.tostring() function. Here's how you fix the previous example:
str3 = "Current Close: " + str.tostring(close)
This function is your best friend when you want to include numeric values like prices, volumes, or calculations in your text displays. However, keep in mind that some chart display functions have limitations - they might require "const string" arguments, which means they can't contain values that change from bar to bar.

Practical Examples

Example 1: Combining String Literals

Let's say you want to create a multi-line annotation that shows when price hits a 30-bar high or low. You can use the newline character \n to create multiple lines of text:
//@version=6 indicator("High/Low Detection", overlay=true) highestHigh = ta.highest(high, 30) lowestLow = ta.lowest(low, 30) if high == highestHigh label.new(bar_index, high, "30-Bar High!" + "\n" + "Price: " + str.tostring(high), style=label.style_label_down, color=color.green) if low == lowestLow label.new(bar_index, low, "30-Bar Low!" + "\n" + "Price: " + str.tostring(low), style=label.style_label_up, color=color.red)
This example combines string literals ("30-Bar High!", "\n") with converted numeric values to create informative labels on your chart.
notion image

Example 2: Mixing String Variables and Literals

You can also combine predefined string variables with literal text to create more dynamic displays:
//@version=6 indicator("Dynamic Labels", overlay=true) symbol_name = "BTC/USD" timeframe_text = "Daily" bullish_message = symbol_name + " is showing bullish momentum on " + timeframe_text bearish_message = symbol_name + " is showing bearish momentum on " + timeframe_text if close > open label.new(bar_index, high, bullish_message, color=color.green) else if close < open label.new(bar_index, low, bearish_message, color=color.red)
notion image

Technical Implementation Details

Syntax and Operators

One neat trick you can use is conditional concatenation with the ternary operator. You need to wrap the conditional part in parentheses for it to work properly:
flag = close > open message = "Price is " + (flag ? "bullish" : "bearish") + " today"
This approach lets you build different messages based on conditions without writing separate concatenation statements for each scenario.

Advanced Methods

While the + operator is great for simple concatenation, Pine Script offers more sophisticated methods when you need to combine multiple strings with the same separator. The array.join() function can be particularly useful:
price_array = array.from(open, high, low, close) ohlc_string = "OHLC: " + array.join(price_array, " | ")
This method is more efficient when you're working with multiple elements that need the same separator between them.

Best Practices and Limitations

Memory Considerations

Here's something important to understand - strings in Pine Script are immutable, meaning they can't be changed once created. Every time you concatenate strings, Pine Script creates a completely new string in memory rather than modifying the existing ones. This isn't usually a problem for typical trading scripts, but it's worth keeping in mind if you're doing heavy string manipulation.

Alternative Methods

For more complex string formatting needs, consider using str.format() instead of concatenation. This function is often more readable and efficient when you need to create formatted strings with multiple variables:
formatted_message = str.format("Symbol: {0}, Price: {1}, Volume: {2}", syminfo.ticker, close, volume)
This approach is cleaner than chaining multiple concatenation operations and gives you more control over the final formatting.

Summary

String concatenation with the + operator is a fundamental skill that opens up many possibilities for creating informative and dynamic chart displays. Remember these key points:
  • The + operator works for string concatenation just like it does for addition
  • Both operands must be strings - use str.tostring() to convert numbers
  • You can combine literals, variables, and converted values in any combination
  • Strings are immutable, so each concatenation creates a new string
  • For complex formatting, consider str.format() or array.join() as alternatives
Once you get comfortable with string concatenation, you'll find yourself using it everywhere - from creating detailed labels and alerts to building comprehensive trading logs. It's one of those skills that seems simple on the surface but becomes incredibly powerful as you explore its possibilities.