# Trend-Context Filter

`D06-F01-A05` · Price Action and Candlesticks → Candle Foundations · archetype `record-transform` · difficulty 3/5 · verification **contract**

Full page: https://docs.thefintechbuilder.com/price-action-and-candlesticks/candle-foundations/trend-context-filter/
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 { trendContextFilter } from "fintech-algorithms/price-action-and-candlesticks/candle-foundations/trend-context-filter";
```

## Signature

```ts
trendContextFilter(inputs)
```

Classifies prior closes as an uptrend, downtrend, or sideways context from normalized net movement and path efficiency.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `inputs` | `{ prior_closes: number[]; prior_ranges: number[]; lookback: number; minimum_efficiency: number; minimum_move_scale: number }` | yes | Record containing aligned `prior_closes`, `prior_ranges`, `lookback`, `minimum_efficiency`, and `minimum_move_scale` fields. |

## Returns

`{ state, trend_context, history_count, net_move, path_move, efficiency, range_scale, normalized_move }`

One readiness record. `state` is `warmup` until `lookback` observations exist, `zero-scale` when the range scale is zero, and `ready` otherwise; this is not a positional warmup series.

## Warm-up

The first `lookback prior observations` positions are `state: warmup`. The function returns one readiness record until the configured lookback is available; it does not emit a positional null prefix.

## Errors

- When lookback is below 3, arrays are misaligned, or thresholds are outside their allowed ranges — throws

## Complexity

Time `O(n log n)`, 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

`inputs`:

```json
{
  "prior_closes": [100, 99, 98, 97, 96, 95],
  "prior_ranges": [2, 2, 2, 2, 2, 2],
  "lookback": 8,
  "minimum_efficiency": 0.6,
  "minimum_move_scale": 2
}
```

### Call

```ts
trendContextFilter(inputs)
```

### Returns

object with 8 fields: state, trend_context, history_count, net_move, path_move, efficiency, range_scale, normalized_move

```json
{
  "state": "ready",
  "trend_context": "downtrend",
  "history_count": 8,
  "net_move": -7,
  "path_move": 7,
  "efficiency": 1,
  "range_scale": 2,
  "normalized_move": -3.5
}
```

## Other exports

`calculate`, `shadowToBodyRatio`, `gapClassification`, `doji`, `dragonflyDoji`, `gravestoneDoji`, `marubozu`, `spinningTop`, `hammer`, `hangingMan`, `invertedHammer`, `shootingStar`, `bullishEngulfing`, `bearishEngulfing`, `bullishHarami`, `bearishHarami`, `piercingLine`, `darkCloudCover`, `tweezerTop`, `tweezerBottom`. 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: **contract**.

The module loads, the entry point is callable and its declared signature matches the compiled code. The example below is real captured output, but no independently published figure asserts the numbers.

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

Generated from the docs.json payload shipped inside fintech-algorithms@0.12.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/price-action-and-candlesticks/candle-foundations/trend-context-filter/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/price-action-and-candlesticks/candle-foundations/trend-context-filter/impl.ts
- Package on npm: https://www.npmjs.com/package/fintech-algorithms
- Domain index for agents: https://docs.thefintechbuilder.com/price-action-and-candlesticks/llms.txt
