Break a period's change in value into its drivers

Closed
JA
21d ago
+3786-36

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

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

Updated 2026-11-05. #4198'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-journal-flows-v2...up/portfolio-drivers-v2. Merge bottom-up; main is merged down after each one. Full suite on 81689838d: 14,770 runs, 0 failures, 0 errors.

Stacked on the journal flows PR, #4198 (up/portfolio-journal-flows-v2), which is stacked on the daily returns PR, #4197. Until those merge, GitHub shows their commits here as well (ten, counted on d28a35d4a). This PR's own commits are the four after d28a35d4a:

  • b18a7f2b4 Break a period's change in value into its drivers
  • a6cb0091c Pin the reconcile tolerance at one minor unit
  • 6c247bb81 Say when the drivers cover a suppressed day
  • cc334ffab Document that an embedded trade commission reads as market

The performance PR stacks on this one.

Problem

Portfolio::DailyReturns gives each day's components (flows, income, fees, market, revaluations, currency effect), but nothing says where a whole period's change in value came from, or whether those components account for all of it. Without that, a return figure has no explanation beside it, and a gap in the data reads as market movement.

Change

app/models/portfolio/drivers.rb (new, 201 lines at cc334ffab; 189 before that commit's 12 comment lines). It sums the daily rows' components:

  • Identity. external_net + composition + income - fees + market + revaluations + fx_effect == change, with any residual reported as unexplained.
  • Currency effect. fx_effect is measured per day by DailyReturns, not taken as the residual. So reconciles? can return false, and unexplained is a real figure.
  • Revaluations. These are reported apart from market. On a valuation day the whole move lands in the balance adjustments, so a manually valued account would otherwise show a flat zero market move.
  • Tolerance. RECONCILE_TOLERANCE is one minor unit, because a multi-currency family accumulates sub-cent dust from converting entries and balance rows by different paths. A known limitation for zero-decimal currencies is noted in the code.
  • Suppressed days. complete?, suppressed_dates and to_h[:suppressed_days] say when a day in the period was suppressed. A day DailyReturns suppresses (an unvaluable journal, a non-positive denominator) still reconciles, because its value stays in market. So reconciles? speaks only to the rows as recorded, and is documented to be read together with complete?. Anything presenting the drivers should show that flag beside them.

Nothing calls this class yet; the performance PR reads to_h.

Updated 2026-10-14. 7fdbad42c merges #4198 at b2b6981e8, which carries #4197's fixes (one row per entry, flow FX, excluded entries) and #4198's journal carry. cc334ffab documents a known limit on Drivers#fees: a commission folded into a buy's or sell's entry amount lands in market, not fees. Writers disagree on where the fee goes, so moving it needs a common contract first; that follow-up is #4229. There is no behaviour change.

Updated 2026-10-15. f9b99fd45 merges #4198 at 1c42bcef9, which closes the three CodeRabbit threads raised here: the journal carry (fixed on #4198), and the excluded counterpart and transfer ordering (fixed on #4197). No change to this PR's own files.

Validation

On 6c247bb81, stacked on d28a35d4a and cut from main at 3e35aaa07:

  • Full suite (bin/rails test): 11,844 runs, 54,579 assertions, 0 failures, 0 errors, 34 skips. Run locally on PostgreSQL 16.
  • 2026-10-14: the head moved to 6525686e0 only by merging the bind fix down the stack, and this PR's own files are unchanged. The portfolio model tests pass (122 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.
  • After the review fixes, on cc334ffab (2026-10-14): portfolio model tests 169 runs, 0 failures. Brakeman reports 0 warnings.
  • On f9b99fd45 (2026-10-15): portfolio model and investment statement tests 176 runs, 0 failures. The full suite on the stack's top (#4218 at 4d6909c07, which contains this head) gave 12,001 runs, 0 failures, 0 errors.
  • On db414ee31 (2026-10-23): merged the latest journal-flows fixes from #4198 (77e3ea386), then current main (db414ee31), with no conflicts and none of this PR's files changed by either. Full suite: 13,271 runs, 0 failures, 0 errors, 36 skips.
  • 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/drivers_test.rb, on 6c247bb81: 10 runs, 45 assertions, 0 failures.

Fail-first. The suppressed-day test failed on the missing complete? while every assertion above it passed.

Mutations. Each was run against this file's tests on the fork pre-flight, then restored. All were caught:

MutationTests that catch it
fees added instead of subtracted2
composition left out of unexplained1
fx_effect taken as the residual1
revaluations folded into market2
reconciles? always true1
value_open from the last row2
an empty scope raising1
tolerance 0, 0.02, or < for <=1 each
complete? always true1
suppressed_days reported as 01

Re-run on this branch, cut from current main:

  • fees added instead of subtracted: 2 failures;
  • < for <= in reconciles?: 1;
  • complete? always true: 1;
  • suppressed_days as 0: 1.

The code was byte-identical to the fork pre-flight it supersedes when rebuilt. cc334ffab (12 comment lines in drivers.rb, no behaviour change) was added afterwards.

Migration notes

None. No schema change.

Summary by CodeRabbit

  • New Features

    • Added daily portfolio return tracking that accounts for currency conversion, account changes, and investment activity.
    • Added a performance breakdown covering external flows, account composition, income, fees, market movement, valuation changes, and currency effects.
    • Results flag incomplete periods and unexplained changes, and show whether the breakdown reconciles within a small tolerance.
  • Bug Fixes

    • Improved transaction classification when multiple transfers are linked to the same transaction, preventing duplicate results and ensuring consistent transfer selection.
    • Added an option to include excluded entries in transaction classification; they remain excluded by default.
    • Account cut-off dates now affect transfer classification, with transfers on the cut-off date remaining in scope.