Home » Array Functions » array.join Function in Pinescript

array.join Function in Pinescript

Photo of author
Published on

Introduction

In this tutorial, we will explore the array.join function in Pine Script, which is a useful tool for concatenating elements of an array into a single string. We will cover the function’s syntax and usage, and then dive into two unique use-case examples to help you understand the concept better.

Understanding the array.join Function

The array.join Function

The array.join function creates and returns a new string by concatenating all the elements of an array, separated by the specified separator string. The syntax for this function is:

array.join(id, separator)

Where:

  • id: This is the identifier of the array whose elements you want to concatenate.
  • separator: This is the string used to separate each array element in the resulting string.

Usage of array.join Function

Creating an Array

Before using the array.join function, you need to create an array using the array.new_<type> function. The <type> can be replaced with the data type you want to store in the array (e.g., float, int, bool, or line). Here’s an example of creating an array of floats:

//@version=4
study("My Script", overlay=true)
floatArray = array.new_float(5, 5)

Joining array Elements into a String

Now that we have created an array, let’s use the array.join function to concatenate its elements into a single string:

joinedString = array.join(floatArray, ",")

In this example, we have used the , separator to join the elements of the floatArray into the joinedString.

//@version=5
indicator('Basic Array.join Example', shorttitle='ArrayJoinExample', overlay=true)

// Create a new string array with some sample elements
stringArray = array.new_string(3)
array.set(stringArray, 0, 'Apple')
array.set(stringArray, 1, 'Banana')
array.set(stringArray, 2, 'Orange')

// Use array.join to concatenate the elements of the array into a single string, separated by a comma and a space
joinedString = array.join(stringArray, ', ')

// Display the joined string as a label on the chart
Clabel = label.new(bar_index, close, 'Fruits: ' + joinedString, color=color.green)
label.delete(Clabel[1])
array.join Function

Here’s a line-by-line explanation of the code:

  1. //@version=5: This line specifies the version of Pine Script being used. In this case, we are using version 5.
  2. ““““`indicator**(“Basic Array.join Example”, shorttitle=”ArrayJoinExample”, overlay=true): This line defines the script's name, short title, and sets the **overlay** property to **true`, which means that the script will be drawn on the main chart instead of a separate pane.
  3. stringArray = array.new_string(3): This line creates a new string array called stringArray with a fixed size of 3 elements.
  4. array.set(stringArray, 0, "Apple"): This line sets the first element (index 0) of the stringArray to the value “Apple”.
  5. array.set(stringArray, 1, "Banana"): This line sets the second element (index 1) of the stringArray to the value “Banana”.
  6. array.set(stringArray, 2, "Orange"): This line sets the third element (index 2) of the stringArray to the value “Orange”.
  7. joinedString = array.join(stringArray, ", "): This line uses the array.join function to concatenate the elements of the stringArray into a single string, separated by a comma and a space. The result is assigned to the variable joinedString.
  8. Clabel = label.new(bar_index, close, "Fruits: " + joinedString, color=color.green): This line creates a new label on the chart at the current bar’s index (bar_index) and close price (close). The text displayed in the label is “Fruits: ” followed by the joinedString. The color of the label is set to green.
  9. label.delete(Clabel[1]): This line deletes the previous label created in the script. By using Clabel[1], we access the label created one bar ago and delete it to avoid cluttering the chart with multiple labels. This ensures that only the latest label is visible on the chart.

Key Takeaways

  1. The array.join function in Pine Script concatenates elements of an array into a single string, separated by the specified separator string.
  2. The syntax for the function is array.join(id, separator), where id is the array identifier and separator is the string used to separate each array element.
  3. Before using the array.join function, an array must be created using the array.new_<type> function, where <type> can be float, int, bool, or line.
  4. The function can be used in various scenarios, such as displaying a string of multiple moving averages or showing high and low price statistics for a given lookback period.
  5. Understanding the array.join function allows for more effective data presentation and visualization in Pine Script, making it a valuable tool for developing informative trading strategies and indicators.

Conclusion

The array.join function in Pine Script is a versatile tool that allows developers to combine array elements into a single string. This capability is useful in a variety of applications, such as presenting calculated data on the chart or customizing the display of multiple indicators.

In this tutorial, we’ve covered the array.join function, its syntax, and its usage. We’ve also provided two unique use-case examples that demonstrate the function’s practical applications in trading strategies and indicators.

Leave a Comment