Value a journalled security as the flow it is
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#5089 (same commits), which replaces we-promise/sure#4198.
Replaces jaysbeekay/sure#444 (same changes, rebased and native-stacked).
Stacked on jaysbeekay/sure#707 in a fork native stack.
Rebased 2026-11-15 onto main at dbeebf14c. Rebuilt as one commit whose message lists the slice's original commits: they no longer replay one by one onto the newer slice below, which the old branch had merged in. Its tree equals the old head with main merged in. test/models/portfolio and test/models/balance at this slice: 275 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
A security moved into an account from a broker outside the scope is written as a Transfer-labelled trade with amount: 0 (Questrade writes price: 0, amount: 0). Portfolio::DailyReturns values a flow at its cash amount, so this one moved nothing. The arriving position then lands in the closing value with the denominator unchanged, and a position worth half the account reads as a 50% day.
Change
Two files: app/models/portfolio/daily_returns.rb (the journal valuation below) and app/models/portfolio/flow_classifier.rb (c2540bd17: a transfer's scope is decided on its counterpart leg's date; see Since review).
app/models/portfolio/daily_returns.rb:
- What a journal is. A Transfer-labelled trade with no cash amount.
- How it is valued. Its external flow is
qty xthat day's holding price, converted from the holding's currency at the previous day's rate. It usesqty, not the holding row'samount, because a top-up would otherwise count units already held. - Which holding row. It is chosen with
LIMIT 1, preferring the entry's currency.holdingsis unique per currency, and a plain join valued the journal once per row. - Market. The same value is subtracted from
market, becauseBalance::BaseCalculatorbooks the arriving position there. Counting it in both leftunexplainedat minus the journal. - Suppression. A journal with no usable price suppresses its own day, and so does one with no rate or no quantity; since
8cb179e86internal legs count too. It raisesrate_missing?when the rate is what is missing.Holding::PortfolioCachewrites a zero-price row on a date with no price, so keying on "no row" would never fire. - Carry (since
8cb179e86). An inbound journal (qty > 0) whose own day has no positive price is also booked on the first later day in the period, inside the account's cut-off, on which the account's holding of that security has a positive price: a flow at that price, converted at the previous day's rate, and taken out ofmarket. The journal's own day stays suppressed. A journal suppressed only for a missing rate or quantity is not carried, and neither is an outbound one. Since1c42bcef9, a journal dated before the period is carried too when its first priced day falls inside the period. - Rate check. A journal is left out of the entry-currency rate check, since it is valued from the holding's currency.
- Known limit: a multi-currency rate gap in
unexplained. When a journalled holding's currency differs from the account's and the rate moved that day,market(valued in the account's currency, converted at the previous day's account-currency rate) andjournal_flow(converted from the holding's currency at the previous day's rate) take different routes. The difference stays inunexplained, where it is visible, rather than being folded into another driver. - Unchanged. A Transfer-labelled trade that carries a cash amount, and Contribution or Withdrawal labelled trades, still flow at their cash amount.
408 non-test lines added, 24 removed: daily_returns.rb +385/−9 and flow_classifier.rb +23/−15.
The journal predicate binds the same way (152bc767e). journal_predicate quotes the Transfer label into a literal, so it now goes through the same fragment markers as the daily query from we-promise/sure#4197, rather than being interpolated before sanitize_sql_array. Its own probe test, "a colon word inside the journal predicate is not read as a bind", covers it.
Updated 2026-10-14: review fixes.
18343b905merges we-promise/sure#4197's51c7d69c5(one row per entry, flow FX, excluded entries, missing-rate checks). The conflict indaily_returns.rbgave the journalELSEbranch and the flow missing-rate check the two-step conversion and theamount <> 0guard.8cb179e86: an inbound journal with no price on its date is carried to the first later day in the period on which the account's holding has a positive price. It is booked there as a flow at that price and taken out ofmarket. The unpriced-journal check now covers internal legs too.b2b6981e8adds the control test that a journal priced on its own day is not carried.
Updated 2026-10-15.
9b88d2db8merges we-promise/sure#4197 at0f371528f(excluded security-transfer legs underinclude_excluded, the transfer lateral preferring the inflow side, the liability refusal).1c42bcef9, carry a journal dated before the period.carried_journals_by_dateselects by the day the carry lands on:entries.date < :end_date AND first_priced.date >= :start_date, instead of the journal's own date inside the period. A zero-price journal dated before the period and first priced inside it is now booked as a flow on that day, not as market.
Validation
On d28a35d4a, stacked on dc22ad370 and cut from main at 3e35aaa07:
- Full suite (
bin/rails test): 11,834 runs, 54,532 assertions, 0 failures, 0 errors, 34 skips. Run locally on PostgreSQL 16. - After the bind fix, on
152bc767e(2026-10-14):daily_returns_test.rb53 runs, 0 failures. CI is green. The full suite was not re-run on this exact head; the stack tip3f622bb8f, which contains this head, ran the full suite: 11,978 runs, 0 failures, 0 errors, 34 skips. - After the review fixes, on
b2b6981e8(2026-10-14): portfolio model tests 124 runs, 0 failures. Brakeman reports 0 warnings. - On
1c42bcef9(2026-10-15): full suite 11,893 runs, 0 failures, 0 errors, 34 skips. Brakeman reports 0 warnings. - Lint and security:
bin/rubocop -f githubon the changed files reports no offenses, andbin/brakeman --no-pager -qreports 0 warnings. No ERB, JS or system-test surface.
Tests
test/models/portfolio/daily_returns_test.rb, on d28a35d4a: 51 runs, 141 assertions, 0 failures. That is 16 more runs than the daily returns PR.
Fail-first:
- With the daily returns PR's
daily_returns.rbin place, 6 of the new tests fail: journal in, journal out, top-up, two suppression cases, and several currencies. The others are negative controls that hold either way. - The entry-currency rate-check test failed before its fix.
- So did the NULL-quantity test.
- Review fixes (
8cb179e86,b2b6981e8): 2 tests failing first onb7bc1e505, plus the control; 5 mutations, all caught. 1c42bcef9: 2 new tests failed onb2b6981e8(priced on the period's second day, and on its first day); a third holds the other side of the boundary. 2 mutations, both caught.
Mutations. Each was run against this file's tests on the fork pre-flight, then restored. All were caught:
| Mutation | Tests that catch it |
|---|---|
| journal valued at its cash amount | 4 |
| not taken out of market | 4 |
| valued from the holding amount | 4 |
| predicate ignores the amount | 1 |
| predicate ignores the label | 1 |
| unpriced journal not suppressed | 2 |
| zero price counted as priced | 1 |
holding join without LIMIT 1 | 1 |
| journal rate not flagged | 1 |
| journal rate at the journal date | 1 |
| rate-check exclusion dropped | 1 |
| NULL-quantity clause dropped | 1 |
BOOL_OR → BOOL_AND | 1 |
Re-run on this branch, cut from current main:
- journal valued at its cash amount: 6 failures;
- not taken out of market: 4;
- NULL-quantity clause dropped: 1;
BOOL_OR→BOOL_AND: 1.
The code was byte-identical to the fork pre-flight it supersedes when rebuilt. The bind fix above (152bc767e) was added afterwards.
Migration notes
None. No schema change.
Since review
c2540bd17: a transfer's scope is decided on its counterpart leg's date, in both the Ruby and SQL forms.35f5754e5: carried journals are capped together per first-priced row and scaled through the splits in between.d31b250f6: every unpriced journal is settled on its first priced row. Only external inbound journals are carried as flow, capped together at the units repriced from zero; if any repriced unit is left over (held before a top-up, the rest of a partial journal out, or moved in from another selected account), that day is suppressed as unvalued, the same rule as the journal day. Internal carries no longer count inexternal_flow.fc400f58f: a carried journal with no rate, or with no quantity, suppresses its first priced day, as it does its own day.- Full suite: 13,035 runs, 0 failures.