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

True Range

Install and import#

bash
npm install fintech-algorithms
ts
import { trueRange } from "fintech-algorithms/technical-indicators/volatility-and-channels/true-range";

Signature#

trueRange(high, low, close)

The greater of today's range, the gap up from yesterday's close, and the gap down from it. Using the plain high−low range instead understates volatility on every gap.

Parameters#

NameTypeNotes
highnumber[]Per-bar high prices, chronological.
lownumber[]Per-bar low prices, chronological.
closenumber[]Per-bar closing prices, chronological.

Returns#

Record<string, (number | null)[]> · length same-as-input

Parallel series including true_range and, for each bar, which of the three candidates won (driver) — which is what makes a surprising ATR diagnosable.

Warm-up#

The first 0 positions are high - low. The first bar has no prior close, so this package publishes high minus low while leaving the two previous-close gap components unavailable.

Errors#

  • When the input series are not all the same length — throws RangeError

Complexity: time O(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#

high
[101.08, 101.6243955, 102.13058177, 102.56507653, 102.90321818, 103.1320792]

Showing 6 of 240 elements.

low
[98.92, 99.39457765, 99.83203556, 100.19996391, 100.47473988, 100.64442473]

Showing 6 of 240 elements.

close
[100, 100.50948657, 100.98130867, 101.38252022, 101.68897903, 101.88825197]

Showing 6 of 240 elements.

Call#

trueRange(high, low, close)

Returns#

object with 5 fields: true_range, high_low, high_gap, low_gap, driver

{
  "true_range": [
    2.1599999999999966,
    2.2298178500000034,
    2.298546210000012,
    2.365112620000005,
    2.4284782999999948,
    2.4876544700000096
  ],
  "high_low": [
    2.1599999999999966,
    2.2298178500000034,
    2.298546210000012,
    2.365112620000005,
    2.4284782999999948,
    2.4876544700000096
  ],
  "high_gap": [
    null,
    1.6243955000000057,
    1.6210951999999992,
    1.5837678599999947,
    1.5206979599999926,
    1.4431001700000081
  ],
  "low_gap": [
    null,
    0.6054223499999978,
    0.6774510100000128,
    0.7813447600000103,
    0.9077803400000022,
    1.0445543000000015
  ],
  "driver": ["high-low", "high-low", "high-low", "high-low", "high-low", "high-low"]
}

Diagrams#

True Range — canonical trace
True Range — failure boundary
True Range — mechanism map
True Range — memory comparison

Calculation flow#

True Range calculation flow
flowchart LR
    A["Finalized H, L, C"] --> B{"First row?"}
    B -- "yes" --> C["TR = H - L"]
    B -- "no" --> D["Compute H-L"]
    B -- "no" --> E["Compute |H-prev C|"]
    B -- "no" --> F["Compute |L-prev C|"]
    D --> G["Stable maximum and driver"]
    E --> G
    F --> G
    C --> H["Aligned output"]
    G --> H

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#

  • True Range — TA-Lib
  • ta_TRANGE.c — TA-Lib
  • Average True Range — TA-Lib
  • Public evidence boundary

The rest of the Volatility and Channels family#