All indicators
Trend / Price pane

Supertrend Colored

Supertrend as one line coloured by the trend, the candles coloured to match, and a triangle on every bar where the trend flips.

  • ST Colored
  • Updates every tick
  • supertrend
  • atr
  • trailing stop
  • trend
  • bar color
  • flip

By Spectral · v1.0.0

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

Supertrend Colored on a Spectral chart
Spectral chart with sample market data, default settings.

Overview

Supertrend trails price with a line placed a multiple of the Average True Range (ATR) away from the middle of each bar, the average of its high and low. In an uptrend the line sits below price and only rises; in a downtrend it sits above price and only falls. When a close crosses the line, the trend flips and the line moves to the other side of price.

This version draws one line that is green in an uptrend and red in a downtrend, colours the candles the same way and marks every flip with a triangle.

How to read it

Green line under price and green candles: the trend is up and the line is a trailing stop for longs. Red line above price and red candles: the trend is down and the line is a trailing stop for shorts. A triangle under a bar marks a flip up, one over a bar a flip down. A flip happens on the close, so on the forming bar the colours can change and change back until the bar closes.

Supertrend works well in trending markets and gives late, costly flips in sideways markets; a wider factor or a longer ATR filters some of them.

Settings and limitations

ATR length (10) and Factor (3) are the common defaults; a smaller factor trails tighter and flips more often. Colour candles and Flip triangles switch those parts; the two trend colours are appearance settings. The line appears after ATR length bars; it starts in a down trend until the first close above it.

Notes

Formula (TradingView Supertrend reference; Olivier Seban): basic bands = (H + L) / 2 +- Factor x ATR, ATR with Wilder smoothing. The final upper band takes the basic upper band when that is lower than the previous final upper band or the previous close was above it, else it keeps the previous one; the final lower band is the mirror. After an up trend the trend turns down when the close is below the final lower band; after a down trend it turns up when the close is above the final upper band. Before the ATR has a value the trend is down. The line is the lower band in an up trend and the upper band in a down trend. On a flip bar the line breaks (no value) so it does not join the two sides with a vertical stroke; the flip triangle marks that bar. Same trend as the built-in Supertrend.

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
ATR lengthWhole number101 – 500Bars in the Average True Range (Wilder smoothing) that sets the distance of the line from price.
FactorNumber30.1 – 20, step 0.1Multiple of the ATR between the middle of the bar and the line. A larger factor flips less often and trails further away.
Colour candlesOn / offOn—Colour the chart's candles by the trend.
Flip trianglesOn / offOn—Mark each bar where the trend flips: a triangle under the bar when it turns up, over the bar when it turns down.

What it draws

Lines

  • Supertrendsolid · 1.5 px · primary · colored per bar from Trend (2 colors)#3FB950 #F85149

Markers

  • Turns uptriangle up, small · below the bar#3FB950
  • Turns downtriangle down, small · above the bar#F85149

Candle colorings

  • Trend candlescandles colored per bar from Trend (2 colors)#3FB950 #F85149

Source

Save it as supertrend-colored.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
/** Spectral draws at most 20,000 markers per key: flips go on the most recent ones. */
const MAX_FLIPS = 20000;

