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

Stochastic Oscillator

Install and import#

bash
npm install fintech-algorithms
ts
import { stochastic } from "fintech-algorithms/technical-indicators/momentum/stochastic-oscillator";

Signature#

stochastic(high, low, close, kp, sk, sd)

Where the close sits within the high–low range of the lookback, expressed as 0–100, then smoothed twice.

Parameters#

NameTypeNotes
highnumber[]Per-bar high prices, chronological.
lownumber[]Per-bar low prices, chronological.
closenumber[]Per-bar closing prices, chronological.
kpnumber%K lookback: the window over which the high–low range is measured.
min: 1 · integer: true
sknumberSmoothing applied to raw %K to produce slow %K.
min: 1 · integer: true
sdnumberSmoothing applied to slow %K to produce %D.
min: 1 · integer: true

Returns#

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

Parallel series: fast_k, slow_k and slow_d.

Warm-up#

The first kp − 1 for fast %K, then sk − 1 and sd − 1 more for each smoothing stage positions are null.

Errors#

  • When any period is < 1 or is not an integer — throws RangeError
  • When the input series are not all the same length — throws RangeError

Complexity: time O(n × kp), 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, 101.992465, 103.28276, 104.655405, 105.856544, 106.661472]

Showing 6 of 120 elements.

low
[99.08, 99.149664, 100.233798, 101.654978, 103.180913, 104.534685]

Showing 6 of 120 elements.

close
[100, 100.999214, 102.324548, 103.755998, 105.030823, 105.911843]

Showing 6 of 120 elements.

Call#

stochastic(high, low, close, kp, sk, sd)

Returns#

object with 3 fields: fast_k, slow_k, slow_d

{
  "fast_k": [null, null, null, null, null, null],
  "slow_k": [null, null, null, null, null, null],
  "slow_d": [null, null, null, null, null, null]
}

Other exports#

This module also exports rsi, stochasticRsi, williamsR, cci, ultimateOscillator, tsi, connorsRsi. 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#

Stochastic Oscillator — stochastic oscillator mechanism

Calculation flow#

Calculation flow
flowchart LR
  A["Finalized finalized high, low, close"] --> B["Validate order, alignment, and finite values"]
  B --> C["Trailing range"]
  C --> D["Fast %K"]
  D --> E["Slow %K SMA"]
  E --> F{"History and denominator valid?"}
  F -- "No · short history" --> G["warming-up + reason"]
  F -- "No · no finite scale" --> H["undefined + reason"]
  F -- "Yes" --> I["Slow %D SMA"]
  I --> J["ready + aligned component trace"]

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#

  • Stochastic Oscillator Slow (STOCH) — TA-Lib
  • TA-Lib Technical Analysis Documentation — TA-Lib
  • Claim-role ledger
  • Evidence boundary

The rest of the Momentum family#