Add performance cards and return index chart to portfolio

JA
7d ago
+760-35

Part of #4748

What: the portfolio page gets a Performance section (time-weighted return, annualised time-weighted, money-weighted return for the period, annualised money-weighted, volatility, max drawdown) and a Return index chart rebased to 100, both for the picked period and both from one InvestmentStatement#performance per request. Portfolio::Performance#flows_known? (cache version v15) lets the cards say "value return" instead of "time-weighted" when a valuation-tracked account's deposits cannot be removed.
Why: the Overview row shows what the portfolio is worth and how much that moved, but deposits are mixed into every figure. The returns engine already computes flow-adjusted returns; nothing on the page reads them.
Start reading: Portfolio::SectionRegistry#performance, #returns and #index_chart_series, then portfolios/_performance.html.erb; Performance#flows_known? last.
Key tests (test/controllers/portfolios_controller_test.rb, test/models/portfolio/section_registry_test.rb, test/models/portfolio/performance_test.rb, test/system/portfolios_test.rb):

  • "the performance section reads every figure from one computation"
  • "each return card prints its own figure"
  • "the return cards and index caption say time-weighted only when every account's flows are known"
  • "the index chart's first point shows no change"
  • "flows are known only when no account in the scope is valuation tracked"
  • system: "the index chart draws its levels as a line"

Behaviour change: preview features off: nothing changes. On: Performance and Return index appear between Overview and Portfolio value (KEYS = %w[kpis performance index_chart value_chart holdings accounts]). That is for a user with no saved order; with a saved order they are added at the end, as Reports does. A figure that cannot be computed reads "Not enough data", not 0%; a missing rate or a day counted as zero is stated in a DS::Alert. The index chart is hidden below two points. Portfolio::Performance::CACHE_VERSION moves v14 → v15, so cached return metrics are recomputed once. No JavaScript change. No migration.

Size: +276 / −9 outside tests, across 6 files (Ruby, ERB, en/de YAML).
Fork pre-flight: jaysbeekay/sure#996 (same commits).

Screenshots

Same seeded family and period as the KPI PR below (90D; the annualised cards read "Not enough data" because the period is under a year, as their hint says).

Before (KPI row)After (this PR)
Desktop 1400pxbefore desktopafter desktop
Phone 390pxbefore phoneafter phone
Evidence

Ported onto the returns stack below and the holdings section.

  • Performance#flows_known? reads return_scopes.values.none?(&:valuation_tracked?) inside compute; both exist on the returns stack, so the method body is unchanged. CACHE_VERSION goes to v15, above the stack's v14, and the cache test adds refute_equal "v14" ("v14 entries were cached without flows_known").
  • Section order and the saved-order tests include holdings. In the fixture family the index chart is hidden (no account has two balance days in the period), so the rendered order is kpis performance value_chart holdings accounts.
  • The page's query ceiling goes from 72 to 78: measured 75 with the performance and index sections, which share one Portfolio::Performance. "does not grow with holdings" still holds.

Blast radius. InvestmentStatement#performance gets its first caller, pinned with expects(:performance).with(period:).once. _kpi_card gains its second caller (rate:), unchanged. time_series_chart_controller.js gains a caller with non-Money values; Series passes them through, and the system test shows the line drawn. Performance#flows_known? is new; index_series's comment now says only recorded flows are removed.

Tests on this branch. Portfolio controller, helper, test/models/portfolio/ and the statement tests: 640 runs, 0 failures. Portfolio and Reports system tests: 19 runs, 0 failures.

Full suite on this branch's head cb035c55e: 19,723 runs, 0 failures, 8 errors, 9 skips. The 8 errors are ReleaseNotesWorkflowTest, which needs node (not in the test image).
Lint: RuboCop 3,664 files, no offenses. erb_lint (862 files): no errors. Brakeman: 0 warnings. Biome lint: no errors; no JavaScript changed.
Checked against AGENTS.md: DS primitives (DS::Alert), functional tokens, icon helper, t() for every string, logic in the registry rather than the views, Minitest only.


Summary by cubic

Adds Performance and Return index sections to the portfolio page, showing what the portfolio returned over the picked period rather than what it is worth. With preview features off, the page is unchanged.

  • Each section reads its figures from a single InvestmentStatement#performance per request: time- and money-weighted returns (with annualised variants), volatility, max drawdown, and a flow-adjusted return index rebased to 100.
  • Figures that cannot be computed read "Not enough data"; missing exchange rates and days counted as zero returns are stated in a DS::Alert above the cards.
  • When any account in scope is valuation-tracked or holds a single balance day, its flows are unknown, so the cards and chart caption say "value return" instead of "time-weighted".
  • The index chart starts from a base of 100 the day before its first return, so the first day's move is measured from it; the chart hides when the period has fewer than two points to draw.
  • The chart tooltip's trend arrow now works for the index's non-money levels, so a rising line shows a rise rather than a flat icon on every point.
  • Both money-weighted cards note when flows change sign more than once, since more than one rate can fit them.
  • CACHE_VERSION moves to v16, so cached return metrics are recomputed once.

Screenshots

Same seeded family and period as the KPI PR below (90D; the annualised cards read "Not enough data" because the period is under a year, as their hint says).

Before (KPI row)After (this PR)
Desktop 1400pxbefore desktopafter desktop
Phone 390pxbefore phoneafter phone

Summary by CodeRabbit

  • New Features
    • Portfolio pages now include time- and money-weighted returns, annualized returns, volatility, and maximum drawdown.
    • A return index chart shows portfolio performance over time when enough data is available, with explanations that reflect whether deposit information is complete.
  • Improvements
    • Performance details explain when figures can’t be measured or are withheld, and show the available return figures for each metric.
    • Return labels clarify when figures reflect value changes because deposit information is incomplete.
    • Rate-based values display as percentages when monetary values aren’t available.
  • Bug Fixes
    • Chart trend indicators now correctly show whether values are rising, falling, or unchanged.