Skip to main content

math.percentile#

Linear-interpolation percentile of values.

SYNTAX#

function math.percentile(values: readonly number[], p: number): number

ARGUMENTS#

  • values (readonly number[]) — Sample of numeric values. Treated as a sample (not a population).
  • p (number) — Percentile in [0, 100]. Values outside the range are clamped.

RETURNS#

number — the interpolated percentile. Returns NaN for empty input; returns values[0] for a one-element input.

REMARKS#

O(n log n) due to the sort. For repeated percentile queries on the same window, sort once outside the loop and index directly.

EXAMPLE#

// 95th-percentile volume over the last 200 bars.
const recentVols = candles.slice(-200).map(c => c.volume);
const p95 = math.percentile(recentVols, 95);
plot('volP95', p95);

SEE ALSO#

math.clamp, math.round