Flag a balance built from a holding valued at parity

JA
7d ago
+631-24

Closes #5631
Part of #4078 (part 3, returns engine)
Fork pre-flight: jaysbeekay/sure#945 (same commits, head d4ec1db23)
Stacked on #5637 (top of the returns stack). It fixes code from #5088; it sits on top rather than above #5088 so none of the eleven PRs above that slice changes.

What: a day whose balance was built from a holding valued at 1:1 for want of an exchange rate is now flagged rate_missing in Portfolio::DailyReturns, so the return series reports it and Performance withholds its ratios. Before, a USD account in a USD family returned a figure built on a made-up value, reported as reliable.
Where to start: Balance::SyncCache#holdings_valued_at_parity?, then Holding::PortfolioCache#price_at_parity?, then the holdings_at_parity / departed_at_parity reads in daily_returns.rb.
Key tests: materializer_test.rb "a holding priced at 1:1 marks its balances, and a partial sync of another security keeps them"; daily_returns_test.rb "a balance built from a holding valued at 1:1 flags its own day only".
Behaviour change: flags only. The 1:1 valuation is unchanged. A rate stored later keeps the flag until a sync recomputes the balance, because the stored value stays wrong until then.
Stack: the merge commit from the PR below ports the rate_missing expression and departure CTE onto the journal-FX and pending-counterpart terms from #5395/#5396. Both sides' terms are kept: the final rate_missing ORs the stack's carry_rate_missing with holdings_at_parity and departed_at_parity, and the departure CTE reports departed_at_parity next to departure_rate_missing. contribution_rate_missing is the stack's, unchanged. Portfolio::Performance::CACHE_VERSION goes to v14, since rate_missing now also means a balance built at 1:1.

Migration notes

20261121120000_add_parity_flags_to_balances_and_holdings adds two boolean columns, default false, not null:

  • balances.holdings_valued_at_parity, set when a balance's holdings value counted a holding at 1:1;
  • holdings.price_at_parity, set when the holdings calculator priced a row at 1:1. It sits on the row because a holding edit re-materializes only its own security (HoldingsController passes security_ids).

There is no backfill. Existing rows read false until they are recomputed. An incremental sync does not recompute balances before its window, so older dates need a full resync to pick up the flag. Rollback drops both columns.

Residual risk

Known gap: a 1:1 mark flags the return series (rate_missing), but the net contributions line, which reads contribution_rate_missing, does not yet withhold on a departure valued at 1:1. That would be new behaviour beyond this issue.

Design and evidence

Why sync time: a read-time SQL flag cleared on a later rate while the stored balance was still 1:1, missed the first day's opening value and a departure valued from a row before the period, copied PortfolioCache's price rules only in part, and cost a correlated subquery per holding row and day.

Flow:

  • PortfolioCache#get_price records a price it kept at 1:1 and clears the mark if a later conversion of the same key succeeds.
  • Both holding calculators set HoldingData#price_at_parity from it. Holding::Gapfillable carries it over days with no price and across splits, and Holding::Materializer stores it.
  • SyncCache builds the totals and the parity dates in one memoised pass. It counts both kinds in its missing-rate DebugLogEntry (unconvertible_holding_count, priced_at_parity_holding_count).
  • build_balance sets the balance flag, for investment accounts only.
  • DailyReturns ORs the flag into rate_missing outside the account-currency gate. It reads the row the close comes from; on the first day, the row before the period (one opening_rows CTE); for a departure, the cut-off row.

Tests that failed before the fix: SyncCache parity dates; the balance forward calculator marking the day; a forward materialize then a partial re-materialize keeping the marks; holding forward (carried days, split day) and reverse calculators; PortfolioCache clearing the mark on a later conversion; a non-investment account never marked; DailyReturns own day only, later rate, first-day opening, carried row, departure from a pre-period cut-off row.

Tests added with the merge (each failed against a one-sided resolution): daily_returns_test.rb "a departure at 1:1 with no rate flags the day and the contributions line apart"; performance_test.rb "every ratio is withheld when a balance was built from a holding at 1:1".

Mutations: every changed line was broken once; 25 tried, all caught. On the merged code, dropping each of carry_rate_missing, holdings_at_parity and departed_at_parity from the final rate_missing is caught (1, 5 and 2 tests).

Full unit suite on 15596e97e: 17,744 runs, 0 failures. The 8 errors are ReleaseNotesWorkflowTest (no node). On the merged head d4ec1db23: 18,036 runs, 0 failures, the same 8 errors.

Siblings: get_price has two callers, both calculators, both changed. HoldingData is built only by them and Gapfillable, and BalanceData only by build_balance; both default the flag to false. Row#rate_missing and #rate_missing? carry the flag to every reader.


Summary by cubic

Flags days whose balance counted a holding valued at 1:1 for want of an exchange rate as rate_missing, so the return series reports them and Performance withholds its ratios. Previously, a USD account in a USD family returned a figure built on that made-up value as reliable.

The mark lives on the balance and holding rows, set during sync when the holdings calculator prices a row at 1:1 or SyncCache converts one at 1:1; a zero-amount holding misstates nothing and marks no day. Cash taken from a total holding valued at 1:1 stays marked until the next total resets it. The 1:1 valuation itself is unchanged, and a later rate doesn't clear the flag until a sync recomputes the balance. A provider price that replaces a calculated holding clears its held mark, and an incremental sync seeded from a flagged balance falls back to a full recalculation. Performance::CACHE_VERSION is bumped to v14 because rate_missing now covers parity balances.

Migration

Adds balances.holdings_valued_at_parity and holdings.price_at_parity, both boolean, default false, not null. There is no backfill; existing rows read false until recomputed, so older dates need a full resync. Rollback drops both columns.

Summary by CodeRabbit

  • New Features
    • Balances and holdings now indicate when values or prices are recorded at 1:1, including when an exchange rate is unavailable.
  • Bug Fixes
    • Portfolio return calculations now flag parity-valued holdings as missing-rate days, including carried-forward balances and holdings outside the selected accounts.
    • Performance results are withheld when required conversion data is unavailable.
    • Parity indicators carry forward with holdings and balances, and clear when a valid converted price becomes available.