Flag a balance built from a holding valued at parity

JA
7d ago
+631-24

Closes jaysbeekay/sure#874

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.
Base: up/drivers-currency-tolerance (jaysbeekay/sure#942), the top of the returns stack. Merge 8f87a4407 ports daily_returns.rb onto we-promise/sure#5395/#5396: 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 unchanged. d4ec1db23 adds the merge tests and bumps Portfolio::Performance::CACHE_VERSION to v14.

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: an earlier read-time SQL flag was dropped after code review. It cleared on a later rate while the stored balance was still 1:1. It missed the first day's opening value and a departure valued from a row before the period. It copied PortfolioCache's price rules only in part, and it 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. That term is needed: on its departure day the leaving account is outside the balances_by_date window, so dropping it fails the departure test.

Tests that failed before the fix:

  • SyncCache says which dates valued a holding at 1:1, and reports a holding the calculator priced at 1:1.
  • The balance forward calculator marks the day.
  • A real forward materialize, then a partial re-materialize of another security, keeps the marks.
  • Holding forward calculator: carried days and a split day stay marked. Reverse calculator: its calculated rows are marked.
  • PortfolioCache: a later successful conversion clears the mark.
  • A non-investment account is never marked and its holdings aren't read.
  • DailyReturns: own day only, a later rate not clearing it, first-day opening, a carried row, a 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 (both copies changed), and BalanceData only by build_balance. Both default the flag to false.
  • Row#rate_missing and #rate_missing? carry the flag to every reader.

Upstream PR: we-promise/sure#5638 (same branch, same commits; stacked on we-promise/sure#5637 in the returns stack). Upstream issue: we-promise/sure#5631.