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:
id (array)
: An array object, which can be either float or integer.
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:
//@version=5
: Specifies that we are using version 5 of Pine Script.indicator('array.range basic example')
: Defines the name of the indicator as “array.range basic example”.a = array.new_float(0)
: Initializes a new float arraya
with an initial size of 0.for i = 0 to 9 by 1
: Loops through the last 10 bars using a step of 1.array.push(a, close[i])
: Pushes the closing price of each bar (indexed byi
) into the arraya
.plot(array.range(a))
: Calculates the range of closing prices in the arraya
using thearray.range
function and plots it on the chart.
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:
//@version=5
: Specifies that we are using version 5 of Pine Script.indicator('True Range values')
: Defines the name of the indicator as “True Range values”.length = 14
: Sets the length of the array to 14.trueRange = math.max(math.max(high - low, math.abs(high - close[1])), math.abs(low - close[1]))
: 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.trArray = array.new_float(length, na)
: Initializes a new float arraytrArray
with a size of 14 and fills it withna
values.for i = 0 to length - 1 by 1
: Loops through the last 14 bars using a step of 1.array.set(trArray, i, trueRange[i])
: Sets the value of thetrArray
array at indexi
to the corresponding True Range value.rangeTR = array.range(trArray)
: Calculates the range of the True Range values using thearray.range
function. The range is the difference between the maximum and minimum values in the array.plot(rangeTR, title='True Range Range', color=color.new(color.blue, 0), linewidth=2)
: 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. Thetitle
parameter labels the plotted line as “True Range Range”.
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.