Skip to content

@livefolio/sdk / rsi

Function: rsi()

rsi(series, period): Series

Defined in: features/indicators/rsi.ts:63

Computes the Relative Strength Index (RSI) using Wilder's smoothing method.

Math definition:

changes[i] = series[i] - series[i-1]

// Seed from simple averages of the first `period` changes:
avgGain[0] = mean(max(changes[0..period-1], 0))
avgLoss[0] = mean(max(-changes[0..period-1], 0))

// Wilder's smoothing for subsequent periods:
avgGain[i] = (avgGain[i-1] * (period-1) + gain[i]) / period
avgLoss[i] = (avgLoss[i-1] * (period-1) + loss[i]) / period

RS[i]  = avgGain[i] / avgLoss[i]
RSI[i] = 100 - 100 / (1 + RS[i])

Special case: when avgLoss === 0, RSI is clamped to 100 (infinite RS means no losing periods in the window).

Warmup: requires period + 1 input bars to produce the first RSI value (one extra bar for the initial change calculation). The first output point corresponds to input index period. The output array is shorter than the input (no undefined placeholders).

Edge cases:

  • period <= 0 — throws Error.
  • series.length < period + 1 — returns [].
  • Flat price series (all changes = 0) — returns RSI values of 100 because avgLoss stays 0.

Parameters

ParameterTypeDescription
seriesSeriesInput price series sorted in ascending timestamp order.
periodnumberLookback window in bars for Wilder's smoothing. Must be a positive integer; 14 is the conventional default.

Returns

Series

A Series of length max(0, series.length - period). Each point's timestamp t is taken from the corresponding input bar. Values are in the range [0, 100].

Example

ts
import { rsi } from '@livefolio/sdk';

// Minimal example: 5 bars, period 3 → 2 RSI values
const prices = [
  { t: new Date('2023-01-02'), v: 100 },
  { t: new Date('2023-01-03'), v: 102 },
  { t: new Date('2023-01-04'), v: 101 },
  { t: new Date('2023-01-05'), v: 105 },
  { t: new Date('2023-01-06'), v: 104 },
];

const result = rsi(prices, 3);
// result.length === 2
// result[0].t => new Date('2023-01-05')
// result[1].t => new Date('2023-01-06')
// values are in [0, 100]

Released under the MIT License.