This article delves into the syntax, usage, and practical applications of array.new_int()
, ensuring that you can incorporate it effectively into your Pine Script projects.
Syntax and Arguments
The syntax for the array.new_int()
function is as follows:
array.new_int(size, initial_value) → array<int>
Let’s break down the components:
- size (
series int
): This optional argument specifies the initial size of the array. If not provided, the default size is 0, creating an empty array. - initial_value (
series int
): Also optional, this argument sets the initial value for all elements within the array. If omitted, the default value is ‘na’, indicating that the elements are not set to any specific integer value.
Example
Consider the following example to illustrate how array.new_int()
can be used in a trading script:
//@version=5 indicator("array.new_int example") length = 5 priceArray = array.new_int(length, int(close)) plot(array.sum(priceArray) / length)

In this example, we use the array.new_int()
function to create an array named priceArray
that holds integer values. Let’s analyze the code line-by-line:
- 1: We specify the version of Pine Script being used, which is version 5.
- 2: The
indicator
function declares a new indicator named “array.new_int example”. - 3: We define a variable
length
and set its value to 5, which will be used as the size of our array. - 4: The
priceArray
is initialized witharray.new_int(length, int(close))
. This creates an array of size 5, where each element is set to the integer part of the current close price. This demonstrates converting a float value (close price) to an integer before storing it in the array. - 5: We use
plot()
to display the average of the values inpriceArray
on the chart. Thearray.sum(priceArray)
computes the sum of the array’s elements, and dividing bylength
gives us the average.
Key Features and Takeaways
- Function Useability and Syntax: The
array.new_int()
function is versatile, allowing for the initialization of integer arrays with specific sizes and initial values. Its syntax is straightforward, requiring up to two optional arguments: size and initial_value. - Application in Trading Scripts: This function is particularly useful for scenarios where you need to store and manipulate sequences of integer data, such as counting occurrences, summing values, or even implementing custom mathematical models.
- Enhanced Data Management: By utilizing arrays, Pine Script developers can write cleaner, more efficient code. Arrays help in managing large datasets, performing batch operations, and simplifying the logic of trading algorithms.