Moving Average Convergence Divergence
MACD line, signal line and a four-color histogram that shows whether momentum is rising or falling.
- MACD
- Built into Spectral
- Updates every tick
- macd
- moving average
- histogram
- momentum
- crossover
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
MACD compares a fast and a slow moving average of the selected price. The MACD line is their difference, the signal line smooths that difference, and the histogram shows the distance between MACD and signal.
How to read it
A MACD line above its signal line means the fast average is pulling away upward; below means the opposite. Histogram bars above zero are positive and below zero negative.
The four histogram colors show direction: dark green and light green above zero for rising and falling bars, light red and dark red below zero for rising and falling bars. Expanding bars show momentum increasing relative to the prior bar; contracting bars show it easing.
Settings and limitations
Defaults are 12 fast, 26 slow and 9 signal bars with exponential averages. Shorter lengths react sooner but are noisier; longer lengths smooth more but lag. Exponential averages start at the first bar, so the earliest values settle after a few multiples of the slow length.
Notes
Computes the same values and colors as the built-in MACD in Spectral. Crosses and zero-line moves are context, not standalone trade instructions.
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 |
|---|---|---|---|---|
| Fast length | Whole number | 12 | 1 – 500 | Length of the fast moving average. |
| Slow length | Whole number | 26 | 1 – 500 | Length of the slow moving average. |
| Signal length | Whole number | 9 | 1 – 500 | Length of the signal-line smoothing. |
| Source | Price source | close | open, high, low, close, hlc3 | Price the averages are calculated from. |
| Oscillator average | Choice | EMA | EMA, SMA | Average type of the fast and slow lines. |
| Signal average | Choice | EMA | EMA, SMA | Average type of the signal line. |
What it draws
Lines
- MACDsolid · 1.5 px · primary
#8AA9C3 - Signalsolid · 1 px
#C4A46A
Levels
- Zero linedotted · 1 px
#788493
Histograms
- Histogram above zero, risinghistogram bars
#6FAF8A - Histogram above zero, fallinghistogram bars
#A4C9B1 - Histogram below zero, risinghistogram bars
#D8B1B1 - Histogram below zero, fallinghistogram bars
#C98282
Source
Save it as macd.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').IndicatorV1} */
export default {
schemaVersion: 1,
id: 'macd',
name: 'Moving Average Convergence Divergence',
shortName: 'MACD',
version: '1.0.0',
author: 'Spectral',
description: 'MACD line, signal line and a four-color histogram that shows whether momentum is rising or falling.',
pane: 'oscillator',
live: { lookback: 1 },
inputs: {
fastLength: { type: 'integer', label: 'Fast length', default: 12, min: 1, max: 500, description: 'Length of the fast moving average.' },
slowLength: { type: 'integer', label: 'Slow length', default: 26, min: 1, max: 500, description: 'Length of the slow moving average.' },
signalLength: { type: 'integer', label: 'Signal length', default: 9, min: 1, max: 500, description: 'Length of the signal-line smoothing.' },
source: { type: 'source', label: 'Source', default: 'close', description: 'Price the averages are calculated from.' },
oscillatorMaType: { type: 'select', label: 'Oscillator average', default: 'EMA', options: ['EMA', 'SMA'], description: 'Average type of the fast and slow lines.' },
signalMaType: { type: 'select', label: 'Signal average', default: 'EMA', options: ['EMA', 'SMA'], description: 'Average type of the signal line.' },
},
plots: {
macd: { label: 'MACD', color: '#8AA9C3', lineWidth: 1.5, primary: true },
signal: { label: 'Signal', color: '#C4A46A', lineWidth: 1 },
},
levels: {
zero: { label: 'Zero line', color: '#788493', lineStyle: 'dotted' },
},
columns: {
posRising: { label: 'Histogram above zero, rising', color: '#6FAF8A' },
posFalling: { label: 'Histogram above zero, falling', color: '#A4C9B1' },
negRising: { label: 'Histogram below zero, rising', color: '#D8B1B1' },
negFalling: { label: 'Histogram below zero, falling', color: '#C98282' },
},
docs: {
overview: 'MACD compares a fast and a slow moving average of the selected price. The MACD line is their difference, the signal line smooths that difference, and the histogram shows the distance between MACD and signal.',
howToRead: 'A MACD line above its signal line means the fast average is pulling away upward; below means the opposite. Histogram bars above zero are positive and below zero negative.\n\nThe four histogram colors show direction: dark green and light green above zero for rising and falling bars, light red and dark red below zero for rising and falling bars. Expanding bars show momentum increasing relative to the prior bar; contracting bars show it easing.',
settings: 'Defaults are 12 fast, 26 slow and 9 signal bars with exponential averages. Shorter lengths react sooner but are noisier; longer lengths smooth more but lag. Exponential averages start at the first bar, so the earliest values settle after a few multiples of the slow length.',
notes: 'Computes the same values and colors as the built-in MACD in Spectral. Crosses and zero-line moves are context, not standalone trade instructions.',
},
calculate(ctx) {
const { bars, inputs, ta } = ctx;
const source = ta.source(bars, inputs.source);
const average = (values, length, type) => (type === 'SMA' ? ta.sma(values, length) : ta.ema(values, length));
const fast = average(source, inputs.fastLength, inputs.oscillatorMaType);
const slow = average(source, inputs.slowLength, inputs.oscillatorMaType);
const macd = fast.map((value, index) => (value === null || slow[index] === null ? null : value - slow[index]));
const signal = average(macd, inputs.signalLength, inputs.signalMaType);
const histogram = macd.map((value, index) => (value === null || signal[index] === null ? null : value - signal[index]));
const columns = { posRising: [], posFalling: [], negRising: [], negFalling: [] };
for (let index = 0; index < bars.length; index += 1) {
const value = histogram[index];
const current = value === null ? 0 : value;
const before = index > 0 && histogram[index - 1] !== null ? histogram[index - 1] : current;
const role = current >= 0
? (current > before ? 'posRising' : 'posFalling')
: (current > before ? 'negRising' : 'negFalling');
for (const key of Object.keys(columns)) columns[key].push(key === role ? value : null);
}
return { plots: { macd, signal }, levels: { zero: 0 }, columns };
},
};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/macd.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.


