Home » Array Functions » Understanding the array.lastindexof() Function in Pine Script

Understanding the array.lastindexof() Function in Pine Script

Photo of author
Published on

This article delves into the array.lastindexof() function, explaining its syntax, functionality, and practical applications with a unique example for better understanding.


The array.lastindexof() function is designed to find the index of the last occurrence of a specified value within an array. If the value is not present in the array, the function returns -1. The syntax of this function is as follows:

array.lastindexof(arrayID, searchValue) → series int


  • arrayID (any array type): This is the array object in which the search for the specified value is performed.
  • searchValue (series ): The value to search for within the array.


Let’s take a closer look at a practical example to understand how array.lastindexof() is used in Pine Script:

indicator("array.lastindexof example")
myArray = array.new_float(5, high)
lastIndex = array.lastindexof(myArray, high)

Walkthrough of Code 

  1. Indicator Declaration: The script begins with the declaration of an indicator named “array.lastindexof example” using the indicator() function, compatible with Pine Script version 5.
  2. Array Creation: myArray is created using array.new_float(5, high). This line initializes a new array of floats with a length of 5, where each element is set to the current high price of the chart’s bars.
  3. Finding the Last Index: lastIndex is determined by applying array.lastindexof(myArray, high). This function call searches for the last occurrence of the current high value within myArray. If found, the index of the last occurrence is returned; otherwise, -1 is returned.
  4. Plotting the Result: Finally, plot(lastIndex) plots the index of the last occurrence of high in myArray on the chart. This visual representation can be useful for analysis and debugging purposes.

Key Features and Takeaways

  • Functionality: The array.lastindexof() function is crucial for searching through array data, especially when dealing with time-series data where you need to find the last occurrence of a specific value.
  • Syntax: Understanding the syntax is essential for correctly implementing the function in your scripts. The function expects an array and a value to search for, returning the index as an integer.
  • Application: This function is particularly useful in financial analysis for tracking price movements, identifying trends, or finding specific market conditions within historical data.
  • Return Value: A unique aspect of array.lastindexof() is its ability to return -1 if the specified value is not found. This feature can be strategically used to check the presence of a value before proceeding with further analysis.

In summary, array.lastindexof() is a versatile function in Pine Script that enhances the script’s ability to interact with and analyze array data efficiently. Whether you’re developing complex trading strategies or simple indicators, understanding and utilizing this function can significantly contribute to your script’s effectiveness and analytical capabilities.

Leave a Comment