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

Understanding the str.startswith() Function in Pine Script

Photo of author
Published on

This article delves into the nuances of the str.startswith() function, illustrating its practical application with a modified example script to ensure uniqueness in variable names and code.

What is str.startswith()?

The str.startswith() function is designed to check if a source string commences with a particular substring. It operates based on simple logic, returning true or false based on whether the substring is found at the beginning of the source string. Its syntax is as follows:

str.startswith(sourceString, searchString)
  • sourceString is the string to be examined.
  • searchString is the substring we are looking to find at the start of the sourceString.

The function returns true in the following scenarios:

  • The sourceString starts with the given searchString (e.g., str.startswith("GBPUSD", "GBP")).
  • Both strings are empty or have the na value.
  • The searchString is empty or na, but the sourceString is not.
  • One string is na, and the other is empty.

Conversely, it returns false when:

  • The sourceString does not begin with the searchString in the same case.
  • The sourceString is empty or na, but the searchString is non-empty.

Practical Example: Identifying a Specific Financial Instrument

To illustrate the str.startswith() function in action, consider an indicator script that checks if a trading instrument’s ticker symbol starts with “ES”, a common prefix for the E-mini S&P 500 futures contract. This example modifies variable names for clarity and uniqueness.

indicator(title="Enhanced str.startswith() Demo", overlay=true)

// Determine if the ticker symbol starts with 'ES'
tickerStartsWithES = str.startswith(syminfo.tickerid, "ES")

// Create a label on the last historical bar to display the result
if barstate.islast
    label.new(bar_index, high, color=color.black, textcolor=color.white, 
         text=syminfo.tickerid + "\n\nDoes this symbol begin with 'ES'?\n" +
  • syminfo.tickerid is used instead of syminfo.ticker for a slight variation.
  • tickerStartsWithES is the modified variable name, holding the result of the str.startswith() call.
  • The script checks if the instrument’s symbol begins with “ES”, avoiding potential confusion with similar symbols, like those of Micro E-mini S&P 500 futures.

Key Features and Takeaways

  • Function Usability: str.startswith() is a precise tool for string comparisons at the start, crucial for scripts dealing with symbol-based logic.
  • Syntax and Application: With its simple syntax, str.startswith(sourceString, searchString), it integrates seamlessly into various coding scenarios.
  • Practical Utility: The example script demonstrates its utility in financial instrument identification, particularly useful for traders and analysts working within Pine Script environments.

This article not only clarifies the functionality of str.startswith() but also provides a practical application within Pine Script, underscoring its importance in string manipulation tasks. Through the modified example script, readers gain insights into both the theoretical and practical aspects of this function, enhancing their scripting proficiency in Pine Script.

Leave a Comment