Moving Average
One moving average of the type you choose (simple, exponential, weighted, Hull, Wilder or volume-weighted) and any length, drawn on the candles.
- MA
- Built into Spectral
- Updates every tick
- Uses volume
- moving average
- sma
- ema
- wma
- hma
- rma
- vwma
- trend
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
One indicator for the six common moving averages, so any type and length is a settings change away.
SMA is the plain average of the last Length prices. EMA weights each new price by 2 / (Length + 1) and carries the rest of its previous value. WMA weights the newest price Length times, the one before it Length - 1 times, and so on. HMA (Hull) combines two weighted averages to cut most of the lag. RMA is Wilder's smoothing, an exponential average with weight 1 / Length, the one inside RSI and ATR. VWMA weights each price by its bar's volume, so heavy-volume bars pull the line harder.
How to read it
A rising average describes positive recent price pressure; a falling one, negative pressure. Traders watch how price behaves around the line (holds, reclaims, rejections), its slope, and how several averages are stacked, rather than treating a single cross as a signal.
At the same length, HMA reacts fastest, then WMA, then EMA and SMA (the EMA turns a little sooner), and RMA slowest. A VWMA above the SMA of the same length means the heavier-volume bars in the window traded at higher prices; below it, at lower prices.
Settings and limitations
Type picks the average; Length defaults to 20 bars; Source is the price used. The presets add WMA 20 and VWMA 20; SMA, EMA and Hull lengths traders use most already have their own entries (SMA 50 and 200, EMA 21 and 200, Hull MA 55).
SMA, WMA, VWMA and RMA show their first value after Length bars, HMA a few bars later. EMA starts at the first bar, so its earliest values lean on the first prices and settle after a few multiples of Length. VWMA needs volume; on a symbol without volume data it draws nothing.
Notes
Each type follows its usual definition. The EMA starts from the first price; some platforms, TradingView among them, start it from the simple average of the first Length prices and draw nothing before, so the earliest values differ and agree once warmed up. RMA starts from the simple average of its first Length prices. HMA rounds the half length and the square-root length down. The SMA and EMA types compute the same values as Spectral's own SMA and EMA indicators.
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 |
|---|---|---|---|---|
| Type | Choice | SMA | SMA, EMA, WMA, HMA, RMA, VWMA | SMA simple, EMA exponential, WMA linearly weighted, HMA Hull, RMA Wilder smoothing (as in RSI and ATR), VWMA weighted by each bar's volume. |
| Length | Whole number | 20 | 1 – 1000 | Number of bars in the average. Longer averages are smoother and react later. |
| Source | Price source | close | open, high, low, close, hlc3 | Price the average is calculated from. |
Presets
Each preset is its own entry in the Indicators toolbox, with these settings changed.
| Toolbox entry | Short name | Settings |
|---|---|---|
| WMA 20 | WMA 20 | Type: WMALength: 20 |
| VWMA 20 | VWMA 20 | Type: VWMALength: 20 |
What it draws
Lines
- MAsolid · 1 px · primary
#B7A1C8
Source
Save it as moving-average.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
/** Hull: a weighted average of twice the half-length WMA minus the full WMA, over the square root of the length. */
function hullAverage(ta, values, length) {
const half = ta.wma(values, Math.max(1, Math.floor(length / 2)));
const full = ta.wma(values, length);
const raw = full.map((value, index) => (value === null || half[index] === null ? null : 2 * half[index] - value));
return ta.wma(raw, Math.max(1, Math.floor(Math.sqrt(length))));
}
/** Sum of price times volume over the window divided by the window's volume; no value while the window has no volume. */
function volumeWeightedAverage(ta, bars, values, length) {
const volumes = bars.map((bar) => (typeof bar.volume === 'number' && Number.isFinite(bar.volume) && bar.volume > 0 ? bar.volume : 0));
const weighted = ta.sum(values.map((value, index) => value * volumes[index]), length);
const total = ta.sum(volumes, length);
return weighted.map((value, index) => (value === null || !(total[index] > 0) ? null : value / total[index]));
}
/** @type {import('./spectral-indicator').IndicatorV2} */
export default {
schemaVersion: 2,
id: 'moving-average',
name: 'Moving Average',
shortName: 'MA',
version: '1.0.0',
author: 'Spectral',
description: 'One moving average of the type you choose (simple, exponential, weighted, Hull, Wilder or volume-weighted) and any length, drawn on the candles.',
pane: 'price',
live: { lookback: '2*length' },
usesVolume: true,
inputs: {
maType: { type: 'select', label: 'Type', default: 'SMA', options: ['SMA', 'EMA', 'WMA', 'HMA', 'RMA', 'VWMA'], description: 'SMA simple, EMA exponential, WMA linearly weighted, HMA Hull, RMA Wilder smoothing (as in RSI and ATR), VWMA weighted by each bar\'s volume.' },
length: { type: 'integer', label: 'Length', default: 20, min: 1, max: 1000, description: 'Number of bars in the average. Longer averages are smoother and react later.' },
source: { type: 'source', label: 'Source', default: 'close', description: 'Price the average is calculated from.' },
},
plots: {
ma: { label: 'MA', color: '#B7A1C8', lineWidth: 1, primary: true },
},
presets: [
{ id: 'wma-20', name: 'WMA 20', shortName: 'WMA 20', inputs: { maType: 'WMA', length: 20 } },
{ id: 'vwma-20', name: 'VWMA 20', shortName: 'VWMA 20', inputs: { maType: 'VWMA', length: 20 } },
],
docs: {
overview: 'One indicator for the six common moving averages, so any type and length is a settings change away.\n\nSMA is the plain average of the last Length prices. EMA weights each new price by 2 / (Length + 1) and carries the rest of its previous value. WMA weights the newest price Length times, the one before it Length - 1 times, and so on. HMA (Hull) combines two weighted averages to cut most of the lag. RMA is Wilder\'s smoothing, an exponential average with weight 1 / Length, the one inside RSI and ATR. VWMA weights each price by its bar\'s volume, so heavy-volume bars pull the line harder.',
howToRead: 'A rising average describes positive recent price pressure; a falling one, negative pressure. Traders watch how price behaves around the line (holds, reclaims, rejections), its slope, and how several averages are stacked, rather than treating a single cross as a signal.\n\nAt the same length, HMA reacts fastest, then WMA, then EMA and SMA (the EMA turns a little sooner), and RMA slowest. A VWMA above the SMA of the same length means the heavier-volume bars in the window traded at higher prices; below it, at lower prices.',
settings: 'Type picks the average; Length defaults to 20 bars; Source is the price used. The presets add WMA 20 and VWMA 20; SMA, EMA and Hull lengths traders use most already have their own entries (SMA 50 and 200, EMA 21 and 200, Hull MA 55).\n\nSMA, WMA, VWMA and RMA show their first value after Length bars, HMA a few bars later. EMA starts at the first bar, so its earliest values lean on the first prices and settle after a few multiples of Length. VWMA needs volume; on a symbol without volume data it draws nothing.',
notes: 'Each type follows its usual definition. The EMA starts from the first price; some platforms, TradingView among them, start it from the simple average of the first Length prices and draw nothing before, so the earliest values differ and agree once warmed up. RMA starts from the simple average of its first Length prices. HMA rounds the half length and the square-root length down. The SMA and EMA types compute the same values as Spectral\'s own SMA and EMA indicators.',
},
calculate(ctx) {
const { bars, inputs, ta } = ctx;
const values = ta.source(bars, inputs.source);
const length = inputs.length;
switch (inputs.maType) {
case 'EMA': return { plots: { ma: ta.ema(values, length) } };
case 'WMA': return { plots: { ma: ta.wma(values, length) } };
case 'HMA': return { plots: { ma: hullAverage(ta, values, length) } };
case 'RMA': return { plots: { ma: ta.rma(values, length) } };
case 'VWMA': return { plots: { ma: volumeWeightedAverage(ta, bars, values, length) } };
default: return { plots: { ma: ta.sma(values, length) } };
}
},
};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/moving-average.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.


