In this tutorial, we’ll explore the array.min
function in Pine Script, a powerful built-in function that allows you to find the minimum value in an array. We’ll cover its basic usage, dive into some practical examples, and learn how to optimize your trading strategies using this handy function.
Understanding the array.min
Function
The array.min
function is a built-in function in Pine Script that returns the minimum value within an array. This function is useful when you need to find the lowest value among a set of elements, such as the lowest price in a specified range or the lowest value of an indicator.
Syntax
The syntax for the array.min
function is as follows:
array.min(array_name)
Parameters:
array_name
: The name of the array for which you want to find the minimum value.
Return Value
The function returns a float, which is the minimum value in the specified array.
How to Use the array.min
Function
To use the array.min
function, you need to follow these steps:
- Declare an array using the
array.new_*()
function, where the * represents the type of the array (e.g.,array.new_float()
for a float array). - Fill the array with the desired values.
- Use the
array.min()
function to find the minimum value in the array.
Let’s look at two unique use case examples to better understand how to use the array.min
function in Pine Script.
Example Use Case 1: Finding the Lowest Price in a Specific Range
In this example, we’ll use the array.min
function to find the lowest price within a specified range.
//@version=5 indicator('Lowest Price in Range', shorttitle='LPR', overlay=true) length = input.int(10, title='Range Length', minval=1) // Initialize the array prices_array = array.new_float(0) // Fill the array with the lowest prices of the last 'length' bars for i = 0 to length - 1 by 1 array.unshift(prices_array, low[i]) // Find the lowest price min_price = array.min(prices_array) // Plot the lowest price l = line.new(x1=bar_index[1], y1=min_price, x2=bar_index, y2=min_price, color=color.red, width=5) line.delete(l[1])
Code Explanation:
- We start by defining the script properties with the
study()
function. - We create an input for the user to define the range length.
- We initialize an empty float array named
prices_array
. - We use a
for
loop to fill theprices_array
with the lowest prices of the lastlength
bars. - We find the minimum price using the
array.min()
function. - We plot the lowest price as a horizontal line on the chart.
Key Takeaways
- The
array.min
function is a built-in function in Pine Script that helps you find the minimum value in an array. - This function can be used in various ways, such as finding the lowest price in a range or identifying local minimums for entry signals.
- To use the
array.min
function, you need to declare an array, fill it with values, and then call the function.
Conclusion
In this tutorial, we covered the array.min
function in Pine Script, its syntax, and how to use it effectively. We also explored two practical use case examples that demonstrated its potential in creating trading strategies. By mastering the array.min
function, you can optimize your trading strategies and make better-informed decisions based on the minimum values of specific ranges or indicators.