Home » Array Functions » array.slice() Function in pinescript

array.slice() Function in pinescript

Photo of author
Published on

In this blog post, we will explore the array.slice() function in Pine Script. The array.slice() function creates a slice or a shallow copy from an existing array.

Defining array.slice() Function


The syntax for the array.slice() function in Pine Script is as follows:

array.slice(id, index_from, index_to) → array<type>


The array.slice() function takes three parameters:

  • id: This refers to an array object.
  • index_from: This is a zero-based index at which to begin extraction.
  • index_to: This is a zero-based index before which to end extraction. The function extracts up to but not including the element with this index.

Key Characteristics of array.slice()

Before we jump into the example, there are a couple of essential characteristics of the array.slice() function that we need to note:

  • When an object from the slice changes, the changes are applied to both the new and the original arrays. This is because array.slice() creates a “shallow copy”, not a “deep copy”.
  • The array.slice() function returns a new array and does not modify the original array’s size.

Working Example

Let’s explore an example to better understand the array.slice() function.

indicator("array.slice example")
a = array.new_float(0)
for i = 0 to 9
    array.push(a, close[i])
// take elements from 0 to 4
// *note that changes in slice also modify original array
slice = array.slice(a, 0, 5)
plot(array.sum(a) / 10)
plot(array.sum(slice) / 5)
array.slice() Function

Code Explanation

In the script above:

  1. We start by declaring the indicator function with the title “array.slice example”.
  2. We then create a new empty floating-point array named a.
  3. A loop runs from 0 to 9, and in each iteration, it pushes the closing price of the i-th bar into the array a.
  4. The array.slice() function is used to create a new array named slice that consists of the first 5 elements of a (from index 0 to index 4).
  5. We then plot two lines on our chart: the average of all values in a and the average of all values in slice.

Key Takeaways

  • The array.slice() function in Pine Script allows you to create a shallow copy of a portion of an existing array. This can be especially useful when you need to perform operations on a subset of your data without affecting the rest.
  • Remember, changes to the slice array also modify the original array. This is an important feature to keep in mind when using this function to avoid unexpected results.
  • The array.slice() function does not change the original array’s size but returns a new array.


In conclusion, the array.slice() function is a powerful tool in Pine Script for manipulating arrays. It provides flexibility in handling data, whether you’re working with market data or custom datasets. Understanding how to properly use this function can greatly enhance your ability to create more sophisticated and effective trading scripts. Always remember the key takeaways while using this function to get the most out of it.

Leave a Comment