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

Understanding the array.last() Function in Pine Script

Photo of author
Published on

In Pine Script, the array.last() function is a powerful tool that programmers use to access the last element of an array efficiently. This feature is especially useful in financial chart analysis and indicator development, where accessing the most recent data points is a common task. This article will explore the array.last() function, providing a comprehensive tutorial on its syntax, usage, and application in Pine Script programming.

Introduction to array.last()

The array.last() function is designed to return the last element of an array. It’s a straightforward yet essential function in the realm of Pine Script, particularly when dealing with dynamic data arrays in trading algorithms and indicators. However, it’s crucial to note that this function will throw a runtime error if the array is empty. This behavior emphasizes the need for careful array management and error handling in your scripts.


The syntax of the array.last() function is:

array.last(id) → series <type>


  • id (any array type): This is the array object from which you want to retrieve the last element. The array must be previously defined and contain at least one element to avoid runtime errors.

Example: Plotting the Last Element of an Integer Array

To illustrate how array.last() works in practice, consider the following Pine Script code:

indicator("array.last example")
myArray = array.new_int(3, 10)

Walkthrough of the Code

  1. Indicator Declaration: The indicator() function declares the script as a trading indicator, which can be used on trading platforms that support Pine Script.
  2. Array Creation: myArray = array.new_int(3, 10) creates a new integer array named myArray with 3 elements, each initialized to the value 10.
  3. Accessing the Last Element: array.last(myArray) retrieves the last element of myArray. Given that all elements are initialized to 10, the last element is also 10.
  4. Plotting the Last Element: plot(array.last(myArray)) plots the value of the last element of myArray on the chart. In this case, it plots the value 10.

Key Features

  • Function Usability: array.last() is versatile, applicable to arrays of any type, including integer, float, boolean, and label arrays.
  • Syntax and Application: The function requires a single argument, the array identifier, making its syntax straightforward and its application easy to grasp for beginners.
  • Runtime Error Handling: Since array.last() throws a runtime error if the array is empty, it emphasizes the importance of managing array contents and incorporating error handling mechanisms in your scripts.


  • The array.last() function is crucial for accessing the last element of an array in Pine Script.
  • Its straightforward syntax and the requirement of only an array identifier as an argument make it an accessible tool for programmers at all levels.
  • Being mindful of the runtime error thrown by an empty array is essential for robust script development.

This tutorial aimed to demystify the array.last() function in Pine Script, providing you with the knowledge to utilize it effectively in your trading indicators and strategies.

Leave a Comment