Moving Average Envelopes
A moving average with bands a fixed percentage above and below it.
- ENV
- Built into Spectral
- Updates every tick
- envelopes
- moving average envelope
- percent bands
- moving average
- channel
- bands
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
Moving Average Envelopes draw a moving average (the basis) with an upper and a lower band a fixed percentage above and below it. With the defaults, the bands sit 2.5 percent above and below the 20-bar simple moving average of the close.
Unlike Bollinger Bands or Keltner Channels, the distance does not follow volatility: it is always the same share of the average.
How to read it
The envelopes show how far price has stretched from its average. In a range, closes near or beyond a band often mark an overextended move that tends to return toward the basis. In a strong trend, price can hug one envelope for a long time, and a pullback to the basis can be an entry instead.
Because the width is fixed, choose it for the market: it should contain most of the price action, with only the larger moves reaching the bands.
Settings and limitations
Length sets the average (20 is common); Average switches between a simple and an exponential average. Percent sets the band distance: about 2 to 3 percent suits daily stock charts, intraday charts need well under 1 percent, and very volatile markets need more. The presets give tight (1 percent) and wide (10 percent, TradingView's default) envelopes.
The simple average appears after Length bars; the exponential one from the first bar.
Notes
Matches TradingView's Envelope apart from the default distance: TradingView starts with 10 percent, Spectral with the 2.5 percent StockCharts uses for daily charts, and offers 10 percent as a preset. With the exponential average, the first few dozen values can differ from TradingView's, which starts its average after Length bars.
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 |
|---|---|---|---|---|
| Length | Whole number | 20 | 1 – 500 | Bars in the moving average. |
| Average | Choice | SMA | SMA, EMA | Simple or exponential moving average for the basis. |
| Source | Price source | close | open, high, low, close, hlc3 | Price the average is computed from. |
| Percent | Number | 2.5 | 0.01 – 50, step 0.1 | Distance of each band from the average, in percent of the average. |
Presets
Each preset is its own entry in the Indicators toolbox, with these settings changed.
| Toolbox entry | Short name | Settings |
|---|---|---|
| Envelopes (20, 1%) | ENV 1% | Percent: 1 |
| Envelopes (20, 10%) | ENV 10% | Percent: 10 |
What it draws
Lines
- Basissolid · 1.5 px · primary
#C4A46A - Upper envelopesolid · 1 px
#B7A1C8 - Lower envelopesolid · 1 px
#B7A1C8
Zones
- Envelope fillfilled at 6% opacity
#B7A1C8
Source
Save it as envelopes.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: 'envelopes',
name: 'Moving Average Envelopes',
shortName: 'ENV',
version: '1.0.0',
author: 'Spectral',
description: 'A moving average with bands a fixed percentage above and below it.',
pane: 'price',
live: { lookback: 0 },
inputs: {
length: { type: 'integer', label: 'Length', default: 20, min: 1, max: 500, description: 'Bars in the moving average.' },
maType: { type: 'select', label: 'Average', default: 'SMA', options: ['SMA', 'EMA'], description: 'Simple or exponential moving average for the basis.' },
source: { type: 'source', label: 'Source', default: 'close', description: 'Price the average is computed from.' },
percent: { type: 'number', label: 'Percent', default: 2.5, min: 0.01, max: 50, step: 0.1, description: 'Distance of each band from the average, in percent of the average.' },
},
plots: {
basis: { label: 'Basis', color: '#C4A46A', lineWidth: 1.5, primary: true },
upper: { label: 'Upper envelope', color: '#B7A1C8', lineWidth: 1 },
lower: { label: 'Lower envelope', color: '#B7A1C8', lineWidth: 1 },
},
zones: {
band: { label: 'Envelope fill', color: '#B7A1C8', opacity: 0.06 },
},
presets: [
{ id: 'tight', name: 'Envelopes (20, 1%)', shortName: 'ENV 1%', inputs: { percent: 1 } },
{ id: 'wide', name: 'Envelopes (20, 10%)', shortName: 'ENV 10%', inputs: { percent: 10 } },
],
docs: {
overview: 'Moving Average Envelopes draw a moving average (the basis) with an upper and a lower band a fixed percentage above and below it. With the defaults, the bands sit 2.5 percent above and below the 20-bar simple moving average of the close.\n\nUnlike Bollinger Bands or Keltner Channels, the distance does not follow volatility: it is always the same share of the average.',
howToRead: 'The envelopes show how far price has stretched from its average. In a range, closes near or beyond a band often mark an overextended move that tends to return toward the basis. In a strong trend, price can hug one envelope for a long time, and a pullback to the basis can be an entry instead.\n\nBecause the width is fixed, choose it for the market: it should contain most of the price action, with only the larger moves reaching the bands.',
settings: 'Length sets the average (20 is common); Average switches between a simple and an exponential average. Percent sets the band distance: about 2 to 3 percent suits daily stock charts, intraday charts need well under 1 percent, and very volatile markets need more. The presets give tight (1 percent) and wide (10 percent, TradingView\'s default) envelopes.\n\nThe simple average appears after Length bars; the exponential one from the first bar.',
notes: 'Matches TradingView\'s Envelope apart from the default distance: TradingView starts with 10 percent, Spectral with the 2.5 percent StockCharts uses for daily charts, and offers 10 percent as a preset. With the exponential average, the first few dozen values can differ from TradingView\'s, which starts its average after Length bars.',
},
calculate(ctx) {
const { bars, inputs, ta } = ctx;
const source = ta.source(bars, inputs.source);
const basis = inputs.maType === 'EMA' ? ta.ema(source, inputs.length) : ta.sma(source, inputs.length);
const factor = inputs.percent / 100;
const upper = basis.map((value) => (value === null ? null : value * (1 + factor)));
const lower = basis.map((value) => (value === null ? null : value * (1 - factor)));
return { plots: { basis, upper, lower }, zones: { band: { upper, lower } } };
},
};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/envelopes.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.


