Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Keep account holding weights at or below 100% when cash is negative (#5019)

arnaurodondev9b5b97679b5a
diff --git a/app/models/account.rb b/app/models/account.rb
index 24c19d11c5..fed971e5fb 100644
--- a/app/models/account.rb
+++ b/app/models/account.rb
@@ -70,6 +70,7 @@ class Account < ApplicationRecord
def reload(*)
@scheduled_entry_flows = nil
+ @current_holdings_total = nil
super
end
@@ -822,6 +823,11 @@ class Account < ApplicationRecord
clear_attribute_changes(restored.keys)
end
+ def current_holdings_total
+ # A holding with no exchange rate is left out rather than counted 1:1.
+ @current_holdings_total ||= current_holdings.sum { |h| h.amount_in_account_currency(unconvertible: 0) }
+ end
+
def current_holdings
if (provider_snapshot_date = latest_provider_holdings_snapshot_date)
holdings
diff --git a/app/models/holding.rb b/app/models/holding.rb
index b43ca390fb..e0112c6626 100644
--- a/app/models/holding.rb
+++ b/app/models/holding.rb
@@ -42,7 +42,11 @@ class Holding < ApplicationRecord
return nil unless amount
return 0 if amount.zero?
- account.balance.zero? ? 1 : amount_in_account_currency / account.balance * 100
+ # Never divide by less than the holdings (this one included), so negative
+ # cash cannot push a weight past 100%. As InvestmentStatement#weight_denominator.
+ value = amount_in_account_currency
+ denominator = [ account.balance, account.current_holdings_total, value ].max
+ denominator.positive? ? value / denominator * 100 : 1
end
# The stored per-share cost when it is a real value:
@@ -389,13 +393,14 @@ class Holding < ApplicationRecord
remove_instance_variable(:@day_change) if defined?(@day_change)
end
- def amount_in_account_currency
+ def amount_in_account_currency(unconvertible: amount)
return amount if currency == account.currency
Money.new(amount, currency).exchange_to(account.currency, date: date).amount
rescue Money::ConversionError
- amount
+ unconvertible
end
+ public :amount_in_account_currency
def calculate_trend
return nil unless amount_money
diff --git a/test/models/holding_test.rb b/test/models/holding_test.rb
index 1503a3079c..c4828268e4 100644
--- a/test/models/holding_test.rb
+++ b/test/models/holding_test.rb
@@ -36,6 +36,50 @@ class HoldingTest < ActiveSupport::TestCase
assert_in_delta 0.75, foreign_holding.weight, 0.001
end
+ # A missing deposit leaves the account's cash negative, so its
+ # balance is below the holdings value. Weights must still stay at or below 100%.
+ test "weight does not exceed 100 percent when account cash is negative" do
+ @account.update!(balance: 3000, cash_balance: 3000 - (3240 + 3720))
+
+ assert_in_delta 3240.0 / (3240 + 3720) * 100, @amzn.weight, 0.001
+ assert_in_delta 3720.0 / (3240 + 3720) * 100, @nvda.weight, 0.001
+ end
+
+ test "weight sums the account holdings once per account instance" do
+ holdings = @account.current_holdings.to_a
+
+ Holding.uncached { assert_queries_count(2) { holdings.each { |holding| 3.times { holding.weight } } } }
+ end
+
+ test "weight sees holdings added before an account reload" do
+ @account.update!(balance: 0)
+ assert_in_delta 3240.0 / (3240 + 3720) * 100, @amzn.weight, 0.001
+
+ @account.holdings.create!(security: Security.create!(ticker: "W1", name: "W1"), date: Date.current, qty: 1, price: 3040, amount: 3040, currency: "USD")
+ @amzn.account.reload
+
+ assert_in_delta 3240.0 / 10_000 * 100, @amzn.weight, 0.001
+ end
+
+ test "weight of a holding outside the current holdings does not exceed 100 percent" do
+ ExchangeRate.create!(from_currency: "EUR", to_currency: "USD", date: Date.current, rate: 1.5)
+ eur = @account.holdings.create!(security: Security.create!(ticker: "ASML", name: "ASML"), date: Date.current, qty: 1, price: 10_000, amount: 10_000, currency: "EUR")
+ @account.update!(balance: 50, cash_balance: 50 - 6960)
+
+ assert_in_delta 100, eur.weight, 0.001
+ end
+
+ test "a holding with no exchange rate does not shrink the other holdings' weights" do
+ # Only a provider snapshot lists holdings in other currencies.
+ provider = AccountProvider.create!(account: @account, provider: plaid_accounts(:one))
+ @account.holdings.update_all(account_provider_id: provider.id)
+ @account.holdings.create!(security: Security.create!(ticker: "7203", name: "Toyota"), date: Date.current, qty: 1, price: 1_000_000, amount: 1_000_000,
+ currency: "JPY", account_provider_id: provider.id)
+ @account.update!(balance: 7000, cash_balance: 40)
+
+ assert_in_delta 3240.0 / 7000 * 100, @amzn.weight, 0.001
+ end
+
test "calculates average cost basis" do
create_trade(@amzn.security, account: @account, qty: 10, price: 212.00, date: 1.day.ago.to_date)
create_trade(@amzn.security, account: @account, qty: 15, price: 216.00, date: Date.current)
@@ -797,4 +841,13 @@ class HoldingTest < ActiveSupport::TestCase
assert_not_nil holding.avg_cost
end
+
+ test "weight stays finite for a short holding when the balance and holdings are not positive" do
+ account = families(:empty).accounts.create!(name: "Short only", balance: 0, cash_balance: 0, currency: "USD", accountable: Investment.new)
+ holding = account.holdings.create!(security: Security.create!(ticker: "SHRT", name: "Short"), date: Date.current,
+ qty: 1, price: 1, amount: 1, currency: "USD")
+ holding.update_columns(qty: -1, price: 500, amount: -500)
+
+ assert_equal 1, holding.reload.weight
+ end
end