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

Understanding the array.set() Function in Pine Script

Photo of author
Published on

In this tutorial, we delve into the array.set() function in Pine Script, a powerful tool for manipulating array elements by updating their values at specific indexes. We will cover its syntax, usage, and provide a hands-on example to illustrate its application in financial charting scripts.

Syntax of array.set()

The array.set() function is essential for modifying the content of arrays in Pine Script. Its syntax is as follows:

array.set(id, index, value) → void


  • id (any array type): This is the array object you want to modify. Pine Script supports different types of arrays, including float, integer, and boolean arrays, among others.
  • index (series int): The position in the array where the value needs to be updated. Pine Script arrays are zero-indexed, meaning the first element has an index of 0.
  • value (series ): The new value you wish to set at the specified index. The type of this value must match the type of the elements in the array.

Practical Example: Moving Average Calculation

indicator("Custom array.set Example", overlay = true)
pricesArray = array.new_float(10)
for index = 0 to 9
    array.set(pricesArray, index, close[index])
plot(array.sum(pricesArray) / 10, "10-Day Moving Average")


  1. Indicator Declaration: We declare our script as an indicator with the title “Custom array.set Example” and enable overlay to display the moving average on the price chart.
  2. Array Initialization: pricesArray = array.new_float(10) creates a new floating-point array with 10 elements, intended to store the closing prices of the last 10 days.
  3. Filling the Array: The for loop iterates through the first 10 indices (0 to 9). For each iteration, array.set(pricesArray, index, close[index]) updates the element at the current index in pricesArray with the closing price of the corresponding day.
  4. Calculating and Plotting the Moving Average: Finally, we calculate the sum of the array’s elements using array.sum(pricesArray) and divide it by 10 to find the average. This value is plotted on the chart as “10-Day Moving Average.”

Key Features and Takeaways

  • Function Usability: array.set() is invaluable for modifying specific elements within an array, enabling dynamic data manipulation within scripts.
  • Syntax and Application: Understanding the correct syntax is crucial for effectively using this function. Remember to match the value’s type with the array’s element type.
  • Practical Use Case: Our example demonstrates how array.set() can be employed to calculate and plot a simple moving average, showcasing its utility in financial analysis scripts.

By mastering array.set() and other array manipulation functions in Pine Script, you can develop sophisticated scripts for technical analysis and financial charting. This tutorial provides a solid foundation, but experimentation and practice are key to becoming proficient in Pine Script programming.

Leave a Comment