In this tutorial, we’ll deep dive into one of Pine Script’s powerful array functions – array.indexof()
. This function is instrumental in searching for the index of a specific value within an array. Pine Script, being a domain-specific language for creating custom technical analysis indicators, offers this function to ease the process of handling arrays during the development of scripts.
How Does array.indexof() Work?
Syntax
The array.indexof()
function is characterized by a straightforward syntax, making it easily accessible for both novice and experienced developers. Below is the general syntax:
array.indexof(id, value) → series int
Parameters
id
(any array type): This is the array object that you want to search within.value
(series ): The specific value you are looking to find within the array.
Returns
The function returns the index of the first occurrence of the specified value, or -1 if the value is not found.
Real-World Use Case Example
Here’s a practical example that demonstrates the usage of the array.indexof()
function in the real world:
//@version=5 indicator("array.indexof example" , overlay = true ) a = array.new_float(1,1) array.push(a , 2) array.push(a , 3) array.push(a , 4) array.push(a , 5) array.push(a , 6) index = array.indexof(a, 4) labell = label.new(x = bar_index , y= high , text = "Index Of 4 in array " + str.tostring(a) + " \n is "+ str.tostring(index) , color = color.white) label.delete(labell[1] )
![](https://pinewizards.com/wp-content/uploads/2023/11/EURUSD_2023-11-08_14-57-22_9ec59-1024x524.png)
Code Explanation
In this script, we’re using array.indexof()
to find specific resistance levels on a chart. Let’s break down the code line by line:
indicator("array.indexof example", overlay = true)
:- This line declares a new indicator with the name “array.indexof example”.
overlay = true
means that the indicator will be drawn over the price chart.
a = array.new_float(1,1)
:- This line initializes a new array named ‘a’ of type float with an initial size of 1 and a default value of 1 for all elements.
array.push(a, 2)
toarray.push(a, 6)
:- These lines add the values 2, 3, 4, 5, and 6 to the end of the array ‘a’.
index = array.indexof(a, 4)
:- This line searches for the value 4 in the array ‘a’ and assigns the index of the first occurrence to the variable ‘index’.
labell = label.new(x = bar_index, y = high, text = "Index Of 4 in array " + str.tostring(a) + " \n is " + str.tostring(index), color = color.white)
:- This line creates a new label on the chart with the following properties:
x = bar_index
: The label’s x-coordinate is set to the current bar index.y = high
: The label’s y-coordinate is set to the current bar’s high price.text
: The text for the label. It includes the text “Index Of 4 in array “, the content of the array ‘a’ converted to a string, and the index of the number 4 in the array, also converted to a string. The\n
is a newline character, meaning the text will appear on two lines.color = color.white
: The color of the label’s text is set to white.
- This line creates a new label on the chart with the following properties:
label.delete(labell[1])
:- This line deletes the previous label created by the script (if any) to avoid cluttering the chart. The indexing
[1]
refers to the historical label, where the current label is indexed as[0]
.
- This line deletes the previous label created by the script (if any) to avoid cluttering the chart. The indexing
The overall functionality of this script is to demonstrate how to use arrays and specifically the array.indexof
function in Pine Script. It will display a label on the chart indicating the index position of the number 4 in the array ‘a’, which would be ‘3’ because arrays are zero-indexed (i.e., the count starts from 0).
Key Takeaway
The array.indexof()
function is an invaluable tool for developers working with arrays in Pine Script. It allows for efficient and rapid searching within arrays, enhancing the functionality and flexibility of custom indicators and strategies. Understanding its syntax and application is crucial for handling arrays and manipulating data effectively in Pine Script.
Conclusion
As we wrap up this tutorial, it’s essential to reiterate the significance of array.indexof()
in data handling and manipulation within Pine Script. Its simplicity and efficiency make it a go-to function for many developers aiming to enhance their scripts’ functionality.
Remember, the function returns the index of the first occurrence of the value, or -1 if the value isn’t found. Always ensure that your arrays and the values you are searching for are defined and initialized appropriately to avoid unexpected results or errors.
Happy coding, and may your custom indicators and strategies be as efficient and effective as possible!