The series points, ascending by time.
The direction, signed USD delta, and percent change of the last move.
describeTrend([{ t: 1, usd: 20 }, { t: 2, usd: 22 }]);
// => { direction: "up", deltaUsd: 2, pctChange: 10 }
export function describeTrend(points: readonly PricePoint[]): PriceTrend {
if (points.length < 2) {
return { direction: 'flat', deltaUsd: 0, pctChange: 0 };
}
const last = points[points.length - 1].usd;
const prev = points[points.length - 2].usd;
const deltaUsd = round2(last - prev);
const pctChange = prev !== 0 ? (deltaUsd / prev) * 100 : 0;
const direction: TrendDirection = deltaUsd > 0 ? 'up' : deltaUsd < 0 ? 'down' : 'flat';
return { direction, deltaUsd, pctChange };
}
Summarize the latest price move for a series from its last two points. Returns a flat, zero-delta trend when there are fewer than two points.