# MACD

> Read the Spreads Before You Read the Signals

`D07-F02-A01` · Technical Indicators → Trend Systems · archetype `series-transform` · difficulty 2/5 · verification **verified**

Full page: https://docs.thefintechbuilder.com/technical-indicators/trend-systems/macd/
Agent skill: `npx skills add IslamBaraka90/Fintech-Algorithms-Library` — https://docs.thefintechbuilder.com/guides/agent-skill/

## Install and import

```bash
npm install fintech-algorithms
```

```ts
import { macd } from "fintech-algorithms/technical-indicators/trend-systems/macd";
```

## Signature

```ts
macd(values, fastSpan, slowSpan, signalSpan)
```

Moving average convergence/divergence: the gap between a fast and a slow EMA, its own EMA as a signal line, and the difference between them as a histogram.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `values` | `(number \| null)[]` | yes | Observation series in chronological order, oldest first. · nulls: propagate |
| `fastSpan` | `number` | yes | Span of the fast EMA. · min: 1, integer: true |
| `slowSpan` | `number` | yes | Span of the slow EMA; must exceed the fast span. · min: 1, integer: true |
| `signalSpan` | `number` | yes | Span of the EMA taken over the MACD line itself. · min: 1, integer: true |

## Returns

`{ fast_ema, slow_ema, macd, signal, histogram }[]` · length same-as-input

One record per position carrying both EMAs alongside the three published series.

## Warm-up

The first `slowSpan − 1 for the MACD line, and slowSpan + signalSpan − 2 for the signal and histogram` positions are `null`. The signal line is an average *of* the MACD line, so it is defined strictly later — plotting the two without allowing for that misaligns them.

## Errors

- When any span is < 1, is not an integer, or fastSpan ≥ slowSpan — throws RangeError

## Complexity

Time `O(n)`, space `O(n)`.

## Worked example

This is the worked example published in the article, replayed by the test suite on every run. The output cannot drift.

### Input

`values`:

```json
[10, 11, 12, 13, 14, 15]
```

Showing 6 of 14 elements.

`fastSpan`:

```json
3
```

`slowSpan`:

```json
5
```

`signalSpan`:

```json
3
```

### Call

```ts
macd(values, fastSpan, slowSpan, signalSpan)
```

### Returns

object with 5 fields: 4, 6, 7, 9, 12

```json
{
  "4": {
    "fastEma": 13,
    "slowEma": 12,
    "macd": 1,
    "signal": null,
    "histogram": null,
    "status": "warming_signal"
  },
  "6": {
    "fastEma": 15,
    "slowEma": 14,
    "macd": 1,
    "signal": 1,
    "histogram": 0,
    "status": "ready"
  },
  "7": {
    "fastEma": 15,
    "slowEma": 14.333333333333334,
    "macd": 0.6666666666666661,
    "signal": 0.833333333333333,
    "histogram": -0.16666666666666696,
    "status": "ready"
  },
  "9": {
    "macd": -0.06481481481481488,
    "signal": 0.24537037037037002,
    "histogram": -0.3101851851851849,
    "status": "ready"
  },
  "12": {
    "macd": 0.2088048696844993,
    "signal": 0.04260973936899856,
    "histogram": 0.16619513031550073,
    "status": "ready"
  }
}
```

## Verification and provenance

Tier: **verified** (via input-expected).

The worked example below is the figure published in this algorithm's article, replayed and asserted by the test suite on every build. The arithmetic cannot drift without the build failing.

Both tiers guarantee the signature. Full explanation: https://docs.thefintechbuilder.com/guides/verification/

Generated from the docs.json payload shipped inside fintech-algorithms@0.13.1.
The signature and parameter list are checked against the compiled implementation at build time,
so a description that contradicts the code fails the build rather than reaching this file.

## Links

- Article (how it works, step by step): https://thefintechbuilder.com/technical-indicators/trend-systems/macd/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/technical-indicators/trend-systems/macd/impl.ts
- Standalone repository: https://github.com/IslamBaraka90/Fintech-MACD-Moving-Average-Convergence-Divergence-algorithm
- Package on npm: https://www.npmjs.com/package/fintech-algorithms
- Domain index for agents: https://docs.thefintechbuilder.com/technical-indicators/llms.txt
