The addition operator (+) is often used with calculating values in TradingView Pine. But we can also use this operator with strings. Let’s see how to do that.

In this article:

Adding strings with the addition operator in TradingView

Operators allow us to add complexity to our TradingView Pine scripts. An operator is a code element that performs an operation on one or several values (Stephens, 2014). We call the values that an operator ‘works on’ operands (Sharp, 2013). For example, with an expression like 10 - 3.5, the minus sign is the operator while 10 and 3.5 are its operands.

Common operators are the arithmetic operators (+, -, *, and /) and we use those when performing calculations (Pine Script Language Tutorial, n.d.). One of them, the addition operator (+), not only works with numbers (like 3 + 0.39 and high + low), but can operate on strings too. Before discussing that, let’s clarify a few terms.

To programmers, a string means a string of text (Stellman & Greene, 2010). There are two types of strings in TradingView (Pine Script Language Tutorial, n.d.):

  • A string literal is text that’s manually typed into the script’s source code and that’s surrounded by single or double quotation marks. Examples of string literals are "enter long" and 'volume increase'.
  • A string variable is a variable that holds text. This can be a built-in variable (like ticker, which returns a string with the instrument’s symbol; TradingView, n.d.) or a variable that we made ourselves. An example of that latter is myText = "long signal".

Now what the addition operator can do (besides adding numbers together) is adding one string to the end of another. Such an operation is called string concatenation (Stellman & Greene, 2010).

Adding strings together in TradingView with + has the following features:

  • String concatenation only works when both operands are already strings. This means "buy " + "signal" works (it returns the "buy signal" string), but "buy" + 10 + "contracts" doesn’t work: it returns the ‘cannot call operator + with arguments (…)’-error message.
  • When an operand is a number, we first need to convert it to a string before it can be used to create a combined string with +. That conversion is done with the tostring() function (TradingView, n.d.). So while "previous close " + close[1] gives an error, "previous close" + tostring(close[1]) does work.
Tip: At this moment, the tostring() function can only be used for comparisons with the equality comparison operator (==); tostring() cannot be used to display text on the chart with functions like plotshape() and plotchar().

Note that, when we combine strings with +, this addition operator merely adds the strings together – so "25" + "2" doesn’t return 27 but a string with the "252" text.

Example: putting string literals together with +

A basic example indicator that uses the + operator to combine string literals is the following:

study(title="Example of combining strings", overlay=true)

newHigh = (high == highest(high, 30))
newLow  = (low == lowest(low, 30))

highText = "New " + "\n" + "high"
lowText  = "New " + "\n" + "30-bar " + "\n" + "low"

plotshape(series=newHigh, location=location.abovebar, 
     style=shape.diamond, color=blue, text=highText)

plotshape(series=newLow, location=location.belowbar,
     style=shape.circle, color=purple, text=lowText)

We first define the script’s properties with the study() function. Then we make two variables:

newHigh = (high == highest(high, 30))
newLow  = (low == lowest(low, 30))

The first of these variables, newHigh, is set to a true/false expression that checks whether the current bar’s high (high) equals (==) the 30-bar high (including the current bar). We get that latter value with the highest() function, which returns the highest value for a certain amount of bars back (TradingView, n.d.). By having that function compute with the high and 30 arguments, it returns the highest high price of the recent 30 bars.

The other variable, newLow, is assigned a value in the same way, except that now we check if the current bar’s low equals the lowest low of the latest 30 price bars (lowest(low, 30)).

Next we create two additional variables:

highText = "New " + "\n" + "high"
lowText  = "New " + "\n" + "30-bar " + "\n" + "low"

Here we give the highText and lowText variables their values by using the + operator to combine several string literals together.

The highText variable is assigned a string that combines two words ("New" and "high") with the newline escape character ("\n") between them. That character separates text lines (Pine Script Language Tutorial, n.d.) and works just like the Enter key does when typing regularly.

The lowText variable is set to a similar string combination, though this text is longer and uses two newline characters. The benefit of using "\n" here is that it makes the text display less wide on the chart (as opposed to plotting one long line).

Then we plot the values of these variables on the chart:

plotshape(series=newHigh, location=location.abovebar, 
     style=shape.diamond, color=blue, text=highText)

