Home » Array Functions » Understanding the array.new_string() Function in Pine Script

Understanding the array.new_string() Function in Pine Script

Photo of author
Published on

In this article, we’ll delve into the array.new_string() function, a tool for creating arrays composed of string elements.

What is array.new_string()?

The array.new_string() function initializes a new array object that holds strings. This capability is essential when you need to store textual data, such as labels, names, or any series of strings, for processing within your script.


array.new_string(size, initial_value) → array<string>


  • size (series int): Specifies the initial size of the array. It’s optional, and if not provided, the default is 0.
  • initial_value (series string): Determines the initial value to fill the array elements with. This argument is also optional, with the default being 'na'.


Let’s take a closer look at how to use array.new_string() with a practical example:

indicator("array.new_string demo", overlay=true)
arraySize = 5 // Modifying variable name for uniqueness
strArray = array.new_string(arraySize, "sample") // Changed variable and value names

// Correct usage of the label.new function
// Ensure to use the '+' operator correctly for string concatenation
label.new(bar_index, close, text="First Element: " + array.get(strArray, 0))

Walkthrough of Code

  1. Indicator Declaration:
    • //@version=5 specifies the use of version 5 of Pine Script.
    • indicator("array.new_string demo", overlay=true) declares a new indicator with the name “array.new_string demo”. The overlay=true parameter ensures that the indicator is drawn over the price chart.
  2. Array Initialization:
    • arraySize = 5 sets the initial size of the array to 5. This variable declaration modifies the name for uniqueness and clarity.
    • strArray = array.new_string(arraySize, "sample") creates a new array named strArray with 5 elements, each initialized to the string “sample”.
  3. Label Creation:
    • label.new(bar_index, close, text="First Element: " + array.get(strArray, 0)) creates a new label on the chart. This label is positioned at the current bar’s index (bar_index) and the closing price (close) of that bar. The text parameter is set to “First Element: ” concatenated with the first element of the strArray. This demonstrates how to access array elements (using array.get(array, index)) and how to concatenate strings with the + operator.


This function returns the identifier of the newly created array object. You can use this ID with other array functions (like array.get(), array.set(), etc.) to manipulate the array.


  • An array’s index in Pine Script starts from 0, meaning the first element is accessed with index 0.
  • This function is particularly useful when you need to track or manipulate a series of strings across different bars on a chart.

Key Features

  • Function Usability: array.new_string() is ideal for scenarios requiring the storage and manipulation of text data in scripts.
  • Syntax Flexibility: Offers optional parameters for initial size and value, providing flexibility in array initialization.
  • Application Versatility: Can be used in a wide range of applications, from generating dynamic labels to managing complex data structures within your indicators or strategies.


  • The array.new_string() function is a powerful tool for initializing string arrays in Pine Script.
  • It offers flexibility in setting the initial size and values of the array.
  • Understanding how to use this function expands your capabilities in handling text data within TradingView scripts.

Through this example and explanation, you should now have a solid understanding of how to utilize the array.new_string() function in your Pine Script projects. 

Leave a Comment