Home » String Functions » Understanding str.repeat() Function in Pine Script

Understanding str.repeat() Function in Pine Script

Photo of author
Published on

In this article we delve into the str.repeat() function, which offers an elegant way to repeat strings, enhancing the flexibility and creativity of your scripts.

Syntax and Overloads

The str.repeat() function in Pine Script is versatile, allowing you to construct new strings by repeating a given source string a specified number of times, with the option to include a separator between each repetition. Pine Script provides various overloads of this function to accommodate different contexts, including constants, inputs, simple strings, and series strings. Here’s a closer look at its syntax:

str.repeat(source, repeat, separator) → const string
str.repeat(source, repeat, separator) → input string
str.repeat(source, repeat, separator) → simple string
str.repeat(source, repeat, separator) → series string


  • source (const string): The string you want to repeat.
  • repeat (const int): The number of times you want to repeat the source string. This must be greater than or equal to 0.
  • separator (const string): An optional argument that specifies the string to insert between each repetition of the source string. By default, this is an empty string, meaning no separator is used.

Example Explained

Let’s examine a practical example to understand how str.repeat() can be utilized within a Pine Script indicator:

indicator("str.repeat Example")
repeatedQuery = str.repeat("?", 3, ",") // Returns "?,?,?"
label.new(bar_index, close, repeatedQuery)

In this example:

  • We define an indicator named “str.repeat Example”.
  • We use str.repeat() to create a new string that repeats the “?” character 3 times, with a comma (,) as the separator between each repetition. The result, stored in repeatedQuery, is “?,?,?”.
  • Finally, we create a new label at the current bar’s index and closing price position, displaying our repeated string.


  • It’s crucial to note that str.repeat() will return na (Pine Script’s equivalent of “not available”) if the source string is na. This ensures your script can gracefully handle missing data.
  • The ability to inject a separator between repetitions adds a layer of customization, allowing for more readable and visually appealing strings in your indicators and strategies.

Key Features and Takeaways

  • Function Usability: str.repeat() is highly versatile, supporting different string types (constant, input, simple, series) for a wide range of applications in Pine Script.
  • Syntax and Application: The function follows a straightforward syntax, enabling easy string repetition and customization with optional separators.
  • Practical Use Cases: Ideal for generating dynamic labels, debugging messages, or creating customized text outputs in your trading indicators and strategies.

Leave a Comment