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

Understanding the str.endswith() Function in Pine Script

Photo of author
Published on

Let’s dive into the details of str.endswith() to understand its syntax, overloads, arguments, and applications.

Syntax & Overloads

The str.endswith() function can be utilized in three different forms, each returning a different type of boolean value. The syntax for these forms is as follows:

  1. Constant Boolean Return:
   str.endswith(source, str) → const bool
  1. Simple Boolean Return:
   str.endswith(source, str) → simple bool
  1. Series Boolean Return:
   str.endswith(source, str) → series bool

Arguments

  • source (const string): This is the main string you are working with, from which you want to check if it ends with a certain substring.
  • str (const string): This is the substring you are looking for at the end of the source string.

Returns

The function returns a boolean value:

  • true if the source string ends with the substring specified in str.
  • false otherwise.

This boolean return can be of three types, as indicated by the function’s overloads: const bool, simple bool, or series bool, catering to different use cases in script execution.

Example

//@version=5
indicator("Ticker Endswith Example", overlay=true)

// Define the source string
string sourceTicker = "AAPLUS"
// Define the substring to search for
string searchSubstring = "US"

// Check if the ticker ends with "US"
bool endsWithUS = str.endswith(sourceTicker, searchSubstring)

// Plot the result
label.new(bar_index, high, text = endsWithUS ? "Ends with US" : "Does not end with US", color = endsWithUS ? color.green : color.red)
Example

Walkthrough

  1. Define the Source and Substring: We start by defining a sourceTicker string that represents the ticker we’re analyzing and a searchSubstring that contains the substring we’re searching for at the end of the ticker.
  2. Use str.endswith(): Next, we use the str.endswith(sourceTicker, searchSubstring) function to check if sourceTicker ends with searchSubstring. This returns true or false, which is stored in endsWithUS.
  3. Plot the Result: Based on the boolean value endsWithUS, we plot a label on the chart indicating whether the ticker ends with “US” or not, with the label color changing accordingly.

Key Features and Takeaways

  • Function Useability: str.endswith() is a versatile function that can be used in various scripting contexts in Pine Script to check the ending of strings, enhancing script logic and condition checks.
  • Syntax and Application: The function supports different return types (const bool, simple bool, series bool) to cater to the needs of static, runtime, and series-based conditions.
  • Practical Example: By using str.endswith(), traders and developers can efficiently filter symbols, tickers, or any string-based data according to their specific criteria, aiding in the analysis and strategy development.

In conclusion, str.endswith() is an essential string manipulation function in Pine Script that adds depth and flexibility to script development, particularly in the context of financial data analysis and custom indicator or strategy creation.

Leave a Comment