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

Understanding the str.contains() Function in Pine Script

Photo of author
Published on

In this tutorial, we’ll dive deep into the str.contains() function in Pine Script, a powerful tool for string manipulation within the TradingView scripting environment. Whether you’re creating custom indicators, strategies, or just automating parts of your trading analysis, understanding how to use str.contains() can add a significant layer of flexibility to your scripts.

What is str.contains()?

The str.contains() function is used to determine whether a given substring exists within another string. It returns a boolean value: true if the substring is found, and false otherwise.

Syntax & Overloads

Pine Script offers multiple overloads for the str.contains() function, allowing it to return different types of boolean values based on the context:

  • str.contains(source, str) → const bool
  • str.contains(source, str) → simple bool
  • str.contains(source, str) → series bool


  • source (const string): The source string in which to search for the substring.
  • str (const string): The substring to search for within the source string.

Practical Example

indicator("Detect Futures Contract")
// Check if the current chart symbol is a continuous futures contract.
var isContinuousFutures = str.contains(syminfo.tickerid, "1!")
plot(isContinuousFutures ? 1 : 0)

In this example, we’ve slightly modified the original code snippet to check for “1!” instead of just “!”, to specifically target continuous futures contracts. This change is for illustration purposes, demonstrating how to customize the use of str.contains() for different scenarios.

Breaking Down the Example

  • indicator("Detect Futures Contract"): This line declares a new indicator named “Detect Futures Contract”.
  • var isContinuousFutures = str.contains(syminfo.tickerid, "1!"): We declare a variable isContinuousFutures that uses str.contains() to check if the syminfo.tickerid (the symbol’s ticker ID) contains the substring “1!”. If so, the variable is set to true.
  • plot(isContinuousFutures ? 1 : 0): This line plots a value of 1 on the chart if isContinuousFutures is true (indicating a continuous futures contract), and 0 otherwise.

Key Features and Takeaways

  • The str.contains() function is essential for string manipulation in Pine Script, especially useful in conditional logic.
  • It supports multiple overloads to accommodate different scripting needs, including constant, simple, and series boolean returns.
  • By modifying the substring argument, users can tailor the function to identify various patterns and conditions within strings, enhancing script flexibility.
  • In the provided example, we demonstrated how to utilize str.contains() to detect continuous futures contracts on TradingView charts, showcasing its practical application in financial scripting.

This tutorial should equip you with the knowledge to effectively utilize the str.contains() function in your Pine Script projects, opening up new possibilities for data analysis and indicator development on TradingView.

Leave a Comment