Flag a balance built from a holding valued at parity
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 (HoldingsControllerpassessecurity_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_pricerecords 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_parityfrom it.Holding::Gapfillablecarries it over days with no price and across splits, andHolding::Materializerstores it. SyncCachebuilds the totals and the parity dates in one memoised pass. It counts both kinds in its missing-rateDebugLogEntry(unconvertible_holding_count,priced_at_parity_holding_count).build_balancesets the balance flag, for investment accounts only.DailyReturnsORs the flag intorate_missingoutside the account-currency gate. It reads:- the row the close comes from;
- on the first day, the row before the period (one
opening_rowsCTE); - for a departure, the cut-off row. That term is needed: on its departure day the leaving account is outside the
balances_by_datewindow, so dropping it fails the departure test.
Tests that failed before the fix:
SyncCachesays 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_pricehas two callers, both calculators, both changed.HoldingDatais built only by them andGapfillable(both copies changed), andBalanceDataonly bybuild_balance. Both default the flag to false.Row#rate_missingand#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.