Home » Array Functions » Understanding the array.indexof Function in Pine Script

Understanding the array.indexof Function in Pine Script

Photo of author
Published on

In this tutorial, we’ll deep dive into one of Pine Script’s powerful array functions – array.indexof(). This function is instrumental in searching for the index of a specific value within an array. Pine Script, being a domain-specific language for creating custom technical analysis indicators, offers this function to ease the process of handling arrays during the development of scripts.

How Does array.indexof() Work?


The array.indexof() function is characterized by a straightforward syntax, making it easily accessible for both novice and experienced developers. Below is the general syntax:

array.indexof(id, value) → series int


  • id (any array type): This is the array object that you want to search within.
  • value (series ): The specific value you are looking to find within the array.


The function returns the index of the first occurrence of the specified value, or -1 if the value is not found.

Real-World Use Case Example

Here’s a practical example that demonstrates the usage of the array.indexof() function in the real world:

indicator("array.indexof example" , overlay = true ) 
a = array.new_float(1,1)
array.push(a , 2)
array.push(a , 3)
array.push(a , 4)
array.push(a , 5)
array.push(a , 6)
index = array.indexof(a, 4)
labell = label.new(x = bar_index , y= high , text = "Index Of 4 in array " + str.tostring(a) + " \n is "+ str.tostring(index) , color = color.white)
label.delete(labell[1] )

Code Explanation

In this script, we’re using array.indexof() to find specific resistance levels on a chart. Let’s break down the code line by line:

  1. indicator("array.indexof example", overlay = true):
    • This line declares a new indicator with the name “array.indexof example”.
    • overlay = true means that the indicator will be drawn over the price chart.
  2. a = array.new_float(1,1):
    • This line initializes a new array named ‘a’ of type float with an initial size of 1 and a default value of 1 for all elements.
  3. array.push(a, 2) to array.push(a, 6):
    • These lines add the values 2, 3, 4, 5, and 6 to the end of the array ‘a’.
  4. index = array.indexof(a, 4):
    • This line searches for the value 4 in the array ‘a’ and assigns the index of the first occurrence to the variable ‘index’.
  5. labell = label.new(x = bar_index, y = high, text = "Index Of 4 in array " + str.tostring(a) + " \n is " + str.tostring(index), color = color.white):
    • This line creates a new label on the chart with the following properties:
      • x = bar_index: The label’s x-coordinate is set to the current bar index.
      • y = high: The label’s y-coordinate is set to the current bar’s high price.
      • text: The text for the label. It includes the text “Index Of 4 in array “, the content of the array ‘a’ converted to a string, and the index of the number 4 in the array, also converted to a string. The \n is a newline character, meaning the text will appear on two lines.
      • color = color.white: The color of the label’s text is set to white.
  6. label.delete(labell[1]):
    • This line deletes the previous label created by the script (if any) to avoid cluttering the chart. The indexing [1] refers to the historical label, where the current label is indexed as [0].

The overall functionality of this script is to demonstrate how to use arrays and specifically the array.indexof function in Pine Script. It will display a label on the chart indicating the index position of the number 4 in the array ‘a’, which would be ‘3’ because arrays are zero-indexed (i.e., the count starts from 0).

Key Takeaway

The array.indexof() function is an invaluable tool for developers working with arrays in Pine Script. It allows for efficient and rapid searching within arrays, enhancing the functionality and flexibility of custom indicators and strategies. Understanding its syntax and application is crucial for handling arrays and manipulating data effectively in Pine Script.


As we wrap up this tutorial, it’s essential to reiterate the significance of array.indexof() in data handling and manipulation within Pine Script. Its simplicity and efficiency make it a go-to function for many developers aiming to enhance their scripts’ functionality.

Remember, the function returns the index of the first occurrence of the value, or -1 if the value isn’t found. Always ensure that your arrays and the values you are searching for are defined and initialized appropriately to avoid unexpected results or errors.

Happy coding, and may your custom indicators and strategies be as efficient and effective as possible!

Leave a Comment