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

Understanding the array.reverse() Function in Pine Script

Photo of author
Published on

Let’s dive deeper into the syntax, usage, and practical applications of array.reverse().

Syntax

array.reverse(id) → void

Arguments:

  • id (any array type): This is the array object you wish to reverse. It can be of any array type supported by Pine Script.

Example: Reversing an Array of Closing Prices

To illustrate the use of array.reverse(), let’s consider an example where we reverse an array containing the last ten closing prices of a trading instrument.

//@version=5
indicator("array.reverse example", overlay = true)
priceArray = array.new_float(0)  // Initialize a new float array
for i = 0 to 9
    array.push(priceArray, close[i])  // Fill the array with the last 10 closing prices
plot(array.get(priceArray, 0))  // Plot the first element of the array (most recent closing price)

array.reverse(priceArray)  // Reverse the array
plot(array.get(priceArray, 0), color=color.red)  // Plot the new first element, which is the oldest closing price
Example

Walkthrough:

  • Script. indicator("array.reverse example", overlay = true) defines a new indicator named “array.reverse example” that will overlay the main price chart.
  • Array Initialization: priceArray = array.new_float(0) initializes a new floating-point array named priceArray with an initial size of 0. This array will be used to store closing prices.
  • Populating the Array:
    • The for-loop for i = 0 to 9 iterates ten times, corresponding to the last ten bars on the chart.
    • Within the loop, array.push(priceArray, close[i]) pushes the closing price of the current bar (indexed by i) into priceArray. The closing prices are stored from the most recent (close[0]) to the oldest (close[9]).
  • Initial Plot: plot(array.get(priceArray, 0)) plots the first element of priceArray on the chart, which is the most recent closing price before the array is reversed.
  • Reversing the Array: array.reverse(priceArray) reverses the order of elements in priceArray. After this operation, the first element of the array becomes what was initially the last, making the oldest closing price now the first element.
  • Plot After Reversal: plot(array.get(priceArray, 0), color=color.red) plots the new first element of priceArray, which, after reversal, is the oldest closing price in the collected range. This plot is distinguished by its red color.

Key Features

  • Function Useability: The array.reverse() function is applicable to arrays of any type, offering flexibility in manipulating data structures for various analytical needs.
  • Syntax and Application: With a simple syntax, it integrates seamlessly into scripts, enhancing data analysis and visualization capabilities without complex coding requirements.

Takeaways

  • The array.reverse() function is a simple yet powerful tool for inverting the order of elements in an array.
  • It finds practical applications in data analysis, allowing traders to easily manipulate and analyze datasets.
  • By understanding and utilizing this function, traders can enhance their trading strategies and analytical models in Pine Script.

This function exemplifies the versatility and efficiency of Pine Script in developing and optimizing trading algorithms, providing traders with the tools needed to analyze and act on financial data effectively.

Leave a Comment