Supertrend
A trailing stop line that follows price at a multiple of the ATR: green below price in an uptrend, red above it in a downtrend.
- ST
- Built into Spectral
- Updates every tick
- supertrend
- trailing stop
- atr
- trend direction
- trend following
- stop and reverse
MIT-0: free to use, change and share, no attribution needed. What you build from it is yours.
Already in Spectral: add it from the Indicators toolbox. The file here is the same indicator written as a custom indicator, to change or learn from. Installed, it appears next to the built-in one, marked Custom.

Overview
Supertrend trails price with a line placed a multiple of the Average True Range 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 jumps to the other side.
The up-trend line is green and the down-trend line red; optional dots mark the bars where the trend changes.
How to read it
Green line under price: the trend is up and the line is a trailing stop for longs. Red line above price: the trend is down and the line is a trailing stop for shorts. A flip happens on the close, so on the forming bar the line can change sides 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. It describes the trend; it does not predict how far a move will go.
Settings and limitations
ATR length (10) and Factor (3) are the common defaults; 7 and 3 reacts faster, 10 and 2 trails tighter. The line appears after ATR length bars and starts in a down trend until the first close above it; like TradingView, the first bar never shows a line. It suits any bar size.
Notes
Matches TradingView's built-in Supertrend: Wilder ATR, the middle of the bar as the base, bands that only tighten while the trend lasts, and a flip on the close. The two lines break at a flip instead of joining, like TradingView.
Settings
Each setting appears in the indicator's settings panel in Spectral, next to a color setting for every output.
| Setting | Type | Default | Range or options | What it does |
|---|---|---|---|---|
| ATR length | Whole number | 10 | 1 – 500 | Number of bars in the Average True Range (Wilder smoothing) that sets the distance of the line from price. |
| Factor | Number | 3 | 0.1 – 20, step 0.1 | Multiple of the ATR between the middle of the bar and the line. A larger factor flips less often and trails further away. |
| Trend change dots | On / off | On | — | Mark each bar where the trend flips with a dot on the new line: green below price when it turns up, red above price when it turns down. |
Presets
Each preset is its own entry in the Indicators toolbox, with these settings changed.
| Toolbox entry | Short name | Settings |
|---|---|---|
| Supertrend (7, 3) | ST 7 | ATR length: 7Factor: 3 |
| Supertrend (10, 2) | ST 10/2 | ATR length: 10Factor: 2 |
What it draws
Lines
- Up trendsolid · 1.5 px · primary
#3FB950 - Down trendsolid · 1.5 px
#E5534B
Markers
- Turns updot, radius 4
#3FB950 - Turns downdot, radius 4
#E5534B
Source
Save it as supertrend.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').IndicatorV2} */
export default {
schemaVersion: 2,
id: 'supertrend',
name: 'Supertrend',
shortName: 'ST',
version: '1.0.0',
author: 'Spectral',
description: 'A trailing stop line that follows price at a multiple of the ATR: green below price in an uptrend, red above it in a downtrend.',
pane: 'price',
live: { lookback: 1 },
inputs: {
atrLength: { type: 'integer', label: 'ATR length', default: 10, min: 1, max: 500, description: 'Number of 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.' },
showFlips: { type: 'boolean', label: 'Trend change dots', default: true, description: 'Mark each bar where the trend flips with a dot on the new line: green below price when it turns up, red above price when it turns down.' },
},
plots: {
upTrend: { label: 'Up trend', color: '#3FB950', lineWidth: 1.5, primary: true, gaps: 'break' },
downTrend: { label: 'Down trend', color: '#E5534B', lineWidth: 1.5, gaps: 'break' },
},
markers: {
flipUp: { label: 'Turns up', color: '#3FB950', radius: 4 },
flipDown: { label: 'Turns down', color: '#E5534B', radius: 4 },
},
presets: [
{ id: 'fast', name: 'Supertrend (7, 3)', shortName: 'ST 7', inputs: { atrLength: 7, factor: 3 } },
{ id: 'tight', name: 'Supertrend (10, 2)', shortName: 'ST 10/2', inputs: { atrLength: 10, factor: 2 } },
],
docs: {
overview: 'Supertrend trails price with a line placed a multiple of the Average True Range 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 jumps to the other side.\n\nThe up-trend line is green and the down-trend line red; optional dots mark the bars where the trend changes.',
howToRead: 'Green line under price: the trend is up and the line is a trailing stop for longs. Red line above price: the trend is down and the line is a trailing stop for shorts. A flip happens on the close, so on the forming bar the line can change sides 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. It describes the trend; it does not predict how far a move will go.',
settings: 'ATR length (10) and Factor (3) are the common defaults; 7 and 3 reacts faster, 10 and 2 trails tighter. The line appears after ATR length bars and starts in a down trend until the first close above it; like TradingView, the first bar never shows a line. It suits any bar size.',
notes: 'Matches TradingView\'s built-in Supertrend: Wilder ATR, the middle of the bar as the base, bands that only tighten while the trend lasts, and a flip on the close. The two lines break at a flip instead of joining, like TradingView.',
},
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 }));
// The bands and the trend carry from bar to bar through ta.scan; `shown` is false where the bar has no value.
const initial = { upper: null, lower: null, direction: 0, shown: false, flip: 0 };
const states = ta.scan(items, initial, (state, item, index) => {
const { bar, range, previousClose } = item;
if (range === null) return { ...state, shown: false, flip: 0 };
const middle = (bar.high + bar.low) / 2;
const basicUpper = middle + inputs.factor * range;
const basicLower = middle - inputs.factor * range;
// A band only tightens while the trend lasts; it resets once the previous close breaks it.
// Before the first band, the previous band counts as 0, as on TradingView.
const previousLower = state.lower === null ? 0 : state.lower;
const previousUpper = state.upper === null ? 0 : state.upper;
const lower = basicLower > previousLower || previousClose < previousLower ? basicLower : previousLower;
const upper = basicUpper < previousUpper || previousClose > previousUpper ? basicUpper : previousUpper;
const previousDirection = state.direction;
// The first bar starts in a down trend, like TradingView; later bars flip on the close.
let direction;
if (previousDirection === 0) direction = -1;
else if (previousDirection === -1) direction = bar.close > upper ? 1 : -1;
else direction = bar.close < lower ? -1 : 1;
// TradingView's Supertrend plots nothing on the first bar of the chart (`barstate.isfirst ? na : supertrend`), also
// when an ATR length of 1 gives that bar a value; the trend and bands still start there.
const shown = index !== 0;
const flip = shown && previousDirection !== 0 && previousDirection !== direction ? direction : 0;
return { upper, lower, direction, shown, flip };
});
const upTrend = states.map((state) => (state.shown && state.direction === 1 ? state.lower : null));
const downTrend = states.map((state) => (state.shown && state.direction !== 1 ? state.upper : null));
const flipUp = [];
const flipDown = [];
if (inputs.showFlips) {
states.forEach((state, index) => {
if (state.flip === 1) flipUp.push({ index, value: state.lower });
else if (state.flip === -1) flipDown.push({ index, value: state.upper });
});
}
return { plots: { upTrend, downTrend }, markers: { flipUp, flipDown } };
},
};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.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.


