Utility Functions
Mathematical utility functions that support technical analysis calculations. These are the building blocks used by many indicators and are also useful for custom analysis.
Average / Mean
Calculates the arithmetic mean of a set of values
Interactive Demo
Code Example
import { getAverage } from 'trading-signals';
const values = [10, 20, 30, 40, 50];
const avg = getAverage(values);
console.log(avg); // 30Median
Finds the middle value in a sorted dataset
Interactive Demo
Code Example
import { getMedian } from 'trading-signals';
const values = [7, 31, 47, 75, 87];
const median = getMedian(values);
console.log(median); // 47Standard Deviation
Measures the amount of variation or dispersion in a dataset
Interactive Demo
Code Example
import { getStandardDeviation } from 'trading-signals';
const values = [10, 12, 14, 16, 18];
const stdDev = getStandardDeviation(values);
console.log(stdDev);Maximum & Minimum
Finds the highest and lowest values in a dataset
Interactive Demo
Code Example
import { getMaximum, getMinimum } from 'trading-signals';
const values = [25, 50, 75, 100, 125];
console.log(getMaximum(values)); // 125
console.log(getMinimum(values)); // 25Percentage Change
Calculate the percentage change between a starting and a final value.
Interactive Demo
Code Example
import { getPercentageChange } from 'trading-signals';
// Price moved from 100 to 150 → +50%
const change = getPercentageChange(100, 150);
console.log(change); // 50Add Percentage Change
Apply a percentage change to a starting value to get the final value.
Interactive Demo
Code Example
import { addPercentageChange } from 'trading-signals';
// Apply a 50% gain to a starting value of 100
const target = addPercentageChange(100, 50);
console.log(target); // 150Share of Total
Calculate what percentage your share represents of a total.
Interactive Demo
Code Example
import { getShare } from 'trading-signals';
// 40 out of 100 → 40%
const share = getShare(40, 100);
console.log(share); // 40getQuartile
Returns a single quartile (Q1, median, or Q3) from a dataset
Signature
getQuartile(values: number[], q: 0.25 | 0.5 | 0.75): numberDetails
Pass `0.25` for the first quartile (Q1, 25th percentile), `0.5` for the median (Q2), or `0.75` for the third quartile (Q3, 75th percentile). Useful for measuring spread and detecting outliers (e.g. computing the interquartile range as `Q3 − Q1`).
getGrid
Generates price grid levels for grid trading strategies
Signature
getGrid({ lower, upper, levels, spacing, tickSize? }): number[]
// spacing: 'arithmetic' | 'geometric'Details
Splits a price range into `levels` evenly spaced points between `lower` and `upper`. Choose `arithmetic` for equal price steps or `geometric` for equal percentage steps. An optional `tickSize` rounds each level to a tradable increment. Used by grid trading strategies that place buy/sell orders at fixed intervals.
getStreaks
Returns the lengths and percentage moves of continuous up- or down-streaks in a price series
Signature
getStreaks(prices: number[], keepSide: 'up' | 'down'): Streak[]
// type Streak = { length: number; percentage: number }Details
Walks the price series and groups consecutive moves in the same direction. Pass `up` to keep uptrends, `down` for downtrends. Each returned `Streak` reports the number of consecutive moves and the total percentage change across the streak. Useful for momentum analysis and indicators that need streak length as input (e.g. Connors RSI).
isMonday / isTuesday / …
Boolean helpers that test whether a date falls on a given weekday in a specific timezone
Signature
isMonday(timezone: string, date?: Date): boolean
// also: isTuesday, isWednesday, …, isSundayDetails
Each helper takes an IANA timezone identifier (e.g. `"America/New_York"`, `"Europe/London"`) and an optional `Date` (defaults to now). Useful for filtering strategies by trading session or analysing day-of-week effects without pulling in a heavy date library.
pushUpdate
Appends or replaces the last value in a bounded ring buffer
Signature
pushUpdate<T>(array: T[], replace: boolean, item: T, maxLength: number): T | null | undefinedDetails
Helper used when implementing custom indicators. Pass `replace: false` to append a new value, `replace: true` to overwrite the most recent one (for streaming/live candles). When the array would exceed `maxLength`, the oldest element is shifted out and returned; otherwise the function returns `null`.