Flag an opening short of unpriced journalled-out units
Closes jaysbeekay/sure#1039
Upstream issue: we-promise/sure#5982
Upstream PR: we-promise/sure#5985 (draft; same branch, same commit), stacked on we-promise/sure#5966, the upstream PR of jaysbeekay/sure#1019, as position 24 of stack 5133.
Base: up/daily-returns-opening-holdings, the head of jaysbeekay/sure#1019, which carries the opening logic this extends.
Summary
An account whose first balance row opens with nothing in start_non_cash_balance values what it held at the open as the units at the close less the units traded that day (jaysbeekay/sure#1019). A journal out with nothing to price it that day counts in those traded units but adds no value: either the security has no holdings row and the trade has no price, or the holdings row is priced at 0 (what Holding::ForwardCalculator writes when the journal's zero is the day's only price). The opening came out short and nothing said so:
Portfolio::DailyReturns: when the journal is internal,contribution_journal_unpricedstayed false, so the net contributions line showed no "may be incomplete" note.InvestmentStatement#period_return_trend: the Period return KPI kept the account andperiod_return_unconvertible_countstayed 0. With a zero-priced row, it also valued a same-day sale at 0 and read its proceeds as a gain (10,750 where 10,200 is right in the test below).
Now both readers mark these openings. DailyReturns raises contribution_journal_unpriced on the opening's row. InvestmentStatement leaves the account out of both period-return legs and counts it in a new period_return_unpriced_count, and the Period return hint names that reason apart from "no exchange rate".
What changed
app/models/portfolio/daily_returns.rb:flows_by_account_dategainstraded_unpriced, for a journal out with(no holdings row AND no price on the trade AND the day's net units in the security sold) OR a holdings row priced at <= 0.openings_by_datecarries it asopening_journal_unpriced, and the final SELECT ORs it intocontribution_journal_unpriced.rate_missingand suppression are unchanged.app/models/investment_statement.rb:period_return_opening_trades_sqlgains anunpriced_journalcolumn, the same predicate written on its own aliases. Its other readers select named columns and are unchanged.- New
period_return_unpriced_journal_account_ids, whichperiod_return_trendsubtracts beside the unconvertible ids. - New
period_return_unpriced_count, which leaves out accountsperiod_return_unconvertible_countalready counts, so no account is named twice.
app/models/portfolio/section_registry.rb: aperiod_return_unpricedKPI.app/helpers/portfolios_helper.rb:kpi_hinttakes an optional third count. The other cards don't pass it.app/views/portfolios/_kpi_row.html.erb: the Period return card passes the count.config/locales/views/portfolios/en.ymlandde.yml:unpriced_journal_accounts.- No cache version changes.
Portfolio::Performancecaches itsmetricshash and no metric readscontribution_journal_unpriced. The Reports body markup is unchanged.
Non-test diff: +72 / −15.
Screenshots
The Portfolio page, last 30 days. Account A goes from 10,000 to 10,200. Account B's first row sells 5 at 110 and journals 5 out at price 0, leaving a holdings row at qty 0 priced at 0.
| Light | Dark | |
|---|---|---|
| Before (306e511d7): $750.00 (7.5%), no note | ![]() | ![]() |
| After: $200.00 (2.0%), "excludes 1 account with an unpriced transfer out" | ![]() | ![]() |
The after shots were taken on 6660d9bb9. f86118add changes only a journal priced on the trade and the double count, and neither applies to this setup (journal at price 0, every account converts), so the output on the pushed head is the same.
Tests
| Test | Before fix |
|---|---|
| DailyReturns "an internal zero-priced journal out with no holdings row on an arrival's first row makes contributions incomplete" | Expected false to be truthy |
| DailyReturns "an internal zero-priced journal out with a holdings row at price 0 on an arrival's first row makes contributions incomplete" | Expected false to be truthy |
| DailyReturns "an internal zero-priced journal out of a whole position at price 0 on an arrival's first row makes contributions incomplete" | Expected false to be truthy |
| DailyReturns "an internal journal out priced on the trade with a holdings row at price 0 on an arrival's first row makes contributions incomplete" | Expected false to be truthy |
| InvestmentStatement "period_return_trend leaves out an account whose first row journals out units at a zero-priced holding" | Expected: 10200 Actual: 10750 |
| InvestmentStatement "period_return_trend leaves out an account whose first row journals out unpriced units with no holdings row" | Expected: 10200 Actual: 10250 |
| InvestmentStatement "period_return_trend leaves out an account whose first row journals out priced units at a zero-priced holding" | Expected: 10200 Actual: 10750 |
| InvestmentStatement "period_return_unpriced_count leaves out an account already counted as unconvertible" | Expected: 0 Actual: 1 |
Negative cases, which pass before and after, in both readers:
- a journal out priced by its holdings row;
- a journal out priced on the trade with no holdings row;
- a zero-priced journal out beside a larger buy;
- a zero-priced journal in;
- a zero-priced sale that is no journal.
The existing "keeps an account whose first-row traded units are worth nothing" now also asserts the new count is 0. An agreement test runs both readers over six shapes and asserts they decide alike: no row, a row at 0, a row at 110, net bought, priced on the trade, and priced on the trade with a row at 0. Helper tests cover the hint in en and de, and the registry test covers the new KPI key.
Mutations, one term at a time, each caught by a named test in each reader: the <= 0 arm, the Transfer label, the trade-price term (dropped, or applied to both arms), net_qty < 0, qty < 0, the OR into contribution_journal_unpriced, the subtraction in period_return_trend, and the subtraction in the count.
On f86118add:
- Full suite on d33604f0d: 20,130 runs, 0 failures, 8 errors, 9 skips. The 8 errors are
ReleaseNotesWorkflowTest, which needsnode, missing from the local test image. tools/precheck: rubocop, erb_lint, Brakeman and Biome all clean.
Known limits
- An internal journal's receiving leg still counts its journal-in as gain when the account it came from is left out of the period return. That predates this change and is out of scope here, so the hint does not mean the figure is otherwise whole.
- The Reports investment card reads
period_return_trendtoo. An affected account drops out of it without a note, as an unconvertible one already does. The owner decided on 2026-11-30 that the Reports card gets no hint. - A provider-written zero-priced holdings row on a day with a priced sale and no journal is not flagged. It is out of scope by the owner's decision on 2026-11-30.



