Add unit tests for performance utilities - #43
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting
| export function computePerformanceSummary(timeline, period) { | ||
| const window = getTimelineWindow(timeline, period); | ||
| if (!window) { | ||
| return null; | ||
| } | ||
| const { startIndex, endIndex } = window; | ||
| if (startIndex > endIndex) { | ||
| return null; | ||
| } | ||
| const startValue = startIndex > 0 ? Number(timeline[startIndex - 1].value) || 0 : 0; | ||
| const endValue = Number(timeline[endIndex].value) || 0; | ||
| const { total: netFlows, positive: positiveFlows } = sumFlows(timeline, startIndex, endIndex); | ||
| const totalPnl = endValue - startValue - netFlows; | ||
| const invested = startValue + positiveFlows; | ||
| const percent = invested > 0 ? (totalPnl / invested) * 100 : startValue !== 0 ? (totalPnl / Math.abs(startValue)) * 100 : null; | ||
|
|
||
| const startDate = parseDate(timeline[startIndex].date); | ||
| const endDate = parseDate(timeline[endIndex].date); | ||
| let cagr = null; | ||
| if (startDate && endDate && endDate > startDate) { | ||
| const cashFlows = []; | ||
| cashFlows.push({ date: startDate, amount: -startValue }); | ||
| for (let i = startIndex; i <= endIndex; i += 1) { | ||
| const flow = Number(timeline[i].netFlows); | ||
| if (!Number.isFinite(flow) || flow === 0) { | ||
| continue; | ||
| } | ||
| const flowDate = parseDate(timeline[i].date); | ||
| if (!flowDate) { | ||
| continue; | ||
| } | ||
| cashFlows.push({ date: flowDate, amount: -flow }); | ||
| } | ||
| cashFlows.push({ date: endDate, amount: endValue }); |
There was a problem hiding this comment.
Start value calculated from wrong date
The performance summary uses timeline[startIndex - 1].value as the starting value while exposing timeline[startIndex].date as the period start and plotting points beginning at startIndex. As a result, the displayed "Starting value" and derived P&L/percent include gains or losses that happened before the visible range. For a sparse timeline, selecting “Last Month” will show a chart that starts on the first point inside that month but the metrics are still based on the previous earlier point, overstating returns for the chosen period. The starting value and cash‑flow baseline should line up with the same timeline index that defines the period, or the period should shift to include the baseline date.
Useful? React with 👍 / 👎.
7f0893f to
d45d743
Compare
c7f2145 to
c8636e3
Compare
Summary
Testing
https://chatgpt.com/codex/tasks/task_e_68dc4d0d3300832d89fe2ca52c655f09