Finds the first position holding a value and returns it, or -1 when the value is not in the array.
The search runs from index 0 upward and stops at the first match. Compare the result with -1 to know whether the value was found; 0 is a valid position.
Values are compared exactly, so a float must match to the last digit.
Syntax
array.indexof(id: any array type, value: series <type of the array's elements>) -> series int
array.indexof(value: series <type of the array's elements>) -> series intArguments
| Name | Type | Default | Description |
|---|---|---|---|
| id | any array type | required | The array to search from the front for the value. |
| value | series <type of the array's elements> | required | The item to look for. It has to match the type of the array's items. |
Returns
series int: The index of the first match, or -1.
Remarks
- On Sidechair: searching for na matches the first na item.
- To search from the end, use
array.lastindexof(). To only test for presence, usearray.includes().
Example
//@version=6
indicator("array.indexof: peak position", overlay = false)
var win = array.new_float()
array.push(win, close)
if array.size(win) > 12
array.shift(win)
plot(array.indexof(win, array.max(win)), "Peak position (0 = oldest)")See also
Runs on Sidechair