Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Start linked investment charts at the pre-activity balance (#4009)

Febryand Nur Hidayatdd8f7326f081
diff --git a/app/models/balance/linked_investment_series_normalizer.rb b/app/models/balance/linked_investment_series_normalizer.rb
index 544b3eae70..b78838ef38 100644
--- a/app/models/balance/linked_investment_series_normalizer.rb
+++ b/app/models/balance/linked_investment_series_normalizer.rb
@@ -89,8 +89,10 @@ class Balance::LinkedInvestmentSeriesNormalizer
@view = view
end
- # Trims points before supported provider history and prepends an anchor point at inception
- # if coarse sampling missed the opening date within the requested period.
+ # Trims points before supported provider history and aligns the series
+ # inception with the balance before the first provider activity, so a
+ # deposit on the opening day reads as a change in value rather than as
+ # the starting value.
def normalize
return series unless account.linked? && account.balance_type == :investment
@@ -100,12 +102,51 @@ class Balance::LinkedInvestmentSeriesNormalizer
active_points = series.values.select { |value| value.date >= first_supported_history_date }
return series if active_points.blank?
- # If periodic sampling missed the exact opening date (e.g. coarse 1-month or 1-week intervals),
- # prepend an anchor point on the exact opening date with the initial balance (e.g. $0)
- # only when the inception date falls within the requested series date range.
+ opening_money = Money.new(opening_amount(first_supported_history_date), active_points.first.value.currency)
+
if first_supported_history_date >= series.start_date && active_points.first.date > first_supported_history_date
- currency = active_points.first.value.currency
- initial_amount = case view.to_sym
+ # Periodic sampling missed the exact opening date (e.g. coarse 1-month or
+ # 1-week intervals): prepend an anchor point on the exact opening date with
+ # the initial balance (e.g. $0), only when the inception date falls within
+ # the requested series date range.
+ active_points = [ opening_point(date: first_supported_history_date, date_formatted: nil, value: opening_money), *active_points ]
+ elsif active_points.first.date == first_supported_history_date &&
+ first_provider_activity_date == first_supported_history_date
+ # Sampling landed exactly on the opening date (e.g. a daily "All" chart)
+ # and that date is the first provider activity date: the first point
+ # carries the day's closing balance, which already includes the first
+ # activity. Reset it to the balance before that activity (#3959).
+ # When the inception date comes from provider holdings rather than
+ # activity, the first point is genuine supported history and is kept.
+ first_point = active_points.first
+ active_points = [
+ opening_point(date: first_point.date, date_formatted: first_point.date_formatted, value: opening_money),
+ *active_points.drop(1)
+ ]
+ end
+
+ return series if unchanged?(active_points)
+
+ Series.new(
+ start_date: active_points.first.date,
+ end_date: series.end_date,
+ interval: series.interval,
+ values: active_points,
+ favorable_direction: series.favorable_direction
+ )
+ end
+
+ def supported_history_start_date
+ [ first_provider_activity_date, stable_provider_holding_start_date ].compact.min
+ end
+
+ private
+
+ # The balance just before the first provider activity: 0 for views that
+ # measure change (gains, holdings), the opening anchor balance for balance
+ # views when the anchor marks the inception date, 0 otherwise.
+ def opening_amount(first_supported_history_date)
+ case view.to_sym
when :gains, :holdings_balance
0
when :cash_balance, :balance
@@ -117,36 +158,28 @@ class Balance::LinkedInvestmentSeriesNormalizer
else
0
end
- opening_money = Money.new(initial_amount, currency)
- anchor_value = Series::Value.new(
- date: first_supported_history_date,
- date_formatted: I18n.l(first_supported_history_date, format: :long),
- value: opening_money,
+ end
+
+ def opening_point(date:, date_formatted:, value:)
+ Series::Value.new(
+ date: date,
+ date_formatted: date_formatted || I18n.l(date, format: :long),
+ value: value,
trend: Trend.new(
- current: opening_money,
+ current: value,
previous: nil,
favorable_direction: series.favorable_direction
)
)
- active_points = [ anchor_value, *active_points ]
end
- return series if active_points.first&.date == series.values.first&.date && active_points.length == series.values.length
-
- Series.new(
- start_date: active_points.first.date,
- end_date: series.end_date,
- interval: series.interval,
- values: active_points,
- favorable_direction: series.favorable_direction
- )
- end
-
- def supported_history_start_date
- [ first_provider_activity_date, stable_provider_holding_start_date ].compact.min
- end
-
- private
+ # The series is untouched when trimming and inception alignment changed
+ # neither the dates nor the values of the points.
+ def unchanged?(active_points)
+ active_points.first&.date == series.values.first&.date &&
+ active_points.length == series.values.length &&
+ active_points.first&.value == series.values.first&.value
+ end
def first_provider_activity_date
@first_provider_activity_date ||= account.entries
diff --git a/test/models/balance/linked_investment_series_normalizer_test.rb b/test/models/balance/linked_investment_series_normalizer_test.rb
index 8c75e06d18..4a1ed3081a 100644
--- a/test/models/balance/linked_investment_series_normalizer_test.rb
+++ b/test/models/balance/linked_investment_series_normalizer_test.rb
@@ -182,7 +182,7 @@ class Balance::LinkedInvestmentSeriesNormalizerTest < ActiveSupport::TestCase
assert_equal Money.new(500, "USD"), normalized.values.first.value
end
- test "normalizer does not duplicate anchor point when coarse sample lands on exact opening date" do
+ test "normalizer resets first point to opening balance when sample lands on exact opening date" do
account = families(:empty).accounts.create!(
name: "Linked Exact Date",
balance: 0,
@@ -220,8 +220,96 @@ class Balance::LinkedInvestmentSeriesNormalizerTest < ActiveSupport::TestCase
normalized = normalizer.normalize
assert_equal opening_date, normalized.start_date
+ # Still one point per date (no duplicated anchor), but the first point now
+ # carries the balance before the first activity instead of the day's closing balance.
assert_equal [ opening_date, Date.current ], normalized.values.map(&:date)
- assert_equal Money.new(100, "USD"), normalized.values.first.value
+ assert_equal Money.new(0, "USD"), normalized.values.first.value
+ assert_equal Money.new(110, "USD"), normalized.trend.value
+ end
+
+ test "normalizer resets first point to zero on daily all-time chart starting at first deposit" do
+ account = families(:empty).accounts.create!(
+ name: "Linked Daily Exact",
+ balance: 0,
+ currency: "USD",
+ accountable: Investment.new
+ )
+ coinstats_item = account.family.coinstats_items.create!(name: "CoinStats", api_key: "test-key")
+ coinstats_account = coinstats_item.coinstats_accounts.create!(name: "Provider", currency: "USD")
+ account.account_providers.create!(provider: coinstats_account)
+
+ first_activity_date = 10.days.ago.to_date
+ account.entries.create!(
+ name: "Initial Deposit",
+ date: first_activity_date,
+ amount: 4703_99,
+ currency: "USD",
+ source: "snaptrade",
+ entryable: Transaction.new
+ )
+
+ raw_series = Series.new(
+ start_date: first_activity_date,
+ end_date: Date.current,
+ interval: "1 day",
+ values: [
+ Series::Value.new(date: first_activity_date, date_formatted: "", value: Money.new(4703_99, "USD")),
+ Series::Value.new(date: 5.days.ago.to_date, date_formatted: "", value: Money.new(4705_37, "USD")),
+ Series::Value.new(date: Date.current, date_formatted: "", value: Money.new(4705_37, "USD"))
+ ],
+ favorable_direction: account.favorable_direction
+ )
+
+ normalizer = Balance::LinkedInvestmentSeriesNormalizer.new(account: account, series: raw_series)
+ normalized = normalizer.normalize
+
+ assert_equal first_activity_date, normalized.start_date
+ assert_equal [ first_activity_date, 5.days.ago.to_date, Date.current ], normalized.values.map(&:date)
+ assert_equal Money.new(0, "USD"), normalized.values.first.value
+ # "vs. beginning" now measures against the pre-activity balance (#3959).
+ assert_equal Money.new(4705_37, "USD"), normalized.trend.value
+ end
+
+ test "normalizer resets first point to opening anchor balance when sample lands on anchor date" do
+ account = families(:empty).accounts.create!(
+ name: "Linked Exact Anchor Date",
+ balance: 0,
+ currency: "USD",
+ accountable: Investment.new
+ )
+ coinstats_item = account.family.coinstats_items.create!(name: "CoinStats", api_key: "test-key")
+ coinstats_account = coinstats_item.coinstats_accounts.create!(name: "Provider", currency: "USD")
+ account.account_providers.create!(provider: coinstats_account)
+
+ opening_date = 7.days.ago.to_date
+ account.set_opening_anchor_balance(balance: 5000, date: opening_date)
+ account.entries.create!(
+ name: "Trade",
+ date: opening_date,
+ amount: 100,
+ currency: "USD",
+ source: "snaptrade",
+ entryable: Transaction.new
+ )
+
+ raw_series = Series.new(
+ start_date: 14.days.ago.to_date,
+ end_date: Date.current,
+ interval: "1 week",
+ values: [
+ Series::Value.new(date: 14.days.ago.to_date, date_formatted: "", value: Money.new(0, "USD")),
+ Series::Value.new(date: opening_date, date_formatted: "", value: Money.new(5100, "USD")),
+ Series::Value.new(date: Date.current, date_formatted: "", value: Money.new(5200, "USD"))
+ ],
+ favorable_direction: account.favorable_direction
+ )
+
+ normalizer = Balance::LinkedInvestmentSeriesNormalizer.new(account: account, series: raw_series)
+ normalized = normalizer.normalize
+
+ assert_equal opening_date, normalized.start_date
+ assert_equal [ opening_date, Date.current ], normalized.values.map(&:date)
+ assert_equal Money.new(5000, "USD"), normalized.values.first.value
end
test "normalizer returns series unmodified when linked account has no history" do
@@ -463,4 +551,62 @@ class Balance::LinkedInvestmentSeriesNormalizerTest < ActiveSupport::TestCase
assert_equal [ anchor_date, 10.days.ago.to_date, Date.current ], normalized.values.map(&:date)
assert_equal Money.new(5000, "USD"), normalized.values.first.value
end
+
+ test "normalizer keeps first point when inception comes from holdings, not activity" do
+ account = families(:empty).accounts.create!(
+ name: "Linked Holdings First",
+ balance: 0,
+ currency: "USD",
+ accountable: Investment.new
+ )
+ coinstats_item = account.family.coinstats_items.create!(name: "CoinStats", api_key: "test-key")
+ coinstats_account = coinstats_item.coinstats_accounts.create!(name: "Provider", currency: "USD")
+ account_provider = account.account_providers.create!(provider: coinstats_account)
+
+ # Real pre-existing balance, anchored before the provider started syncing.
+ anchor_date = 30.days.ago.to_date
+ account.set_opening_anchor_balance(balance: 5000, date: anchor_date)
+
+ # Provider holdings exist, but no posted provider activity yet (only pending).
+ holdings_start_date = 10.days.ago.to_date
+ security = Security.create!(ticker: "TST", name: "Test")
+ Holding.create!(
+ account: account,
+ security: security,
+ date: holdings_start_date,
+ qty: 10,
+ price: 500,
+ amount: 5000,
+ currency: "USD",
+ account_provider_id: account_provider.id
+ )
+ account.entries.create!(
+ date: holdings_start_date,
+ name: "Pending Transaction",
+ amount: 100,
+ currency: "USD",
+ source: "plaid",
+ entryable: Transaction.new(extra: { "plaid" => { "pending" => true } })
+ )
+
+ raw_series = Series.new(
+ start_date: holdings_start_date,
+ end_date: Date.current,
+ interval: "1 day",
+ values: [
+ Series::Value.new(date: holdings_start_date, date_formatted: "", value: Money.new(5000, "USD")),
+ Series::Value.new(date: Date.current, date_formatted: "", value: Money.new(5100, "USD"))
+ ],
+ favorable_direction: account.favorable_direction
+ )
+
+ normalizer = Balance::LinkedInvestmentSeriesNormalizer.new(account: account, series: raw_series)
+ normalized = normalizer.normalize
+
+ # The first point is genuine supported history (no activity on that date),
+ # so it must keep its value instead of being reset to 0.
+ assert_equal holdings_start_date, normalized.start_date
+ assert_equal [ holdings_start_date, Date.current ], normalized.values.map(&:date)
+ assert_equal Money.new(5000, "USD"), normalized.values.first.value
+ end
end