# Robust Trendline Fitting

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

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

## Signature

```ts
fitRobustTrendline(pivots, kind, toleranceBps, minInliers, maxPoints, projectionIndex)
```

Fits a trendline through pivots in log space with outlier resistance. Log space matters: a straight line in price implies a constant *dollar* change per bar, which is not what a trend on a long chart means.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `pivots` | `Pivot[]` | yes | Pivots to fit through. |
| `kind` | `"support" \| "resistance"` | yes | Which side to fit, which decides how violations are treated. |
| `toleranceBps` | `number` | yes | How far a pivot may sit from the line and still count as an inlier. · min: 0 |
| `minInliers` | `number` | yes | Minimum inliers for the fit to be accepted. · min: 2, integer: true |
| `maxPoints` | `number` | yes | Cap on pivots considered, bounding the search. · min: 2, integer: true |
| `projectionIndex` | `number` | no | Bar index to project the fitted line to. Defaults to one past the highest pivot index, so the line is extended a single bar beyond its support. |

## Returns

`{ kind, slope_log_per_bar, intercept_log, projected_index, projected_price, inlier_count, … }`

The fit in log space plus its projection in price, with the inlier count — a two-point 'trendline' is arithmetic, not evidence.

## Errors

- When fewer pivots are supplied than minInliers — throws

## Complexity

Time `O(points²)`, space `O(points)`.

## Worked example

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

### Input

`pivots`:

```json
[
  {
    "kind": "low",
    "event_index": 0,
    "confirmation_index": 2,
    "price": 99.48431564193378
  },
  {
    "kind": "low",
    "event_index": 10,
    "confirmation_index": 12,
    "price": 109.94717245212352
  },
  {
    "kind": "low",
    "event_index": 20,
    "confirmation_index": 22,
    "price": 121.51041751873485
  }
]
```

Showing 3 of 4 elements.

`kind`:

```json
"low"
```

`toleranceBps`:

```json
50
```

`minInliers`:

```json
3
```

`maxPoints`:

```json
12
```

`projectionIndex`:

```json
undefined
```

### Call

```ts
fitRobustTrendline(pivots, kind, toleranceBps, minInliers, maxPoints, projectionIndex)
```

### Returns

object with 10 fields: kind, slope_log_per_bar, intercept_log, projected_index, projected_price, inlier_count, outlier_count, median_absolute_residual_bps, …

```json
{
  "kind": "low",
  "slope_log_per_bar": 0.010000000000000009,
  "intercept_log": 4.6,
  "projected_index": 31,
  "projected_price": 135.63941440846523,
  "inlier_count": 3,
  "outlier_count": 1,
  "median_absolute_residual_bps": 0,
  "inlier_event_indexes": [0, 10, 20],
  "source_pivot_count": 4
}
```

## Other exports

`detectCausalPivots`. Every module additionally exports `run` as an alias of its primary
function, and a `meta` object carrying its catalog id, domain, family, shape and article URL.

## 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/robust-trendline-fitting/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/geometric-chart-patterns/pivots-and-levels/robust-trendline-fitting/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
