All indicators
Patterns & signals / Price pane

Pivot Trendlines

Connects consecutive confirmed swing highs and swing lows, extends the latest high and low line to the right, and boxes the range between the latest swing high and low.

  • Trendlines
  • Updates at bar close
  • pivot
  • swings
  • trendline
  • lines
  • box
  • support
  • resistance

By Spectral examples · v1.0.0

MIT-0: free to use, change and share, no attribution needed. What you build from it is yours.

Pivot Trendlines on a Spectral chart
Spectral chart with sample market data, default settings.

Overview

A swing high is a high above the highs of the bars on each side (a swing low the mirror), confirmed once those bars closed. Each swing is joined to the previous swing on its side.

How to read it

Rising lines (green) show higher highs or higher lows, falling lines (red) lower ones. The two latest lines (amber) continue to the right edge: a close beyond the latest high line breaks a falling resistance. The violet box spans the latest swing high and low; a close outside it leaves the range.

Settings and limitations

Pivot strength sets how many bars each side confirm a swing; Lines shown limits the older swing lines.

Notes

A swing is only known once the bars to its right closed (live: none), so lines appear that many bars late.

Settings

Each setting appears in the indicator's settings panel in Spectral, next to a color setting for every output.

SettingTypeDefaultRange or optionsWhat it does
Pivot strengthWhole number51 – 30Bars on each side a swing high must stand above (a swing low below) to count.
Lines shownWhole number400 – 500Most recent swing-to-swing lines drawn per side.

What it draws

Line segments

  • Swing linesdotted · 1 px · colors from Slope (2 colors)#8B949E #3FB950 #F85149
  • Latest trendlinessolid · 1.5 px · extends right#D29922

Boxes

  • Swing rangefilled at 8% opacity · extends right#A371F7

Source

Save it as pivot-trendlines.js and add it with Indicators → Custom indicators → Add file…. The format is explained in the custom indicators guide.

// Copyright (c) 2026 Spectral
// SPDX-License-Identifier: MIT-0
// Free to use, copy, modify and share: https://spectral.trade/docs/custom-indicators/LICENSE.txt
/** @type {import('./spectral-indicator').IndicatorV3} */
export default {
  schemaVersion: 3,
  id: 'pivot-trendlines',
  name: 'Pivot Trendlines',
  shortName: 'Trendlines',
  version: '1.0.0',
  author: 'Spectral examples',
  description: 'Connects consecutive confirmed swing highs and swing lows, extends the latest high and low line to the right, and boxes the range between the latest swing high and low.',
  pane: 'price',
  live: 'none',
  inputs: {
    strength: { type: 'integer', label: 'Pivot strength', default: 5, min: 1, max: 30, description: 'Bars on each side a swing high must stand above (a swing low below) to count.' },
    history: { type: 'integer', label: 'Lines shown', default: 40, min: 0, max: 500, description: 'Most recent swing-to-swing lines drawn per side.' },
  },
  palettes: {
    slope: { label: 'Slope', colors: ['#3FB950', '#F85149'] },
  },
  segments: {
    swings: { label: 'Swing lines', color: '#8B949E', palette: 'slope', lineWidth: 1, lineStyle: 'dotted' },
    latest: { label: 'Latest trendlines', color: '#D29922', lineWidth: 1.5, extend: 'right' },
  },
  boxes: {
    range: { label: 'Swing range', color: '#A371F7', opacity: 0.08, extend: 'right', textSize: 'small', hideBelowBarWidth: 2 },
  },
  docs: {
    overview: 'A swing high is a high above the highs of the bars on each side (a swing low the mirror), confirmed once those bars closed. Each swing is joined to the previous swing on its side.',
    howToRead: 'Rising lines (green) show higher highs or higher lows, falling lines (red) lower ones. The two latest lines (amber) continue to the right edge: a close beyond the latest high line breaks a falling resistance. The violet box spans the latest swing high and low; a close outside it leaves the range.',
    settings: 'Pivot strength sets how many bars each side confirm a swing; Lines shown limits the older swing lines.',
    notes: 'A swing is only known once the bars to its right closed (live: none), so lines appear that many bars late.',
  },
  calculate(ctx) {
    const { bars, inputs } = ctx;
    const n = inputs.strength;
    const highs = [];
    const lows = [];
    for (let index = n; index < bars.length - n; index += 1) {
      let isHigh = true;
      let isLow = true;
      for (let offset = 1; offset <= n && (isHigh || isLow); offset += 1) {
        if (bars[index - offset].high >= bars[index].high || bars[index + offset].high > bars[index].high) isHigh = false;
        if (bars[index - offset].low <= bars[index].low || bars[index + offset].low < bars[index].low) isLow = false;
      }
      if (isHigh) highs.push({ index, value: bars[index].high });
      if (isLow) lows.push({ index, value: bars[index].low });
    }
    const lines = (pivots) => pivots.slice(1).map((pivot, position) => {
      const previous = pivots[position];
      return { x1: previous.index, y1: previous.value, x2: pivot.index, y2: pivot.value, palette: pivot.value >= previous.value ? 0 : 1 };
    });
    const highLines = lines(highs);
    const lowLines = lines(lows);
    const high = highs[highs.length - 1];
    const low = lows[lows.length - 1];
    const range = high && low ? [{
      x1: Math.min(high.index, low.index), y1: low.value, x2: Math.max(high.index, low.index), y2: high.value,
      text: `range ${ctx.format.price(high.value - low.value)}`,
    }] : [];
    return {
      boxes: { range },
      segments: {
        swings: [...highLines.slice(-inputs.history - 1, -1), ...lowLines.slice(-inputs.history - 1, -1)],
        latest: [...highLines.slice(-1), ...lowLines.slice(-1)].map(({ palette, ...line }) => line),
      },
    };
  },
};

Adapt it with an LLM

Paste this into your assistant and replace the second-to-last line with the change you want.

Start from this Spectral custom indicator: https://spectral.trade/indicators/pivot-trendlines.js
Format reference: https://spectral.trade/docs/custom-indicators.llm.md
Keep the file format and every rule of the reference; give the result a new id, name, shortName and version, and update docs and input descriptions to match.
Keep its live updates exact and fast (section "Fast live updates" of the reference): carry values from bar to bar only through ctx.ta, and raise live.lookback if the change reads further back; Spectral rejects a file whose live updates differ from a full recalculation or are too slow.
Change I want: <describe the change>
Answer with the complete file in one code block, ready to save as <id>.js.