Skip to main content
A pivot high is a bar whose high is above the highs of the bars around it. You know it only after the next bar closes, so test one bar back, on a closed bar: high[1] > high[2] and high[1] > high is true on the bar after the pivot. On a forming bar high still moves, so the test can pass and later fail; barstate.isconfirmed waits for the close. Keep the last pivot’s bar and price in static variables, and when the next pivot comes draw a line from the old one to it. line.new is a drawing, so it may sit inside an if.
  • The pivot is on bar bar_index - 1, so that is the x of the line’s end. A bar index is what line.new takes by default.
  • On the first bars high[2] is na, the comparison is na, and an if on na does not run.
  • A pivot low is the mirror: low[1] < low[2] and low[1] < low.
  • Each line is a drawing, and a script keeps 50 of each kind unless max_lines_count says more (see Lines, labels, boxes and polylines and Limits).
For a wider test, with several bars on each side, talib.highest and talib.lowest give the extremes of a window. See Referencing previous bars for what high[1] reads.