> ## Documentation Index
> Fetch the complete documentation index at: https://gocharting.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# How to draw a line between two pivots

> Finding swing highs with a comparison on neighbouring bars, remembering the last one in static variables, and drawing a line to it with line.new.

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`.

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Line between swing highs", overlay=true)
static int lastIndex = -1
static float lastHigh = 0.0
isPivot = high[1] > high[2] and high[1] > high
if barstate.isconfirmed and isPivot {
    if lastIndex >= 0 {
        line.new(lastIndex, lastHigh, bar_index - 1, high[1], color=color.red)
    }
    lastIndex := bar_index - 1
    lastHigh := high[1]
}
```

* 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](https://gocharting.com/docs/scripting/chart-output/drawings.md) and [Limits](https://gocharting.com/docs/scripting/distribution/limits.md)).

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](https://gocharting.com/docs/scripting/evaluation/referencing-past-bars.md) for what `high[1]` reads.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.