# Least-Squares Moving Average (LSMA)

`D07-F01-A16` · Technical Indicators → Trend Smoothing · archetype `series-transform` · difficulty 2/5 · verification **verified**

Full page: https://docs.thefintechbuilder.com/technical-indicators/trend-smoothing/least-squares-moving-average-lsma/
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 { leastSquaresMovingAverageLsma } from "fintech-algorithms/technical-indicators/trend-smoothing/least-squares-moving-average-lsma";
```

## Signature

```ts
leastSquaresMovingAverageLsma(input)
```

Fits an ordinary least-squares line to each `period`-bar window of close and reports the value of that line at the window's last bar, so the curve follows the local trend rather than its average.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `input` | `TopicInput` | yes | `bars` is a non-empty array of OHLCV records with strictly increasing `timestamp`, finite `open`, `high`, `low`, `close` and non-negative `volume`, all sharing one adjustment basis. From `parameters` this topic reads only `period`, an integer of at least 2, default 14, which is the regression window length. |

## Returns

`TopicResult`

`series` and `latest` carry a single key, `value`, the fitted endpoint `intercept + slope * (period - 1)`. The first `period - 1` entries are null while the window fills, so with the default period `ready_at` is 13.

## Warm-up

The first `period - 1 bars (13 at the default period of 14)` positions are `null`. A regression needs a full `period`-bar window, so the first fit lands at index `period - 1` and `ready_at` is that index. A window with zero variance in its bar index is given a slope of 0 rather than dividing by zero.

## Errors

- When `parameters.period` is not an integer of at least 2 — throws Error
- When `bars` is empty or a bar violates the OHLCV contract — throws Error

## Complexity

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

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

`input`:

```json
{
  "bars": [
    {
      "timestamp": "2024-01-02",
      "basis": "synthetic-unadjusted",
      "open": 100,
      "high": 101.45,
      "low": 98.695,
      "close": 100,
      "volume": 750000,
      "benchmark": 200
    },
    {
      "timestamp": "2024-01-03",
      "basis": "synthetic-unadjusted",
      "open": 101.49111452,
      "high": 103.38381693,
      "low": 100.05480022,
      "close": 101.78791214,
      "volume": 795117,
      "benchmark": 200.56326135
    },
    {
      "timestamp": "2024-01-04",
      "basis": "synthetic-unadjusted",
      "open": 102.45519048,
      "high": 104.6701838,
      "low": 100.91147007,
      "close": 102.9549389,
      "volume": 840234,
      "benchmark": 201.11020913
    }
  ],
  "parameters": {}
}
```

### Call

```ts
leastSquaresMovingAverageLsma(input)
```

### Returns

object with 9 fields: topic_id, title, state, ready, ready_at, series, latest, parameters, …

```json
{
  "topic_id": "D07-F01-A16",
  "title": "Least-Squares Moving Average (LSMA)",
  "state": "calculated",
  "ready": true,
  "ready_at": 13,
  "series": {
    "value": [null, null, null, null, null, null]
  },
  "latest": {
    "value": 101.85696110114286
  },
  "parameters": {},
  "diagnostics": {
    "causal": true,
    "input_count": 96
  }
}
```

## Verification and provenance

Tier: **verified** (via E).

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.0.
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-smoothing/least-squares-moving-average-lsma/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/technical-indicators/trend-smoothing/least-squares-moving-average-lsma/impl.ts
- Package on npm: https://www.npmjs.com/package/fintech-algorithms
- Domain index for agents: https://docs.thefintechbuilder.com/technical-indicators/llms.txt
