# ZigZag Segmentation

`D08-F01-A02` · Geometric Chart Patterns → Pivots and Levels · archetype `record-transform` · difficulty 3/5 · verification **verified**

Full page: https://docs.thefintechbuilder.com/geometric-chart-patterns/pivots-and-levels/zigzag-segmentation/
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 { zigzagSegment } from "fintech-algorithms/geometric-chart-patterns/pivots-and-levels/zigzag-segmentation";
```

## Signature

```ts
zigzagSegment(closes, threshold, minBars)
```

Segments a series into alternating swings that exceed a percentage threshold. Useful for structure, and routinely misused: the final swing is provisional and can be revised by the next bar.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `closes` | `number[]` | yes | Observation series in chronological order, oldest first. |
| `threshold` | `number` | yes | Minimum retracement, as a fraction, before a reversal is accepted. · min: 0 |
| `minBars` | `number` | yes | Minimum bars a swing must span. · min: 1, integer: true |

## Returns

`{ pivots, states }`

The pivots with a per-bar state, which is what shows the last swing is still open rather than settled.

## Errors

- When threshold is not positive — throws

## 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

`closes`:

```json
[100, 103, 106, 101, 100]
```

`threshold`:

```json
0.05
```

`minBars`:

```json
2
```

### Call

```ts
zigzagSegment(closes, threshold, minBars)
```

### Returns

object with 1 field: pivots

```json
{
  "pivots": [
    {
      "kind": "low",
      "event_index": 0,
      "confirmation_index": 2,
      "price": 100
    },
    {
      "kind": "high",
      "event_index": 2,
      "confirmation_index": 4,
      "price": 106
    }
  ]
}
```

## 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/geometric-chart-patterns/pivots-and-levels/zigzag-segmentation/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/geometric-chart-patterns/pivots-and-levels/zigzag-segmentation/impl.ts
- Package on npm: https://www.npmjs.com/package/fintech-algorithms
- Domain index for agents: https://docs.thefintechbuilder.com/geometric-chart-patterns/llms.txt
