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

Understanding the array.copy Function in Pine Script

Photo of author
Published on

One of the functions that stand out is array.copy. This function is indispensable when you need to create a copy of an existing array, preserving the original array’s state while enabling manipulations on the copied version. In this tutorial, we will delve into the intricacies of the array.copy function, exploring its syntax, application, and a unique use case to cement your understanding.

Syntax

The syntax for the array.copy function is straightforward. It is expressed as:

array.copy(id) → array<type>

Here, id refers to the array object that you intend to copy. The function then returns a new array that is a copy of the specified array.

Arguments

The array.copy function takes a single argument:

  • id (any array type): This is the array object that you want to copy.

Returns

The function returns a copy of the specified array. It’s essential to note that modifications to the copied array do not affect the original array, and vice versa.

Example Use Case

To put this into perspective, let’s consider a practical example:

//@version=5
indicator("array.copy Example", overlay = true)
length = 14
a = array.new_float(length, close)
b = array.copy(a)
a := array.new_float(length, open)
plot(array.sum(a) / length, title="Average Open", color=color.red)
plot(array.sum(b) / length, title="Average Close", color=color.blue)
Understanding the array.copy Function in Pine Script

In this example, we are creating a moving average indicator that plots the average of open and close prices separately, utilizing the array.copy function.

Line-by-Line Explanation

  1. //@version=5: Specifies the version of Pine Script being used.
  2. indicator("array.copy Example", overlay = true): Creates an indicator with the title “array.copy Example” that is overlayed on the price chart.
  3. length = 14: Sets the length of the moving average to 14 periods.
  4. a = array.new_float(length, close): Initializes a float array a with the length of 14 and fills it with closing prices.
  5. b = array.copy(a): Creates a copy of array a and assigns it to b. Now, both a and b contain closing prices.
  6. a := array.new_float(length, open): Re-initializes array a to contain open prices, but array b still contains the original closing prices.
  7. plot(array.sum(a) / length, title="Average Open", color=color.red): Calculates the average of open prices from array a and plots it in red.
  8. plot(array.sum(b) / length, title="Average Close", color=color.blue): Calculates the average of close prices from array b and plots it in blue.

In this example, the array.copy function allows us to have two separate arrays, one for open prices and another for close prices, derived from the original array of closing prices.

Key Takeaway

The array.copy function is instrumental in creating distinct copies of arrays that can be manipulated independently. This is pivotal in scenarios where you need to maintain the integrity of the original array while performing various operations on its copy. It ensures data consistency and reliability, especially in complex scripting and data analysis scenarios in Pine Script.

Conclusion

Mastering the use of array.copy enriches your Pine Script programming toolkit, granting you the flexibility to manipulate and analyze data without affecting the original datasets. This is especially crucial in financial analysis and trading strategy development, where data integrity and accuracy are paramount. By grasping this concept, you open doors to advanced data handling and manipulation techniques, paving the way for more sophisticated and accurate trading algorithms and indicators.

Leave a Comment