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

Average True Range (ATR)

Install and import#

bash
npm install fintech-algorithms
ts
import { averageTrueRange } from "fintech-algorithms/technical-indicators/volatility-and-channels/atr";

Signature#

averageTrueRange(high, low, close, p)

Wilder-smoothed true range. Most often used for position sizing and stop placement rather than as a signal — it says how far price typically moves, not which way.

Parameters#

NameTypeNotes
highnumber[]Per-bar high prices, chronological.
lownumber[]Per-bar low prices, chronological.
closenumber[]Per-bar closing prices, chronological.
pnumberWilder smoothing period. Note this is 1/p decay, not the 2/(p+1) of a standard EMA; substituting one changes every published ATR value.
min: 1 · integer: true

Returns#

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

Parallel series: true_range and atr.

Warm-up#

The first p - 1 positions are null. Applies to atr only; true_range is defined from the first bar. The first ATR is the mean of the first p true ranges, so it lands at index p - 1 and Wilder smoothing continues from there.

Errors#

  • When p < 1 or is not an integer — throws RangeError
  • 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.

p
14

Call#

averageTrueRange(high, low, close, p)

Returns#

object with 2 fields: true_range, atr

{
  "true_range": [
    2.1599999999999966,
    2.2298178500000034,
    2.298546210000012,
    2.365112620000005,
    2.4284782999999948,
    2.4876544700000096
  ],
  "atr": [null, null, null, null, null, null]
}

Other exports#

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

Average True Range (ATR) — canonical trace
Average True Range (ATR) — failure boundary
Average True Range (ATR) — mechanism map
Average True Range (ATR) — memory comparison

Calculation flow#

Average True Range (ATR) calculation flow
flowchart LR
    A["True Range stream"] --> B{"n values available?"}
    B -- "no" --> C["ATR = null"]
    B -- "first complete seed" --> D["Arithmetic mean of first n TR"]
    B -- "after seed" --> E["Previous ATR + (TR - previous ATR) / n"]
    D --> F["Directionless price-unit scale"]
    E --> F
    G["Historical revision"] --> H["Recompute every later recursive state"]

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#

  • New Concepts in Technical Trading Systems — J. Welles Wilder Jr.
  • Average True Range — TA-Lib
  • ta_ATR.c — TA-Lib
  • True Range — TA-Lib
  • Public evidence boundary

The rest of the Volatility and Channels family#