1Branch0Tags
typescript
export type PriceChangeResult = {
previous: number;
percent: number;
};
export function computePriceChange(
values: number[],
lookback = 24
): PriceChangeResult | null {
if (values.length < 2) return null;
const current = values[values.length - 1];
const index = Math.max(0, values.length - 1 - lookback);
const previous = values[index];
if (previous === 0) return null;
const percent = ((current - previous) / previous) * 100;
return {
previous,
percent,
};
}