Highest

Returns two values:

  1. The highest value from a series for a given period
  2. The distance from the current bar to that value.
    0
    if it was found on the current bar,
    1
    if it was found on the previous bar, etc.
Parameter NameTypeDefault ValueNotes
valueSeries<Number>--
periodNumber--

Returns: Series<Number>, Number

Highest is a built-in function - you can use it directly. Below is an example implementation:

1 function Highest(value, period):
2 highest = value
3 distanceToHighest = 0
4
5 for distance from 1 to (period - 1)
6 isNewHigh = value[distance] > highest
7 highest = value[barIndex] if isNewHigh else highest
8 distanceToHighest = distance if isNewHigh else distanceToHighest
9
10 highest, distanceToHighest