Home » Data Types In Pinescript » String Function in Pine Script

String Function in Pine Script

Photo of author
Published on

In this programming tutorial, we delve into the exciting world of Pine Script with a particular focus on strings. Pine Script, the native language of TradingView, allows users to create custom indicators, scripts, and strategies in the TradingView environment. In Pine Script, a string is a sequence of characters used to store and manipulate text.

Defining a String

In Pine Script, a string can be explicitly declared using the keyword string. The general syntax for declaring a string is string variableName = "your text". For example:

//@version=5
indicator("string")
string s = "Hello World!"    // Same as `s = "Hello world!"`

Here, we declare a string variable s and assign the value “Hello World!” to it. Note that the indicator function is used to set the title of the indicator plotted on the chart.

String Initialization with na

Sometimes, you might need to declare a string variable without assigning any value to it. In this case, you can use the na value, which stands for ‘not available’ or essentially an empty string. The syntax would look like this:

string s = na // same as ""

Here, the na essentially creates an empty string.

Using Strings in Pine Script

Strings are used in many different ways in Pine Script. They can be used to set plot titles, labels, and tooltips. Strings can also be used in conditional statements or for creating custom messages or alerts.

Let’s look at a more detailed example:

//@version=5
indicator("My Script", overlay=true)
string myMessage = "Price is above the moving average"
float closePrice = close
smaValue = ta.sma(close, 14)
plot(smaValue)
var label l = na
if closePrice > smaValue
    l :=label.new(bar_index, close, text = myMessage , size = size.large)
label.delete(l[1])
String Function

Let’s break down the script line by line:

  1. //@version=5: This line defines the version of the Pine Script that is being used. As of now, we are using version 5.
  2. indicator("My Script", overlay=true): The indicator function is used to set the properties of the indicator. “My Script” is the title of the indicator. The overlay=true argument means that the indicator will be drawn in the same panel as the price chart.
  3. string myMessage = "Price is above the moving average": Here, a string variable named myMessage is declared and assigned the value “Price is above the moving average”.
  4. float closePrice = close: Here, a float variable closePrice is declared and assigned the value of the closing price of the current bar.
  5. smaValue = ta.sma(close, 14): The ta.sma function calculates the Simple Moving Average of the closing price over the past 14 bars. The resulting SMA value is stored in the variable smaValue.
  6. plot(smaValue): This line plots the SMA value on the chart.
  7. var label l = na: This line declares a label variable l and initializes it with na, which stands for ‘not available’. The var keyword means that the variable will keep its value between updates. This way, you can keep track of labels you create to modify or delete them later.
  8. if closePrice > smaValue: This line is a conditional statement that checks if the closing price is greater than the SMA value.
  9. l :=label.new(bar_index, close, text = myMessage , size = size.large): If the condition is met (i.e., the closing price is above the SMA), a new label is created at the position of the current bar (bar_index) and at the level of the closing price. The text of the label is the string stored in myMessage, and its size is set to size.large. The := operator is used to update the value of the variable l with the id of the new label.
  10. label.delete(l[1]): This line deletes the label created two bars ago. In Pine Script, l[1] refers to the value of l from the previous bar, so l[1] actually refers to the label created in the previous bar. This line ensures that there is only one label on the chart at a time, the most recent one.

Key Takeaways

In Pine Script, strings play a vital role in defining and manipulating textual data. They are versatile and can be used in a myriad of ways, from setting titles and labels to creating custom messages for alert conditions. The understanding and effective usage of strings allow for a more robust and interactive scripting experience.

Conclusion

To sum up, strings in Pine Script are essential for handling and manipulating textual information. Whether you are creating simple scripts or advanced strategies, understanding how to work with strings effectively is a crucial part of Pine Script programming. This tutorial aimed to provide a foundational understanding of strings and their usage in Pine Script. Keep experimenting and happy coding!

Leave a Comment