Group a portfolio's income by month, with its fees and fee ratio

Closed
JA
21d ago
+6312-36

Part of #4078 (part 3, returns engine)
Part of #3494

Fork pre-flight: jaysbeekay/sure#453 (pull/453), same branch, same commit. Its CI and review history are there. The earlier evidence for this code is on jaysbeekay/sure#377, the pre-flight it supersedes.

Updated 2026-11-05. #4214's head is merged down, bringing #4197's bound-value marker fix (11bb8a1bd, 0e334559a) and upstream main (529d7de76). No change to this PR's own files. To review this slice alone: up/portfolio-performance-v2...up/portfolio-income-v2. Merge bottom-up; main is merged down after each one. Full suite on 2427131e7: 14,853 runs, 0 failures, 0 errors.

Stacked on the performance PR, #4214 (up/portfolio-performance-v2), which stacks on the drivers and return scope PRs. Until those merge, GitHub shows their commits here as well. This PR's own commits are the five after 7fc5ba1e0:

  • 85e58e9b6 Group a portfolio's income by month, with its fees and fee ratio
  • 63474c0f5 Add the fee fixture back beside the fee ratio tests
  • c0f8a61a8 Read the income's missing-rate flag from the daily returns
  • 63f73b518 Emit each income month as an ISO 8601 string
  • 62281704a Correct two comments on the income buckets and fee ratio

The income by security PR stacks on this one.

Problem

The daily rows carry each day's dividend and interest income and its fees, but nothing reports them over a period: how much a portfolio paid out each month, what it was charged, and what that charge is as a share of the money invested.

Change

app/models/portfolio/income.rb (new, 126 lines). It regroups the daily rows' income and fees by calendar month and re-derives nothing. What counts as income is the classifier's answer, already converted as the balance took it: into the account's currency on its own date (the entry's own rate first), then into the family's currency at the previous day's rate. That includes a Trade-shaped dividend since #1311, or a Transaction-shaped one. So the months add up to the drivers' income.

  • Months. A month that paid nothing is absent. A month whose payouts and reversals net to zero or below is kept, so the buckets always add up to the total.
  • Fees. Reported apart, as a positive magnitude, not netted out of income.
  • Fee ratio. Fees over the period's average daily value, so it does not shrink as the portfolio grows. It is nil when there was no value.
  • Cached shape. Each bucket's month is an ISO 8601 string. A JSON-serialising cache store would turn a Date into a string anyway, so a caller would otherwise read a different type depending on the store.

app/models/portfolio/performance.rb (+23):

  • #income. It is a cache entry of its own, computed only when asked, so a caller that reads only the returns never pays for it.
  • The missing-rate flag. It reads the flag from the daily returns, so reading the income never runs the returns' computation.
  • Withholding. The fee ratio is withheld when a rate is missing. The money figures are not.

Updated 2026-10-14. b786df777 merges #4214 at 88fdbd232, which brings #4197's, #4198's, #4212's and #4213's review fixes. Portfolio::Income reads the rows' income, so the total now includes excluded income entries and converts foreign income as the balance did. This slice's own files are unchanged.

Updated 2026-10-15. 52031186f merges #4214 at fce8b6868 (with #4197's and #4198's review fixes below it). No change to this PR's own files.

Validation

On 62281704a, stacked on 7fc5ba1e0:

  • Full suite (bin/rails test): 11,957 runs, 54,994 assertions, 0 failures, 0 errors, 34 skips. Run locally on PostgreSQL 16.
  • 2026-10-14: the head moved to d835d5899 only by merging the bind fix down the stack, and this PR's own files are unchanged. The portfolio model tests pass (198 runs, 0 failures) and CI is green; the stack tip 3f622bb8f, which contains this head, ran the full suite: 11,978 runs, 0 failures, 0 errors, 34 skips.
  • On b786df777 (2026-10-14): portfolio model tests 246 runs, 0 failures. Brakeman reports 0 warnings.
  • On 52031186f (2026-10-15): portfolio model and investment statement tests 253 runs, 0 failures. Full suite: see #4218 (12,001 runs, 0 failures, 0 errors).
  • Lint and security: bin/rubocop -f github on the changed files reports no offenses, and bin/brakeman --no-pager -q reports 0 warnings. No ERB, JS or system-test surface.

Tests

test/models/portfolio/income_test.rb and test/models/portfolio/performance_test.rb, on 62281704a: 51 runs, 160 assertions, 0 failures.

Fail-first:

  • "reading the income alone computes none of the returns" failed with an unexpected Portfolio::Performance#compute invocation.
  • "the cached payload's months survive a JSON round trip unchanged" failed with a Date where the string belonged.

Mutations. Each was run on the fork pre-flight, then restored. All were caught:

MutationTests that catch it
empty months kept6
a net-zero month dropped1
a negative month dropped2
fees netted out of income1
fee ratio over the closing value2
fee ratio 0 instead of nil with no value1
buckets unsorted2
fee ratio not withheld on a missing rate1
the missing-rate flag not passed to income1
a Date month in the cached hash1

Re-run on this branch:

  • fee ratio not withheld on a missing rate: 2 failures;
  • a Date month: 1;
  • fee ratio 0 instead of nil: 1;
  • income reading the returns' flag instead of the daily returns': 1.

The code is byte-identical to the fork pre-flight it supersedes.

Migration notes

None. No schema change.

Summary by CodeRabbit

  • New Features
    • Added portfolio performance reporting with time-weighted and money-weighted returns, annualized returns, volatility, maximum drawdown, and a rebased performance index.
    • See performance changes broken down by external cash flows, portfolio composition, income, fees, market movement, revaluations, and currency effects.
    • Review monthly income totals, fees, and fee ratios.
    • Reports indicate when metrics are unavailable or incomplete due to limited account history, missing exchange rates, or unvalued activity.
    • Return calculations respect account activity cutoff dates and distinguish external cash flows from changes in portfolio composition.