@livefolio/sdk / sma
Function: sma()
sma(
series,period):Series
Defined in: features/indicators/sma.ts:43
Computes a Simple Moving Average (SMA) over a price series.
Math definition:
SMA[i] = (series[i] + series[i-1] + ... + series[i-period+1]) / periodWarmup: the first output point corresponds to index period - 1 in the input. Inputs series[0] through series[period - 2] have no SMA value and are excluded from the output entirely (no undefined placeholders; the output array is shorter than the input).
Edge cases:
period <= 0— throwsError.series.length < period— returns[](not enough data for even one window).series.length === period— returns a single-pointSeries.
Parameters
| Parameter | Type | Description |
|---|---|---|
series | Series | Input price series sorted in ascending timestamp order. |
period | number | Window size in bars. Must be a positive integer. |
Returns
A Series of length max(0, series.length - period + 1). Each point's timestamp t is taken from the last bar in its window (series[i + period - 1]).
Example
ts
import { sma } from '@livefolio/sdk';
const prices = [
{ t: new Date('2023-01-02'), v: 100 },
{ t: new Date('2023-01-03'), v: 110 },
{ t: new Date('2023-01-04'), v: 120 },
{ t: new Date('2023-01-05'), v: 130 },
];
const result = sma(prices, 3);
// result.length === 2
// result[0] => { t: new Date('2023-01-04'), v: 110 } // (100+110+120)/3
// result[1] => { t: new Date('2023-01-05'), v: 120 } // (110+120+130)/3