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)
Line-by-Line Explanation
- 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. - Array Creation: The
array.new_float(5, high)
function creates a new floating-point arrayb
with five elements, each initialized to thehigh
price of the current bar. - Removing an Element: The
array.remove(b, 0)
function removes the element at index 0 from the arrayb
. The removed element’s value is stored in the variableremovedElement
. - Plotting Array Size: The
plot(array.size(b))
function plots the size of the arrayb
after the removal operation, illustrating the decrease in array size. - Plotting Removed Element: The
plot(removedElement)
function plots the value of the element that was removed from the arrayb
, 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.