Home » Array Functions » array.insert Funtion in Pinescript

array.insert Funtion in Pinescript

Photo of author
Published on


In this tutorial, we will explore the array.insert function in Pine Script, which is a versatile and powerful tool for inserting elements into arrays. We will go through a detailed explanation of the function and its usage, and then dive into two unique use-case examples to help you grasp the concept better.

Understanding the Array.insert Function

What is an Array?

An array is a data structure that can store a fixed-size sequential collection of elements of the same type. In Pine Script, arrays are used to store and manipulate data efficiently during the execution of your trading strategies or indicators.

The Array.insert Function

The array.insert function allows you to insert an element into an array at a specific index. The syntax for this function is:

array.insert(id, index, value)


  • id: This is the identifier of the array in which you want to insert the element.
  • index: This is the index at which the element will be inserted.
  • value: This is the element that you want to insert into the array.

Usage of Array.insert Function

Creating an Array

Before using the array.insert function, you need to create an array using the array.new_<type> function. The <type> can be replaced with the data type you want to store in the array (e.g., float, int, bool, or line). Here’s an example of creating an array of integers:

study("My Script", overlay=true)
intArray = array.new_int(0)

Inserting Elements into the Array

Now that we have created an array, let’s insert some elements into it using the array.insert function:

array.insert(intArray, 0, 1)
array.insert(intArray, 1, 2)
array.insert(intArray, 2, 3)

In this example, we have inserted the integers 1, 2, and 3 into the intArray at indices 0, 1, and 2, respectively.

Use-Case Examples

Example 1: Calculating a Moving Average with Array.insert

In this example, we will use the array.insert function to calculate the moving average of the close prices of a stock.

indicator('Moving Average with Array.insert', overlay=true)
length = input.int(14, minval=1, title='Length')
source = close

pricesArray = array.new_float(0)
sum = 0.0
for i = 0 to math.min(length - 1, bar_index) by 1
    array.insert(pricesArray, i, source[i])
    sum += source[i]

movingAverage = sum / length
plot(movingAverage, color=color.new(color.blue, 0), linewidth=2)
array.insert Funtion

Here’s a line-by-line explanation of the code:

  1. The script version and title are defined.
  2. The length of the moving average is set as an input, with a default value of 14.
  3. The close price of the stock is assigned to the source variable.
  4. A new float array called pricesArray is created to store the close prices.
  5. A variable named sum is initialized with the value of 0.0.
  6. A for loop iterates from 0 to the minimum of the length - 1 or bar_index.
  7. The array.insert function is used to insert the close price at index i into the pricesArray.
  8. The sum is updated by adding the close price at index i.
  9. The moving average is calculated by dividing the sum by the length.
  10. The plot function is used to display the moving average on the chart, with the color set to blue and the linewidth set to 2.

Key Takeaways

  1. The array.insert function is used in Pine Script to insert an element into an array at a specific index.
  2. The syntax for the function is array.insert(id, index, value), where id is the array identifier, index is the position at which the element will be inserted, and value is the element to be inserted.
  3. Before using the array.insert function, an array must be created using the array.new_<type> function, where <type> can be float, int, bool, or line.
  4. The function can be used in various scenarios, such as calculating a moving average or tracking the highest highs and lowest lows within a specified lookback period.
  5. Proper understanding of the array.insert function allows for more efficient data manipulation and analysis in Pine Script, making it an essential tool for developing effective trading strategies and indicators.


In this tutorial, we covered the array.insert function in Pine Script, its syntax, and its usage. We also examined two unique use-case examples that demonstrated how the function can be applied to create useful trading indicators, such as a moving average and tracking the highest highs and lowest lows.

With this knowledge, you can now effectively utilize the array.insert function in your own Pine Script strategies and indicators to manipulate and analyze data in arrays.

Leave a Comment