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

Understanding the array.first Function in Pine Script

Photo of author
Published on

In this tutorial, we will delve deep into one specific function associated with arrays in Pine Script – the array.first function. We’ll explore its syntax, applications, and illustrate its use with a practical example.

What is array.first Function?

Definition

The array.first function is utilized to retrieve the first element of an array in Pine Script. It is particularly handy in scenarios where quick access to the initial element is required, without the need to traverse the entire array.

Syntax

The syntax for this function is straightforward. It accepts an array object as its parameter and returns the first element of that array. If the array is empty, a runtime error is thrown, ensuring that your script doesn’t proceed with an undefined or non-existent value.

array.first(id) → series<type>

Here, id represents any array type, which is the array from which the first element will be fetched.

A Practical Example

Let’s bring this function to life with a practical example.

//@version=5
indicator("My Array First Example", overlay = true)
arr = array.new_float(5)
for i = 1 to 5
    array.push(arr, ta.sma(close, i*5))
plot(array.first(arr), color=color.red, title="First SMA")

Code Breakdown

In this example, we create an array of floating-point numbers and populate it with the values of simple moving averages (SMAs) calculated with different periods. We then use the array.first function to plot the first SMA on the chart.

  1. We initiate a new indicator script with the version 5 of Pine Script and set its title and overlay property.
//@version=5
indicator("My Array First Example", overlay = true)
  1. We then create a new float array with an initial capacity of 5.
arr = array.new_float(5)
  1. A for loop is used to populate this array with SMAs of closing prices, each having a unique period calculated as i*5.
for i = 1 to 5
    array.push(arr, ta.sma(close, i*5))
  1. The array.first function is employed to retrieve the first element of the array (the first SMA), which is then plotted on the chart in red color.
plot(array.first(arr), color=color.red, title="First SMA")

Key Takeaway

The array.first function is not just a tool for accessing the initial element of an array; it embodies efficiency and simplification. It mitigates the need for complex code structures to achieve what can be done in a single line of code, making scripts cleaner, more readable, and optimized. It’s essential to handle this function with care, considering it throws a runtime error if the array is empty, hence ensuring that arrays are appropriately checked or pre-populated before access is attempted can save a lot of debugging time.

Conclusion

Understanding and efficiently using the array.first function can be a substantial advantage in crafting efficient and optimized Pine Scripts. Especially in scenarios where the performance and speed of the script are paramount, avoiding unnecessary loops or iterations can be a game-changer.

Remember always to check if the array is empty before using this function to avoid runtime errors, ensuring the smooth execution of your scripts. Armed with this knowledge, you can now apply array.first in various contexts, enhancing your Pine Script coding skills and the performance of your scripts. Happy coding!

Leave a Comment