@livefolio/sdk / seriesAt
Function: seriesAt()
seriesAt(
series,t):number|undefined
Defined in: features/series-utils.ts:93
Looks up the value at or immediately before timestamp t in a sorted Series.
Uses binary search to find the largest index i where series[i].t <= t. This is the standard "as-of" lookup used throughout the strategy loop to read the most recent indicator value available on a given session date without peeking into the future.
Parameters
| Parameter | Type | Description |
|---|---|---|
series | Series | A Series sorted in ascending timestamp order. Behaviour is undefined for unsorted input. |
t | Date | The target date. May fall between two data points or exactly on one. |
Returns
number | undefined
The v value of the last data point at or before t, or undefined if the series is empty or every point comes after t.
Example
ts
import { seriesAt } from '@livefolio/sdk';
const series = [
{ t: new Date('2023-01-02'), v: 380.0 },
{ t: new Date('2023-01-03'), v: 385.0 },
{ t: new Date('2023-01-04'), v: 390.0 },
];
seriesAt(series, new Date('2023-01-03')); // => 385.0 (exact match)
seriesAt(series, new Date('2023-01-03T12:00:00Z')); // => 385.0 (between points)
seriesAt(series, new Date('2023-01-01')); // => undefined (before all points)