Home » Array Functions » array.get function in Pine Script

array.get function in Pine Script

Photo of author
Published on

In this tutorial, we will explore the array.get function in Pine Script, an essential tool for retrieving elements from arrays. The array.get function allows you to extract elements from an array, which can be particularly helpful when working with time series data in trading algorithms. This tutorial will cover the syntax and usage of the array.get function and provide two unique use cases that demonstrate its capabilities. We will be using Pine Script v4 for all examples.

The Array.get Function: Syntax and Usage

The array.get function is used to access an element from an array by specifying the index of the element. Here’s the syntax for the array.get function:

array.get(array_id, index)
  • array_id: This parameter represents the identifier of the array from which you want to extract the element.
  • index: This parameter denotes the index of the element you wish to access. Indexing starts at 0, so the first element is at index 0, the second at index 1, and so on.

It’s important to note that if the specified index is out of the range of the array, the array.get function will return na, the Pine Script representation for “not available” or “not applicable.”

Example

//@version=5
indicator('Array.get Example 1', shorttitle='EG1')

// Create an array and populate it with sample data
arr = array.new_float(5)
array.set(arr, 0, close)
array.set(arr, 1, close[1])
array.set(arr, 2, close[2])
array.set(arr, 3, close[3])
array.set(arr, 4, close[4])

// Retrieve an element using array.get
element = array.get(arr, 2)  // Retrieves the element at index 2 (30)
plot(element)

In this example, we create an array to store the closing prices of the current bar and the previous four bars. We then use the array.get function to access the closing price from two bars ago and plot it on the chart. Here’s a breakdown of each line:

  1. //@version=5: This line sets the version of Pine Script used for the script. In this case, we use version 5.
  2. indicator('Array.get Example 1', shorttitle='EG1'): This line declares a custom indicator and sets the title to “Array.get Example 1” and the short title to “EG1”.
  3. arr = array.new_float(5): This line creates a new floating-point array named arr with a size of 5.
  4. array.set(arr, 0, close): This line sets the value at index 0 of the arr array to the closing price of the current bar.
  5. array.set(arr, 1, close[1]): This line sets the value at index 1 of the arr array to the closing price of the previous bar.
  6. array.set(arr, 2, close[2]): This line sets the value at index 2 of the arr array to the closing price of the bar two periods ago.
  7. array.set(arr, 3, close[3]): This line sets the value at index 3 of the arr array to the closing price of the bar three periods ago.
  8. array.set(arr, 4, close[4]): This line sets the value at index 4 of the arr array to the closing price of the bar four periods ago.
  9. element = array.get(arr, 2): This line uses the array.get function to retrieve the value at index 2 of the arr array (the closing price from two bars ago) and assigns it to the variable element.
  10. plot(element): This line plots the value of element on the chart, which is the closing price from two bars ago.

In summary, this example demonstrates how to create an array, populate it with historical closing prices, and use the array.get function to access a specific value within the array (in this case, the closing price from two bars ago). The value is then plotted on the chart.

Key Takeaways

  1. The array.get function is used to retrieve elements from an array based on the specified index.
  2. The syntax for the array.get function is array.get(array_id, index).
  3. If an index is out of the range of the array, the function will return na.
  4. The array.get function can be used in various scenarios, such as accessing basic arrays or working with historical price data.

Conclusion

The array.get function is an essential tool for working with arrays in Pine Script. This tutorial provided an overview of the function’s syntax and usage, along with two unique examples that demonstrated its application in different scenarios.

By understanding how to use the array.get function, you can enhance your Pine Script code and create more advanced trading algorithms. Remember to handle out-of-range indexes appropriately and always ensure that the array being accessed is properly initialized and populated.

As you continue to explore Pine Script, consider experimenting with other array functions to further your understanding of arrays and their capabilities in trading algorithms.

Leave a Comment