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

Understanding the array.remove() Function in Pine Script

Photo of author
Published on

This tutorial will delve into the syntax, usage, and practical applications of the array.remove() function, complete with an illustrative example to enhance your understanding.

Syntax and Arguments

The syntax for the array.remove() function is as follows:

array.remove(arr, idx) → series<type>
  • arr (any array type): This argument specifies the array from which you want to remove an element. The array can be of any type (e.g., float, int, bool).
  • idx (series int): This is the index of the element you wish to remove from the array. The index is zero-based, meaning that the first element of the array is at index 0.

Example

Let’s examine a practical example to see array.remove() in action:

//@version=5
indicator("array.remove demonstration")
b = array.new_float(5, high)
removedElement = array.remove(b, 0)
plot(array.size(b))
plot(removedElement)
Example

Line-by-Line Explanation

  1. Indicator Declaration: The script begins with declaring an indicator named “array.remove demonstration” using the indicator function. This is standard for creating a script in Pine Script.
  2. Array Creation: The array.new_float(5, high) function creates a new floating-point array b with five elements, each initialized to the high price of the current bar.
  3. Removing an Element: The array.remove(b, 0) function removes the element at index 0 from the array b. The removed element’s value is stored in the variable removedElement.
  4. Plotting Array Size: The plot(array.size(b)) function plots the size of the array b after the removal operation, illustrating the decrease in array size.
  5. Plotting Removed Element: The plot(removedElement) function plots the value of the element that was removed from the array b, providing visual feedback on the removed element’s value.

Key Features and Takeaways

  • Functionality: array.remove() alters the contents of an array by removing the element at a specified index, effectively decreasing the array’s size by one.
  • Return Value: The function returns the value of the removed element, allowing further manipulation or analysis.
  • Indexing: Remember that Pine Script arrays are zero-indexed, so the first element is at index 0.
  • Application: This function is particularly useful in financial data analysis and trading strategy development, where dynamic data manipulation is required.

Leave a Comment