← Back to Pull requests

Flag an opening short of unpriced journalled-out units #5985

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Nov 30, 2026 18:10 UTC
Author

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.

    | | Light | Dark |
    |---|---|---|
    | Before (306e511d7): $750.00 (7.5%), no note | !before light | !before dark |
    | After: $200.00 (2.0%), "excludes 1 account with an unpriced transfer out" | !after light | !after 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

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

    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.
  • <!-- This is an auto-generated description by cubic. -->
    ---

    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.
  • <sup>Written for commit d33604f0dde66048f7f6229830def0a6dd780c71. Summary will update on new commits.</sup>

    <a href="https://cubic.dev/pr/we-promise/sure/pull/5985?utm_source=github" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img alt="View guided diff" src="https://www.cubic.dev/buttons/review-in-cubic-light.svg"></picture></a> <a href="https://www.cubic.dev/action/auto-fix/pr/we-promise/sure/5985?returnTo=https%3A%2F%2Fgithub.com%2Fwe-promise%2Fsure%2Fpull%2F5985&source=description" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/turn-on-auto-fix-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/turn-on-auto-fix-light.svg"><img alt="Turn on auto-fix" src="https://www.cubic.dev/buttons/turn-on-auto-fix-light.svg"></picture></a>

    <!-- End of auto-generated description by cubic. -->

    <!-- This is an auto-generated comment: release notes by coderabbit.ai -->
    <!-- coderabbit-ai-generated:v1 -->

    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.
    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: jaysbeekay Review duration: 6994m
    All Vouch review confidence checks passed
    Residual risk 0.28 = Change Risk (0.52) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #5985"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 27.7% · Safe to merge
    27.7%