/** @type {import('./spectral-indicator').IndicatorV3} */
export default {
  schemaVersion: 3,
  id: 'supertrend-colored',
  name: 'Supertrend Colored',
  shortName: 'ST Colored',
  version: '1.0.0',
  author: 'Spectral',
  description: 'Supertrend as one line coloured by the trend, the candles coloured to match, and a triangle on every bar where the trend flips.',
  pane: 'price',
  live: { lookback: 1 },
  inputs: {
    atrLength: { type: 'integer', label: 'ATR length', default: 10, min: 1, max: 500, description: 'Bars in the Average True Range (Wilder smoothing) that sets the distance of the line from price.' },
    factor: { type: 'number', label: 'Factor', default: 3, min: 0.1, max: 20, step: 0.1, description: 'Multiple of the ATR between the middle of the bar and the line. A larger factor flips less often and trails further away.' },
    colorBars: { type: 'boolean', label: 'Colour candles', default: true, description: 'Colour the chart\'s candles by the trend.' },
    showFlips: { type: 'boolean', label: 'Flip triangles', default: true, description: 'Mark each bar where the trend flips: a triangle under the bar when it turns up, over the bar when it turns down.' },
  },
  palettes: {
    trend: { label: 'Trend', colors: ['#3FB950', '#F85149'] },
  },
  plots: {
    supertrend: { label: 'Supertrend', color: '#3FB950', lineWidth: 1.5, primary: true, gaps: 'break', palette: 'trend' },
  },
  barColors: {
    candles: { label: 'Trend candles', palette: 'trend' },
  },
  markers: {
    flipUp: { label: 'Turns up', color: '#3FB950', shape: 'triangleUp', size: 'small', placement: 'belowBar' },
    flipDown: { label: 'Turns down', color: '#F85149', shape: 'triangleDown', size: 'small', placement: 'aboveBar' },
  },
  docs: {
    overview: 'Supertrend trails price with a line placed a multiple of the Average True Range (ATR) away from the middle of each bar, the average of its high and low. In an uptrend the line sits below price and only rises; in a downtrend it sits above price and only falls. When a close crosses the line, the trend flips and the line moves to the other side of price.\n\nThis version draws one line that is green in an uptrend and red in a downtrend, colours the candles the same way and marks every flip with a triangle.',
    howToRead: 'Green line under price and green candles: the trend is up and the line is a trailing stop for longs. Red line above price and red candles: the trend is down and the line is a trailing stop for shorts. A triangle under a bar marks a flip up, one over a bar a flip down. A flip happens on the close, so on the forming bar the colours can change and change back until the bar closes.\n\nSupertrend works well in trending markets and gives late, costly flips in sideways markets; a wider factor or a longer ATR filters some of them.',
    settings: 'ATR length (10) and Factor (3) are the common defaults; a smaller factor trails tighter and flips more often. Colour candles and Flip triangles switch those parts; the two trend colours are appearance settings. The line appears after ATR length bars; it starts in a down trend until the first close above it.',
    notes: 'Formula (TradingView Supertrend reference; Olivier Seban): basic bands = (H + L) / 2 +- Factor x ATR, ATR with Wilder smoothing. The final upper band takes the basic upper band when that is lower than the previous final upper band or the previous close was above it, else it keeps the previous one; the final lower band is the mirror. After an up trend the trend turns down when the close is below the final lower band; after a down trend it turns up when the close is above the final upper band. Before the ATR has a value the trend is down. The line is the lower band in an up trend and the upper band in a down trend. On a flip bar the line breaks (no value) so it does not join the two sides with a vertical stroke; the flip triangle marks that bar. Same trend as the built-in Supertrend.',
  },
  calculate(ctx) {
    const { bars, inputs, ta } = ctx;
    const atr = ta.atr(bars, inputs.atrLength);
    const items = bars.map((bar, index) => ({ bar, range: atr[index], previousClose: index > 0 ? bars[index - 1].close : Number.NaN }));
    // Final bands and the trend carry from bar to bar; `flip` is +1/-1 on a bar whose close flips the trend.
    const states = ta.scan(items, { upper: null, lower: null, up: null, flip: 0 }, (state, item) => {
      const { bar, range, previousClose } = item;
      if (range === null) return state.flip === 0 ? state : { ...state, flip: 0 };
      const middle = (bar.high + bar.low) / 2;
      const basicUpper = middle + inputs.factor * range;
      const basicLower = middle - inputs.factor * range;
      const upper = state.upper === null || basicUpper < state.upper || previousClose > state.upper ? basicUpper : state.upper;
      const lower = state.lower === null || basicLower > state.lower || previousClose < state.lower ? basicLower : state.lower;
      // The first bar with an ATR starts in a down trend; later bars flip on the close.
      const up = state.up === null ? false : state.up ? !(bar.close < lower) : bar.close > upper;
      const flip = state.up === null || state.up === up ? 0 : up ? 1 : -1;
      return { upper, lower, up, flip };
    });
    const supertrend = states.map((state) => (state.up === null || state.flip !== 0 ? null : state.up ? state.lower : state.upper));
    const trend = states.map((state) => (state.up === null ? null : state.up ? 0 : 1));
    const flipUp = [];
    const flipDown = [];
    if (inputs.showFlips) {
      for (let index = 0; index < states.length; index += 1) {
        if (states[index].flip === 1) flipUp.push({ index });
        else if (states[index].flip === -1) flipDown.push({ index });
      }
    }
    return {
      plots: { supertrend },
      colors: { supertrend: trend },
      barColors: { candles: inputs.colorBars ? trend : trend.map(() => null) },
      markers: { flipUp: flipUp.slice(-MAX_FLIPS), flipDown: flipDown.slice(-MAX_FLIPS) },
    };
  },
};

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/supertrend-colored.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.