Home » Array Functions » Exploring the array.includes() Function in Pine Script

Exploring the array.includes() Function in Pine Script

Photo of author
Published on

In this tutorial, we’ll delve into the syntax, usage, and practical applications of the array.includes() function, providing a comprehensive understanding through examples and detailed explanations.

Syntax and Arguments

The array.includes() function has a straightforward syntax:

array.includes(id, value) → series bool
  • id (any array type): This is the array object you’re working with. It can be an array of any type recognized by Pine Script, such as array.new_int(), array.new_float(), etc.
  • value (series ): The value you wish to search for within the array. The type of this value must match the type of the elements stored in the array.

Example

Let’s break down the provided example to understand how array.includes() can be utilized within a trading script.

Script

//@version=5
indicator("array.includes example")
a = array.new_float(5, high)
p = close
if array.includes(a, high)
    p := open
plot(p)
Example
  • Indicator Declaration: The script begins with declaring a version 5 indicator named “array.includes example”.
  • Array Creation: a = array.new_float(5, high) creates a new floating-point array a with 5 elements, each initialized to the current bar’s high price.
  • Price Variable: A variable p is initialized to the current bar’s close price.
  • Condition Check: The if array.includes(a, high) line checks if the current bar’s high price is found within the array a.
  • If true, the script sets p to the current bar’s open price.
  • Plotting: The plot(p) function plots the value of p on the chart, which will be the close price by default, unless the high price of the current bar is found in the array a, in which case it plots the open price.

Key Features

  • Function Usability: The array.includes() function is versatile, allowing for dynamic checks within arrays of any type. This is particularly useful for strategies that involve tracking specific values over a period, such as highs, lows, or any custom metric.
  • Syntax and Application: This function enhances script flexibility and decision-making capabilities by enabling conditional operations based on the presence of specific values within arrays.
  • Performance Considerations: While powerful, it’s important to use this function judiciously within scripts to maintain optimal performance, especially when dealing with large arrays or complex conditions.

Takeaways

  • The array.includes() function is essential for searching specific values within arrays in Pine Script.
  • Proper usage involves understanding the type compatibility between the array and the value being searched.
  • This function enhances the logic and decision-making capabilities of trading scripts, enabling more dynamic and condition-based trading strategies.

By incorporating the array.includes() function into your Pine Script strategies, you can significantly enhance their sophistication and adaptability, leading to potentially more informed and nuanced trading decisions.

Leave a Comment