Stop converting foreign flows at parity in the period return
Closes #181. Triage plan and the two decisions taken: #181 comment.
The defect
R13 forbids converting at parity, and names this method as the one place that still did:
New code must never convert at parity through COALESCE(rate, 1); InvestmentStatement#period_return_trend does that today and is the pattern not to copy.
Two sites, both live on main — the issue body cites only the first:
312: SELECT COALESCE(SUM(b.net_market_flows * COALESCE(er.rate, 1)), 0) -- flows
344: SELECT COALESCE(SUM(b.end_balance * COALESCE(er.rate, 1)), 0) -- start value
A EUR balance with no EUR→USD rate was counted as though one euro were one dollar. The figure feeds the hub's Period return card, and it is an absolute money amount rather than a percentage — so it reads as "the portfolio made X" when part of X was an unconverted foreign balance, with nothing on the page saying so.
The fix
Both sites now use the lookup R13 prescribes and Portfolio::DailyReturns already implements: most recent rate on or before the date, else earliest rate after it, else NULL — never 1. A pair with any rate row anywhere resolves, so a weekend or a holiday does not suppress an account.
An account whose balance currency has no rate at all is dropped from both legs. Dropping it from one would leave numerator and denominator measured over different sets — a subtler wrong answer than the parity one.
The part worth reading twice
The drop is judged per account, not per row. The two legs select rows differently: the flows leg takes every row in the period, the start-value leg takes the last row before it.
So an account that changed currency has an anchor that converts and in-period rows that do not. Leaving the SQL to return NULL per row would have counted its start value while silently discarding the flows measured against it — understating the return, with no disclosure.
I did not spot that from reading. The mutation removing the account-narrowing passed, because my original fixture couldn't distinguish it. A test now constructs exactly that account, and the mutation fails it.
Scope and UI — both settled on the issue
- Scope: both parity legs. The Gatekeeper resolved this and asked for confirmation; fixing only the numerator would leave the return measured against a parity denominator.
- UI: partial figure with a disclosure, rather than suppressing the card. It rides the card's existing
hintlocal, so it reads "vs last month · excludes 1 account with no exchange rate".
Evidence
Six tests: the no-rate account excluded rather than converted; a rated account converted correctly; carry-forward from before the date; carry-back from after it; the partly-convertible account dropped whole; and a grep test asserting COALESCE(<alias>.rate, 1) appears nowhere in the model, so the pattern cannot return by another route.
Three mutations, all caught: the account set not narrowed, the carry-back arm removed, carry-forward narrowed to an exact date match.
305 runs / 1252 assertions across statement, registry, controller and helpers. Full suite: 9897 runs, 41188 assertions, 0 failures, 1 error — the PG::ConnectionBad that is #168 and reproduces on branches carrying none of these changes. Rubocop, erb_lint and Brakeman clean.
The contract
docs/portfolio/returns-contract.md R13 is updated in the same commit. It named this method as the live exception, and that sentence has stopped being true — leaving it would make the contract itself inaccurate.