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

JA
7d ago
+773-16

Part of #121 (IP2, the performance engine, under epic #118). Part of we-promise/sure#4078 and we-promise/sure#3494.

Fork pre-flight for we-promise/sure#5093 (same commits), which replaces we-promise/sure#4217.

Replaces jaysbeekay/sure#453 (same changes, rebased and native-stacked).

Stacked on jaysbeekay/sure#711 in a fork native stack.

Rebased 2026-11-15 onto main at dbeebf14c. Its 6 own commits replayed onto the slice below unchanged, plus e82d3d780, which counts a commission folded into a trade's amount as a fee (see Commissions on trades). test/models/portfolio and test/models/balance at this slice: 373 runs, 0 failures. Full suite at the top of the stack: 16,441 runs, 0 failures (8 ReleaseNotesWorkflowTest errors: no node in the test image). Rubocop and Brakeman clean.

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 we-promise/sure#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. A day before the portfolio's first balance counts as zero in the average (pinned since 6ab406c87: "a day before the portfolio's first balance counts as zero in the fee ratio", which fails against a mean over valued days only, 990 for 660).
  • 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 we-promise/sure#4214 at 88fdbd232, which brings we-promise/sure#4197's, we-promise/sure#4198's, we-promise/sure#4212's and we-promise/sure#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 we-promise/sure#4214 at fce8b6868 (with we-promise/sure#4197's and we-promise/sure#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 we-promise/sure#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.

Commissions on trades (added 2026-11-15)

A commission recorded on a buy or sell (trades.fee) and folded into its entry amount -- amount = qty × price + fee, as the trade form, SnapTrade and IBKR write it -- was booked by the balance calculator as a market loss, so fees and the fee ratio read 0% for a brokerage that charges per trade. Portfolio::DailyReturns now moves such a commission from market to fees, converted as the trade's own amount is (e82d3d780). The change, the returns and unexplained do not move; only the split does. A fee kept out of the amount was never charged to the balance and is not counted. Fees include commissions recorded on trades; there is no UI copy in this PR, so the model comments say so.

  • Tests, each seen to fail first: a buy of 100 @ 50 with a fee of 5 gives fees 5 and market 0 (before: 0 and −5) and still reconciles; a sell's fee lands in fees; a fee kept out of the amount stays uncounted; no fee moves nothing; a EUR trade's 5 EUR fee is 5.5 USD at the previous day's 1.1.
  • Mutations, both caught: dropping the amount-includes-fee check; moving the fee without adding it back to market.
  • Known limit: on a valuation day the balance books the day's move as a revaluation, not as market, so a commission that day leaves market up by the fee and the loss in revaluations. The period still reconciles.

Migration notes

None. No schema change.