Start linked investment charts at the pre-activity balance

Merged
RY
2mo ago
+211-32

Problem

On a linked investment account, when chart sampling lands exactly on the first supported history date (e.g. a daily "All" chart), the first point carries that day's closing balance — which already includes the day's first activity. A first deposit of $4,703.99 then reads as the starting value, and the "vs. beginning" comparison shows $1.38 instead of +$4,705.37.

Closes #3959.

What changed

  • app/models/balance/linked_investment_series_normalizer.rb
    • When sampling lands exactly on the first supported history date and that date is the first provider activity date, the first point is reset to the balance before the first activity (0, or the opening anchor balance when the anchor marks the inception date). This reuses the same opening-balance semantics as the existing prepended anchor used when coarse sampling misses the date.
    • When the inception date comes from provider holdings rather than activity, the first point is genuine supported history and is kept as-is.
    • One point per date is preserved (no duplicated anchor).
  • test/models/balance/linked_investment_series_normalizer_test.rb
    • Updated the exact-date test to expect the opening balance instead of the day's closing balance.
    • Added: daily all-time chart starting at first deposit (the issue scenario, including a trend assertion).
    • Added: exact hit on an opening-anchor date uses the anchor balance.
    • Added: holdings-driven inception keeps the first point's value.

Validation

  • test/models/balance/linked_investment_series_normalizer_test.rb: 16 runs, 36 assertions, 0 failures
  • test/models/balance/chart_series_builder_test.rb: 14 runs, 29 assertions, 0 failures
  • Bug-hunter scan-only single-pass on the changed file: 2 findings, 1 fixed (reset misfiring on holdings-driven inception), 1 assessed as consistent with the existing "valuations don't establish history" convention.

Summary by CodeRabbit

  • Bug Fixes
    • Corrected the opening value shown on investment balance charts when the chart begins on the first activity date or a configured history start date.
    • Preserved the first holdings value when an investment’s history begins with holdings rather than completed activity, including when activity is still pending.
    • Improved detection of unchanged chart data so updates to the starting date or value are reflected.