In Pine Script, working with strings (text values) is a common task, especially when dealing with symbol tickers or custom text annotations. Understanding how to compare strings is crucial for developing robust trading scripts. Pine Script offers a set of functions designed specifically for string comparison, enabling developers to test if strings contain, start with, or end with certain substrings. Additionally, Pine Script provides logical operators for equality and inequality comparisons. In this article, we’ll explore these functions and operators in detail.
String Comparison Functions in Pine Script
Function | Description |
---|---|
str.contains() | Determines if a string includes another string as a substring. |
str.startswith() | Checks if a string begins with a specified substring. |
str.endswith() | Verifies if a string ends with a particular substring. |
These functions significantly enhance the flexibility of string handling in Pine Script, allowing developers to perform a wide range of text-based operations efficiently.
String Contains Another String
The str.contains()
function is used to check if a string (haystack
) contains another string (needle
). It is case-sensitive, meaning it distinguishes between uppercase and lowercase letters. The function returns true
if the needle
is found within the haystack
and false
otherwise.
Example:
// Verify if the 'BTCUSD' string contains the phrase 'BTC' instrumentBTC = str.contains("BTCUSD", "BTC")
In this example, str.contains()
returns true
since ‘BTC’ is found within ‘BTCUSD’.
Consider a case-sensitive scenario:
// Look if the instrument's ticker contains the text 'btc' instrumentBTC = str.contains(syminfo.tickerid, "btc")
Here, even if the ticker symbol includes ‘BTC’ in uppercase, searching for ‘btc’ in lowercase results in false
due to the case sensitivity.
String Starts With Another String
The str.startswith()
function checks if a string begins with a specified substring. It is also case-sensitive.
Example:
// Check if the 'SOLUSD' string begins with the 'SOL' phrase instrumentSOL = str.startswith("SOLUSDT", "SOL")
This function returns true
as ‘SOLUSDT’ indeed starts with ‘SOL’.
Case sensitivity example:
// Verify if the instrument's ticker starts with 'sol' instrumentSOL = str.startswith(syminfo.tickerid, "sol")
In this scenario, the function returns false
for uppercase tickers, highlighting the importance of matching case in comparisons.
String Ends With Another String
Lastly, the str.endswith()
function determines if a string ends with a specified substring, following the same case-sensitive comparison principle.
Example:
// Determine if 'EURGBP' ends with 'GBP' instrumentGBP = str.endswith("EURGBP", "GBP")
Here, str.endswith()
returns true
, confirming ‘EURGBP’ ends with ‘GBP’.
Case sensitivity test:
// Test if the ticker symbol ends with 'gbp' instrumentGBP = str.endswith(syminfo.tickerid, "gbp")
This example will result in false
for uppercase tickers, illustrating the case-sensitive nature of the function.
Key Features and Takeaways
- Function Usability: Pine Script provides
str.contains()
,str.startswith()
, andstr.endswith()
for versatile string comparisons. - Syntax and Application: These functions require two arguments—the string to check and the substring to find—and are case-sensitive, affecting their use in scripts.
- Logical Operators: In addition to functions, Pine Script supports
==
and!=
operators for direct string equality and inequality checks.
Understanding and effectively utilizing these string comparison functions and operators are fundamental for scripting in Pine Script, especially when dealing with textual data or identifiers.