plotshape(series=newLow, location=location.belowbar,
     style=shape.circle, color=purple, text=lowText)

The plotshape() function plots visual shapes on the chart whenever its series argument is true (Pine Script Language Tutorial, n.d.).

Here we set that argument of the first plotshape() function call to newHigh, and that makes the function draw a shape on the chart with every 30-bar high. We set the shape’s type to a diamond (shape.diamond) and place it above the bar (location.abovebar). The text that goes with this diamond is the highText variable, which we set to a combination of string literals earlier.

The second plotshape() function call is similar: this time we plot a circle (shape.circle) whenever the newLow variable holds true. That shape’s colour is set to the purple standard TradingView colour and is placed below the price bar (location.belowbar). The text that goes along this shape is set to the lowText variable.

When we add the above example to a chart, it looks like:

TradingView example of string concatenation

Here we see our “New high” and “New 30-bar low” text appear on the chart whenever the bar made a new 30-bar extreme price.

Combining strings and variables with the + operator in TradingView

The addition operator can also combine a string literal and a string variable together, like this:

study(title="Example of combining strings", overlay=true)

beginText = "Closed "

closedHigher = (close > open)

plotshape(series=closedHigher, style=shape.arrowup, 
     location=location.belowbar, color=green,
     text=beginText + "higher")

plotshape(series=not closedHigher, style=shape.arrowdown,
     location=location.abovebar, color=red,
     text=beginText + "lower")

We first use the required study() function here to set the script’s settings. Then we create the beginText string variable and store the "Closed " text in it. The next variable we make is closedHigher, and this one is assigned true or false based on an expression that evaluates whether the bar’s close is greater than (>) its open.

We then draw two arrows on the chart with plotshape():

plotshape(series=closedHigher, style=shape.arrowup, 
     location=location.belowbar, color=green,
     text=beginText + "higher")

plotshape(series=not closedHigher, style=shape.arrowdown,
     location=location.abovebar, color=red,
     text=beginText + "lower")

The first plotshape() function call draws an upward arrow (shape.arrowup) on the chart whenever the closedHigher variable is true. We place these arrows that mark higher closes below the bar (location.belowbar) and colour them with the green basic TradingView colour.

The text that’s accompanied with the arrow is generated with the addition operator (+) to join the beginText string variable and the "higher" string literal together. Since beginText includes a space, the two words display correctly when combined (see image below).

With the last plotshape() function call we draw a red, down-pointing arrow (shape.arrowdown) on the chart whenever the not closedHigher expression is true. Due to the not logical operator, this expression is true whenever closedHigher is false (Pine Script Language Tutorial, n.d.).

This means that those red arrows plot whenever the bar did not close higher (so with an unchanged or lower close). The location of these arrows is above the bar (location.abovebar), and the text that we plot on the chart are the beginText and "lower" strings combined with +.

This code example added to a chart looks as follows:

TradingView example of combining strings

Summary

A string of text is called a string, and a string can be a literal string (like "enter long") or a string variable that holds text. While the addition operator (+) is often used to add numbers together, it can combine string literals and string variables too. This requires that the value on the left and the value on the right of this operator are both strings. As a consequence of that, a number first needs to be converted to a string with the tostring() function before + can add it to another string. We can insert new lines into strings with the newline escape character ("\n").

Learn more:

  • While we can combine strings together with +, the most common use of this arithmetic operator is still adding values together. See the article about TradingView’s arithmetic operators for more.
  • Another atypical TradingView operator is the modulus operator, which returns the remainder of division.

References

Pine Script Language Tutorial (n.d.). Retrieved on August 13, 2015, from https://docs.google.com/document/d/1sCfC873xJEMV7MGzt1L70JTStTE9kcG2q-LDuWWkBeY/

Sharp, J. (2013). Microsoft Visual C# 2013 Step by Step. Microsoft Press.

Stellman, A. & Greene, J. (2010). Head First C#: A Brain-Friendly Guide (2nd edition). Sebastopol, CA: O’Reilly Media.

Stephens, R. (2014). C# 5.0 Programmer Reference. Indianapolis, IN: John Wiley & Sons.

TradingView (n.d.). Script Language Reference Manual. Retrieved on September 16, 2015, from https://www.tradingview.com/study-script-reference/