fintech-algorithms
Using a coding agent? Give it the skill: npx skills add IslamBaraka90/Fintech-Algorithms-Library What it does →

Trend-Context Filter

Install and import#

bash
npm install fintech-algorithms
ts
import { trendContextFilter } from "fintech-algorithms/price-action-and-candlesticks/candle-foundations/trend-context-filter";

Signature#

trendContextFilter(inputs)

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

Parameters#

NameTypeNotes
inputs{ prior_closes: number[]; prior_ranges: number[]; lookback: number; minimum_efficiency: number; minimum_move_scale: number }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#

executed 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
{
  "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#

trendContextFilter(inputs)

Returns#

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

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

This module also 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.

Diagrams#

Trend-Context Filter — system map

How it works#

This page states the contract — how to call it correctly. The article explains the concept: why it works, and where it breaks.

Read the article →

References#

The rest of the Candle Foundations family#