The personal finance app for everyone (by everyone)
Ruby
Stop gains chart counting securities the account left (#4149)
jaysbeekayb0de6a1c087b
diff --git a/app/models/balance/chart_series_builder.rb b/app/models/balance/chart_series_builder.rbindex cd09821244..a287050239 100644--- a/app/models/balance/chart_series_builder.rb+++ b/app/models/balance/chart_series_builder.rb@@ -249,6 +249,17 @@ class Balance::ChartSeriesBuilder# holding snapshot per (account, security) on or before that date (LOCF), convert to# the target currency, and aggregate unrealized gains (amount - cost_basis * qty).# Holdings only exist on asset accounts, so no liability sign handling is needed.+ #+ # Which securities count on a date follows the Holdings tab (Account#current_holdings).+ # Once an account has a provider holdings snapshot on or before the date, a security+ # counts only if that snapshot holds it at a nonzero quantity: a security the provider+ # moved to another record, stopped reporting without a closing row, or reports at+ # zero, is no longer held, and a calculated row after the snapshot (the reverse+ # calculator gap-fills to today, and stale calculated rows are not purged) does not+ # bring it back, unless the account bought the security after the snapshot: the+ # reverse calculator then rebuilds that position from its trades, so it counts from+ # the trade until the next snapshot says otherwise. An account with no provider+ # snapshot yet keeps every security's latest row, carried forward.def gains_query<<~SQLWITH dates AS (@@ -274,12 +285,61 @@ class Balance::ChartSeriesBuilderFROM holdings hWHERE h.account_id = ANY(array[:account_ids]::uuid[])),+ -- Each account's provider snapshot dates, and the date the next one starts.+ provider_dates AS MATERIALIZED (+ SELECT+ snapshot.account_id,+ snapshot.date,+ LEAD(snapshot.date) OVER (PARTITION BY snapshot.account_id ORDER BY snapshot.date) AS next_date+ FROM (+ SELECT DISTINCT h.account_id, h.date+ FROM holdings h+ WHERE h.account_id = ANY(array[:account_ids]::uuid[])+ AND h.account_provider_id IS NOT NULL+ ) snapshot+ ),+ -- The account's latest provider snapshot on or before each series date. Resolved+ -- once per (date, account) here: as a lateral beside the security join, Postgres+ -- re-scanned the account's holdings for every (date, security).+ provider_snapshots AS MATERIALIZED (+ SELECT d.date, pd.account_id, pd.date AS snapshot_date+ FROM dates d+ JOIN provider_dates pd+ ON pd.date <= d.date+ AND (pd.next_date IS NULL OR d.date < pd.next_date)+ ),daily_gains AS (SELECTd.date,COALESCE(SUM(CASEWHEN last_basis.cost_basis IS NOT NULL+ AND (+ provider_snapshot.snapshot_date IS NULL+ OR EXISTS (+ SELECT 1+ FROM holdings ph+ WHERE ph.account_id = accounts.id+ AND ph.security_id = sec.security_id+ AND ph.account_provider_id IS NOT NULL+ AND ph.date = provider_snapshot.snapshot_date+ AND ph.qty <> 0+ )+ -- Acquired since that snapshot: the reverse calculator rebuilds the+ -- position from its trades until the next snapshot reports it. A sale+ -- cannot open a position the snapshot lacks, so only a buy counts.+ OR EXISTS (+ SELECT 1+ FROM entries te+ JOIN trades t ON t.id = te.entryable_id+ WHERE te.entryable_type = 'Trade'+ AND te.account_id = accounts.id+ AND t.security_id = sec.security_id+ AND t.qty > 0+ AND te.date > provider_snapshot.snapshot_date+ AND te.date <= d.date+ )+ )THEN (last_h.amount - (last_basis.cost_basis * last_h.qty)) * CASE WHEN last_h.currency = :target_currency THEN 1 ELSE er.rate ENDELSE 0END@@ -287,6 +347,9 @@ class Balance::ChartSeriesBuilderFROM dates dLEFT JOIN selected_accounts accountsON accounts.active_until_date IS NULL OR d.date <= accounts.active_until_date+ LEFT JOIN provider_snapshots provider_snapshot+ ON provider_snapshot.date = d.date+ AND provider_snapshot.account_id = accounts.idLEFT JOIN account_securities sec ON sec.account_id = accounts.idLEFT JOIN LATERAL (SELECT h.date, h.amount, h.qty, h.currencydiff --git a/test/models/balance/chart_series_builder_test.rb b/test/models/balance/chart_series_builder_test.rbindex 29d2666781..9b38d33ca4 100644--- a/test/models/balance/chart_series_builder_test.rb+++ b/test/models/balance/chart_series_builder_test.rb@@ -1,7 +1,8 @@require "test_helper"class Balance::ChartSeriesBuilderTest < ActiveSupport::TestCase- include BalanceTestHelper+ include BalanceTestHelper, EntriesTestHelper+ include EntriesTestHelpersetup doend@@ -631,7 +632,292 @@ class Balance::ChartSeriesBuilderTest < ActiveSupport::TestCaseassert_equal 200, series.values.last.trend.current.amountend+ # The membership rule below is the Holdings tab's (`Account#current_holdings`):+ # once an account has a provider snapshot on or before a date, a security counts+ # on that date only if that snapshot holds it. These tests write provider rows,+ # as a provider sync does; calculated rows stand in for gap-fills between syncs.+ test "gains series stops counting a security record the provider moved the position off" do+ account = accounts(:investment)+ account.holdings.destroy_all+ link = link_provider(account)+ old_record = Security.create!(ticker: "AAPL", name: "Apple Inc. (duplicate record)", exchange_operating_mic: "XXXX")+ new_record = securities(:aapl)++ # The provider reported the position on one record, then on another for the same ticker.+ stale = create_provider_holding(account: account, link: link, security: old_record, date: 5.days.ago.to_date, qty: 10, price: 100, cost_basis: 90)+ 4.downto(0) do |days_ago|+ create_provider_holding(account: account, link: link, security: new_record, date: days_ago.days.ago.to_date, qty: 10, price: 110, cost_basis: 90)+ end++ with_stale_row = gains_amounts(account, start_date: 5.days.ago.to_date)+ stale.destroy!+ without_stale_row = gains_amounts(account, start_date: 5.days.ago.to_date)++ # The old record is held on the day it was reported, and on no later day.+ assert_equal [ 100, 200, 200, 200, 200, 200 ], with_stale_row+ assert_equal without_stale_row.drop(1), with_stale_row.drop(1)+ end++ test "gains series stops counting a position the provider stops reporting" do+ account = accounts(:investment)+ account.holdings.destroy_all+ link = link_provider(account)++ # MSFT drops out of the provider's snapshot after two days ago, with no zero-quantity row and no sell.+ 2.downto(0) do |days_ago|+ create_provider_holding(account: account, link: link, security: securities(:aapl), date: days_ago.days.ago.to_date, qty: 10, price: 100, cost_basis: 90)+ end+ create_provider_holding(account: account, link: link, security: securities(:msft), date: 2.days.ago.to_date, qty: 5, price: 60, cost_basis: 50)++ # AAPL 100 every day; MSFT 50 only on the day it was held.+ assert_equal [ 150, 100, 100 ], gains_amounts(account, start_date: 2.days.ago.to_date)+ end++ test "gains series keeps counting every security in the latest provider snapshot across gap-filled days" do+ account = accounts(:investment)+ account.holdings.destroy_all+ link = link_provider(account)++ create_provider_holding(account: account, link: link, security: securities(:aapl), date: 2.days.ago.to_date, qty: 10, price: 102, cost_basis: 90)+ create_provider_holding(account: account, link: link, security: securities(:msft), date: 2.days.ago.to_date, qty: 5, price: 60, cost_basis: 50)+ # No sync since: calculated gap-fill rows carry no cost_basis, and the position is unchanged.+ 1.downto(0) do |days_ago|+ create_holding(account: account, security: securities(:aapl), date: days_ago.days.ago.to_date, qty: 10, price: 100 + days_ago, cost_basis: nil)+ end+ create_holding(account: account, security: securities(:msft), date: 1.day.ago.to_date, qty: 5, price: 60, cost_basis: nil)+ create_holding(account: account, security: securities(:msft), date: Date.current, qty: 5, price: 70, cost_basis: nil)++ # AAPL 120, 110, 100; MSFT 50, 50, 100.+ assert_equal [ 170, 160, 200 ], gains_amounts(account, start_date: 2.days.ago.to_date)+ end++ test "gains series carries the latest provider snapshot forward when the account has not synced since" do+ account = accounts(:investment)+ account.holdings.destroy_all+ link = link_provider(account)++ create_provider_holding(account: account, link: link, security: securities(:aapl), date: 3.days.ago.to_date, qty: 10, price: 100, cost_basis: 90)+ create_provider_holding(account: account, link: link, security: securities(:msft), date: 3.days.ago.to_date, qty: 5, price: 60, cost_basis: 50)++ # No rows after three days ago at all: that snapshot is still the account's position.+ assert_equal [ 150, 150, 150, 150 ], gains_amounts(account, start_date: 3.days.ago.to_date)+ end++ test "gains series judges each account against its own provider snapshot" do+ synced = accounts(:investment)+ synced.holdings.destroy_all+ behind = synced.family.accounts.create!(name: "Not synced lately", balance: 1000, currency: "USD", accountable: Investment.new)+ synced_link = link_provider(synced)+ behind_link = link_provider(behind, name: "Second link")++ 2.downto(0) do |days_ago|+ create_provider_holding(account: synced, link: synced_link, security: securities(:aapl), date: days_ago.days.ago.to_date, qty: 10, price: 100, cost_basis: 90)+ end+ create_provider_holding(account: behind, link: behind_link, security: securities(:msft), date: 2.days.ago.to_date, qty: 5, price: 60, cost_basis: 50)++ builder = Balance::ChartSeriesBuilder.new(+ account_ids: [ synced.id, behind.id ],+ currency: "USD",+ period: Period.custom(start_date: 2.days.ago.to_date, end_date: Date.current),+ interval: "1 day"+ )++ # The other account's later snapshot does not end this account's position.+ assert_equal [ 150, 150, 150 ], builder.gains_series.map { |v| v.value.amount }+ end++ test "gains series counts a zero-quantity closing row as no gain" do+ account = accounts(:investment)+ account.holdings.destroy_all++ create_holding(account: account, security: securities(:aapl), date: 2.days.ago.to_date, qty: 10, price: 100, cost_basis: 90)+ create_holding(account: account, security: securities(:aapl), date: 1.day.ago.to_date, qty: 0, price: 100, cost_basis: nil)+ create_holding(account: account, security: securities(:aapl), date: Date.current, qty: 0, price: 100, cost_basis: nil)++ assert_equal [ 100, 0, 0 ], gains_amounts(account, start_date: 2.days.ago.to_date)+ end++ # Plaid dates each holding by its own `institution_price_as_of`, so one sync+ # can leave a security's provider row older than the rest. The Holdings tab+ # (`Account#current_holdings`) and the reverse calculator's starting+ # portfolio (`Holding::PortfolioSnapshot`) both take only the provider rows on+ # the account's latest provider date, so that security is not part of the+ # position they show. Today's gain must agree with the Holdings tab.+ test "today's gain agrees with the Holdings tab when provider rows carry different dates" do+ account = accounts(:investment)+ account.holdings.destroy_all+ account.entries.destroy_all+ link = AccountProvider.create!(account: account, provider: plaid_accounts(:one))++ [ [ securities(:aapl), Date.current, 10, 110, 90 ], [ securities(:msft), 2.days.ago.to_date, 5, 60, 50 ] ].each do |security, date, qty, price, basis|+ account.holdings.create!(+ security: security, date: date, qty: qty, price: price, amount: qty * price, currency: "USD",+ cost_basis: basis, cost_basis_source: "provider", account_provider: link+ )+ end+ Holding::Materializer.new(account, strategy: :reverse).materialize_holdings++ tab_gain = account.current_holdings.sum { |h| h.amount - (h.cost_basis * h.qty) }+ assert_equal [ securities(:aapl).id ], account.current_holdings.map(&:security_id)+ assert_equal tab_gain, gains_amounts(account, start_date: Date.current).last+ end++ # A linked account's provider snapshot+ # is dated before today, so the reverse calculator gap-fills calculated rows for+ # its securities through today. When the next snapshot moves the position to+ # another record, the old record's calculated row on today is not purged: the+ # cleanup removes only rows for securities the new snapshot holds.+ test "a calculated row for a security the latest provider snapshot dropped is not counted" do+ account = accounts(:investment)+ account.holdings.destroy_all+ account.entries.destroy_all+ link = link_provider(account)+ old_record = Security.create!(ticker: "AAPL", name: "Apple Inc. (duplicate record)", exchange_operating_mic: "XXXX")++ create_provider_holding(account: account, link: link, security: old_record, date: 1.day.ago.to_date, qty: 10, price: 100, cost_basis: 90)+ Holding::Materializer.new(account, strategy: :reverse).materialize_holdings+ create_provider_holding(account: account, link: link, security: securities(:aapl), date: Date.current, qty: 10, price: 110, cost_basis: 90)+ Holding::Materializer.new(account, strategy: :reverse).materialize_holdings++ assert account.holdings.exists?(security: old_record, date: Date.current, account_provider_id: nil),+ "precondition: the old record's calculated row on today survives the second sync"+ assert_equal [ securities(:aapl).id ], account.current_holdings.map(&:security_id)+ tab_gain = account.current_holdings.sum { |h| h.amount - (h.cost_basis * h.qty) }++ assert_equal [ 100, tab_gain ], gains_amounts(account, start_date: 1.day.ago.to_date)+ end++ # Editing one holding re-materializes+ # only that security (HoldingsController -> Balance::Materializer with+ # security_ids:), so its rows reach today while the others' end at the last full+ # sync. An account with no provider snapshot keeps every security's latest row,+ # as the Holdings tab does.+ test "a manual account keeps a security whose rows end before another security's" do+ account = accounts(:investment)+ account.holdings.destroy_all+ account.entries.destroy_all+ create_trade(securities(:aapl), account: account, qty: 10, date: 5.days.ago.to_date, price: 100)+ create_trade(securities(:msft), account: account, qty: 5, date: 5.days.ago.to_date, price: 50)++ travel_to(1.day.ago) { Holding::Materializer.new(account, strategy: :forward).materialize_holdings }+ Holding::Materializer.new(account, strategy: :forward, security_ids: [ securities(:msft).id ]).materialize_holdings++ last_dates = account.holdings.group(:security_id).maximum(:date)+ assert_equal 1.day.ago.to_date, last_dates[securities(:aapl).id], "precondition: AAPL's rows end at yesterday's full sync"+ assert_equal Date.current, last_dates[securities(:msft).id], "precondition: MSFT's rows reach today"+ assert_equal [ securities(:aapl).id, securities(:msft).id ].sort, account.current_holdings.map(&:security_id).sort++ yesterday, today = gains_amounts(account, start_date: 1.day.ago.to_date)+ assert yesterday.positive?, "precondition: the position shows a gain"+ assert_equal yesterday, today+ end++ # Plaid dates a holding by its+ # price, so a sync can report a position it has already sold at zero quantity+ # on a snapshot dated before the sale. The reverse calculator reads that+ # snapshot as today's position and undoes the sale, so the calculated rows+ # after the snapshot carry the shares. The Holdings tab skips a zero-quantity+ # provider row, and so must the chart.+ test "a security the provider snapshot holds at zero quantity is not counted from later rows" do+ account = accounts(:investment)+ account.holdings.destroy_all+ account.entries.destroy_all+ link = link_provider(account)+ sold = Security.create!(ticker: "SOLD", name: "Sold position", exchange_operating_mic: "XXXX")+ 0.upto(6) { |days_ago| Security::Price.create!(security: sold, date: days_ago.days.ago.to_date, price: 100, currency: "USD") }++ create_trade(sold, account: account, qty: 5, date: 6.days.ago.to_date, price: 80)+ create_trade(sold, account: account, qty: -5, date: Date.current, price: 100)+ create_provider_holding(account: account, link: link, security: sold, date: 2.days.ago.to_date, qty: 0, price: 100, cost_basis: nil)+ create_provider_holding(account: account, link: link, security: securities(:aapl), date: 2.days.ago.to_date, qty: 10, price: 110, cost_basis: 90)+ Holding::Materializer.new(account, strategy: :reverse).materialize_holdings++ assert_equal 5, account.holdings.find_by!(security: sold, date: 1.day.ago.to_date, account_provider_id: nil).qty,+ "precondition: the calculated row after the snapshot carries the sold shares"+ assert_equal [ securities(:aapl).id ], account.current_holdings.map(&:security_id)++ with_sold = gains_amounts(account, start_date: 3.days.ago.to_date)+ account.holdings.where(security: sold).delete_all+ without_sold = gains_amounts(account, start_date: 3.days.ago.to_date)++ # Before the snapshot the position is held: 5 x (100 - 80).+ assert_equal 100, with_sold.first - without_sold.first+ # From the snapshot on, it adds nothing.+ assert_equal without_sold.drop(1), with_sold.drop(1)+ end++ test "a provider row's quantity decides whether the security's later rows count" do+ account = accounts(:investment)+ account.holdings.destroy_all+ link = link_provider(account)++ msft_snapshot = create_provider_holding(account: account, link: link, security: securities(:msft), date: 1.day.ago.to_date, qty: 0, price: 60, cost_basis: nil)+ create_provider_holding(account: account, link: link, security: securities(:aapl), date: 1.day.ago.to_date, qty: 10, price: 100, cost_basis: 90)+ # Calculated rows after the snapshot, as the reverse calculator writes them.+ create_holding(account: account, security: securities(:msft), date: Date.current, qty: 5, price: 60, cost_basis: 50)+ create_holding(account: account, security: securities(:aapl), date: Date.current, qty: 10, price: 110, cost_basis: nil)++ # MSFT is held at zero on the snapshot, so only AAPL counts: 100, then 200.+ assert_equal [ 100, 200 ], gains_amounts(account, start_date: 1.day.ago.to_date)++ # The same row holding shares keeps MSFT's later row counting: 5 x (60 - 50) on each day.+ msft_snapshot.update!(qty: 5, amount: 300, cost_basis: 50)+ assert_equal [ 150, 250 ], gains_amounts(account, start_date: 1.day.ago.to_date)+ end++ # Between two provider snapshots the reverse calculator rebuilds the position+ # from trades. A security bought after the earlier snapshot is held from its+ # trade on, so its gain counts on the days in between, not only from the next+ # snapshot.+ test "gains series counts a position bought between provider snapshots" do+ account = accounts(:investment)+ account.holdings.destroy_all+ account.entries.destroy_all+ link = AccountProvider.create!(account: account, provider: plaid_accounts(:one))+ aapl = securities(:aapl)+ msft = securities(:msft)+ [ aapl, msft ].each do |security|+ Security::Price.where(security: security).delete_all+ (0..4).each { |days_ago| Security::Price.create!(security: security, date: days_ago.days.ago.to_date, price: security == aapl ? 100 : 60, currency: "USD") }+ end++ provider_row = ->(security, date, qty, basis) do+ account.holdings.create!(security: security, date: date, qty: qty, price: security == aapl ? 100 : 60,+ amount: qty * (security == aapl ? 100 : 60), currency: "USD",+ cost_basis: basis, cost_basis_source: "provider", account_provider: link)+ end+ provider_row.(aapl, 4.days.ago.to_date, 10, 90)+ create_trade(msft, account: account, qty: 5, price: 50, date: 3.days.ago.to_date)+ provider_row.(aapl, Date.current, 10, 90)+ provider_row.(msft, Date.current, 5, 50)+ Holding::Materializer.new(account, strategy: :reverse).materialize_holdings++ # AAPL 10 * (100 - 90) = 100 every day; MSFT 5 * (60 - 50) = 50 from its trade on.+ assert_equal [ 100, 150, 150, 150, 150 ], gains_amounts(account, start_date: 4.days.ago.to_date)+ end+private+ def gains_amounts(account, start_date:)+ Balance::ChartSeriesBuilder.new(+ account_ids: [ account.id ],+ currency: "USD",+ period: Period.custom(start_date: start_date, end_date: Date.current),+ interval: "1 day"+ ).gains_series.map { |v| v.value.amount }+ end++ def link_provider(account, name: "Provider")+ item = account.family.coinstats_items.create!(name: name, api_key: "test-key")+ AccountProvider.create!(account: account, provider: item.coinstats_accounts.create!(name: name, currency: account.currency))+ end++ def create_provider_holding(account:, link:, security:, date:, qty:, price:, cost_basis:)+ account.holdings.create!(+ security: security, date: date, qty: qty, price: price, amount: qty * price,+ currency: account.currency, cost_basis: cost_basis, cost_basis_source: "provider", account_provider: link+ )+ end+def create_holding(account:, security:, date:, qty:, price:, cost_basis:)Holding.create!(account: account,