# Breadth-Divergence Detector

`D04-F04-A06` · Market Breadth and Internals → Thrust and Pressure · archetype `record-transform` · difficulty 5/5 · verification **verified**

Full page: https://docs.thefintechbuilder.com/market-breadth-and-internals/thrust-and-pressure/breadth-divergence-detector/
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 { calculate } from "fintech-algorithms/market-breadth-and-internals/thrust-and-pressure/breadth-divergence-detector";
```

## Signature

```ts
calculate(rows, left, right, minPriceChange, minBreadthSeparation, breadthScale)
```

Flags sessions where the index and a breadth measure move meaningfully in opposite directions — the classic warning that an advance is being carried by fewer and fewer names.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `rows` | `Row[]` | yes | Sessions carrying an index level and a breadth value. Rows carry a `ready` flag; a row that is not ready is excluded rather than treated as zero, because a missing count and a count of zero mean opposite things about market breadth. |
| `left` | `number` | yes | Left comparison offset in sessions. · min: 1, integer: true |
| `right` | `number` | yes | Right comparison offset in sessions. · min: 0, integer: true |
| `minPriceChange` | `number` | yes | Minimum index move required before a divergence is considered; filters noise. · min: 0 |
| `minBreadthSeparation` | `number` | yes | Minimum breadth move required, expressed in the units `breadthScale` implies. · min: 0 |
| `breadthScale` | `number` | yes | Scaling applied to the breadth series before comparison. **Note:** the published fixture and this implementation currently disagree on the unit of the resulting `breadth_separation` — the fixture states a ratio, the implementation returns a scaled value. See the article before relying on an absolute threshold. · min: 0 |

## Returns

`{ status, events }`

The divergence events detected, each naming the sessions compared and the separation measured.

## Errors

- When left ≤ right, making the comparison window invalid — throws

## Complexity

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

## Worked example

Captured by running this function on the input its own test provides. Real output of real code — but not asserted against a published figure.

### Input

`rows`:

```json
[
  {
    "session_date": "2026-01-05",
    "index_level": 100,
    "breadth_value": 500,
    "ready": true
  },
  {
    "session_date": "2026-01-06",
    "index_level": 101,
    "breadth_value": 520,
    "ready": true
  },
  {
    "session_date": "2026-01-07",
    "index_level": 102,
    "breadth_value": 540,
    "ready": true
  }
]
```

Showing 3 of 36 elements.

`left`:

```json
2
```

`right`:

```json
2
```

`minPriceChange`:

```json
0.01
```

`minBreadthSeparation`:

```json
0.05
```

`breadthScale`:

```json
1000
```

### Call

```ts
calculate(rows, left, right, minPriceChange, minBreadthSeparation, breadthScale)
```

### Returns

object with 2 fields: status, events

```json
{
  "status": "bearish",
  "events": [
    {
      "status": "bearish",
      "first_pivot_date": "2026-01-19",
      "second_pivot_date": "2026-02-05",
      "confirmation_date": "2026-02-09",
      "price_change": 0.05454545454545454,
      "breadth_separation": -0.13,
      "breadth_scale": 1000
    }
  ]
}
```

## Verification and provenance

Tier: **verified** (via row-fixture).

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/market-breadth-and-internals/thrust-and-pressure/breadth-divergence-detector/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/market-breadth-and-internals/thrust-and-pressure/breadth-divergence-detector/impl.ts
- Package on npm: https://www.npmjs.com/package/fintech-algorithms
- Domain index for agents: https://docs.thefintechbuilder.com/market-breadth-and-internals/llms.txt
