Flag an opening short of unpriced journalled-out units

JA
4d ago
+425-35

Closes #5982

Fork pre-flight: jaysbeekay/sure#1040 (same commits: same branch, same head).
Stacked on #5966 (up/daily-returns-opening-holdings), 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 (#5966). 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_unpriced stayed false, so the net contributions line showed no "may be incomplete" note.
  • InvestmentStatement#period_return_trend: the Period return KPI kept the account and period_return_unconvertible_count stayed 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_date gains traded_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_date carries it as opening_journal_unpriced, and the final SELECT ORs it into contribution_journal_unpriced. rate_missing and suppression are unchanged.
  • app/models/investment_statement.rb:
    • period_return_opening_trades_sql gains an unpriced_journal column, the same predicate written on its own aliases. Its other readers select named columns and are unchanged.
    • New period_return_unpriced_journal_account_ids, which period_return_trend subtracts beside the unconvertible ids.
    • New period_return_unpriced_count, which leaves out accounts period_return_unconvertible_count already counts, so no account is named twice.
  • app/models/portfolio/section_registry.rb: a period_return_unpriced KPI.
  • app/helpers/portfolios_helper.rb: kpi_hint takes 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.yml and de.yml: unpriced_journal_accounts.
  • No cache version changes. Portfolio::Performance caches its metrics hash and no metric reads contribution_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.

LightDark
Before (306e511d7): $750.00 (7.5%), no notebefore lightbefore dark
After: $200.00 (2.0%), "excludes 1 account with an unpriced transfer out"after lightafter dark

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

TestBefore 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.

Verification:

  • On d33604f0d: the full suite ran 20,130 runs, 0 failures, 8 errors, 9 skips. The 8 errors are ReleaseNotesWorkflowTest, which needs node, missing from the local test image. Rubocop, erb_lint, Brakeman and Biome were all clean.
  • The head, d9063ae09, merges #5966's head (7111d2209) into the branch. That brings base changes to investment_statement.rb and three of the test files above. It has been verified by CI only (green on the fork pre-flight), not by a local full-suite run.

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_trend too. An affected account drops out of it without a note, as an unconvertible one already does. It gets no hint here.
  • 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 here.

Summary by cubic

Flags accounts whose opening value is short of units journalled out with no price, so the figure is no longer silently understated.

Portfolio::DailyReturns now raises contribution_journal_unpriced when an internal journal out on an account's first row has no usable price that day, and the Period return KPI in InvestmentStatement excludes such accounts and names the reason in its hint. Previously an internal journal stayed unflagged, and a zero-priced holdings row also caused a same-day sale's proceeds to be read as a gain.

  • The unpriced check covers a journal out with no holdings row and no trade price, or a holdings row priced at ≤ 0; both readers now select it from one shared predicate, Portfolio::DailyReturns.unpriced_journal_out_sql, built on a single journal_sql definition of a security journal.
  • A journal fully netted by a same-day buy is not flagged; both readers pin the net-sale test strictly negative.
  • The Period return hint now distinguishes "no exchange rate" from "unpriced transfer out"; an account already counted as unconvertible is not named twice.
  • The German hint now also calls the transfer outgoing.
  • No cache version changes; the Reports investment card still excludes affected accounts without a note, as it does for unconvertible ones.

Summary by CodeRabbit

  • New Features
    • Period-return figures now exclude accounts with unpriced transfers that make their opening values unknown. The KPI hint reports how many accounts were excluded, alongside any accounts lacking a currency conversion rate.
    • Contribution completeness now reflects unpriced outgoing transfers, helping identify when transfer values leave account performance figures uncertain. Known transfer prices and offsetting purchases continue to be reflected in contribution calculations.