In this tutorial, we will explore the array.range function in Pine Script, which is a powerful built-in function used to calculate the difference between the minimum and maximum values in a given array. The array.range function is an essential tool for a wide range of applications, including data analysis and manipulation, and can be useful for detecting trends or outliers in your data.

**Introduction to array.range**

The array.range function in Pine Script is designed to return the difference between the minimum and maximum values within a given array. This can be useful in various scenarios, such as identifying the range of price fluctuations over a specific period or understanding the spread of data points in your analysis.

**Syntax of array.range**

The syntax for the array.range function is as follows:

array.range(id) → series[float] array.range(id) → series[integer]

**Arguments:**

: An array object, which can be either float or integer.`id (array)`

**Returns:**

- The difference between the min and max values in the array.

**Remarks:**

- The function requires a float or integer array.

**Using array.range in Pine Script**

In this section, we will demonstrate how to use the array.range function in Pine Script by providing two different examples.

**Basic Example**

In this basic example, we will create an array of the closing prices of the last 10 bars and then calculate the range of these prices using the array.range function.

//@version=5 indicator('array.range basic example') a = array.new_float(0) for i = 0 to 9 by 1 array.push(a, close[i]) plot(array.range(a))

**Code Explanation:**

: Specifies that we are using version 5 of Pine Script.`//@version=5`

: Defines the name of the indicator as “array.range basic example”.`indicator('array.range basic example')`

: Initializes a new float array`a = array.new_float(0)`

with an initial size of 0.`a`

: Loops through the last 10 bars using a step of 1.`for i = 0 to 9 by 1`

: Pushes the closing price of each bar (indexed by`array.push(a, close[i])`

) into the array`i`

.`a`

: Calculates the range of closing prices in the array`plot(array.range(a))`

using the`a`

function and plots it on the chart.`array.range`

**Unique Use Case Example**

In this unique use case example, we will calculate the range of the True Range (TR) values for the last 14 bars.

//@version=5 indicator('True Range values') length = 14 trueRange = math.max(math.max(high - low, math.abs(high - close[1])), math.abs(low - close[1])) trArray = array.new_float(length, na) for i = 0 to length - 1 by 1 array.set(trArray, i, trueRange[i]) rangeTR = array.range(trArray) plot(rangeTR, title='True Range Range', color=color.new(color.blue, 0), linewidth=2)

**Code Explanation:**

This Pine Script code calculates the range of the True Range (TR) values for the last 14 bars and plots it on the chart. Here’s a line-by-line explanation of the code:

: Specifies that we are using version 5 of Pine Script.`//@version=5`

: Defines the name of the indicator as “True Range values”.`indicator('True Range values')`

: Sets the length of the array to 14.`length = 14`

: Calculates the True Range (TR) value for each bar by taking the maximum of three values: (a) the difference between the high and low prices, (b) the absolute difference between the high price and the previous close, and (c) the absolute difference between the low price and the previous close.`trueRange = math.max(math.max(high - low, math.abs(high - close[1])), math.abs(low - close[1]))`

: Initializes a new float array`trArray = array.new_float(length, na)`

with a size of 14 and fills it with`trArray`

values.`na`

: Loops through the last 14 bars using a step of 1.`for i = 0 to length - 1 by 1`

: Sets the value of the`array.set(trArray, i, trueRange[i])`

array at index`trArray`

to the corresponding True Range value.`i`

: Calculates the range of the True Range values using the`rangeTR = array.range(trArray)`

function. The range is the difference between the maximum and minimum values in the array.`array.range`

: Plots the range of the True Range values on the chart with a blue line (using the default opacity of 100%) and a linewidth of 2. The`plot(rangeTR, title='True Range Range', color=color.new(color.blue, 0), linewidth=2)`

parameter labels the plotted line as “True Range Range”.`title`

**Key Takeaways**

- The array.range function in Pine Script calculates the difference between the minimum and maximum values in a given array.
- This function can be used for a variety of applications, such as data analysis, detecting trends, or identifying outliers.
- The function requires an array of either float or integer data type.
- Using the array.range function in your Pine Script code can help improve the efficiency and readability of your data analysis.

**Conclusion**

In this tutorial, we discussed the array.range function in Pine Script and demonstrated its use with two different examples. Understanding and utilizing the array.range function can greatly enhance your ability to analyze and manipulate data within Pine Script, ultimately leading to more informed trading decisions.