Skip to content
Algo Trade Analytics Docs

array.binary_search_rightmost() - Pine Script Function

The function returns the index of the value if it is found. When the value is not found, the function returns the index of the element to the right of where the value would lie if it was in the array. The array must be sorted in ascending order.

array.binary_search_rightmost(id, val) → series int
NameTypeDescription
idarray<int/float>An array object.
  • A binary search works on sorted arrays in ascending order. It begins by comparing an element in the middle of the array with the target value. If the element matches the target value, its position in the array is returned. If the element’s value is greater than the target value, the search continues in the lower half of the array. If the element’s value is less than the target value, the search continues in the upper half of the array. By doing this recursively, the algorithm progressively eliminates smaller and smaller portions of the array in which the target value cannot lie.
//@version=6
indicator("array.binary_search_rightmost, repetitive elements")
a = array.from(4, 5, 5, 5)// Returns the index of the last instance.position = array.binary_search_rightmost(a, 5)
plot(position) // Plots 3