mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-24 23:59:40 +02:00
Match Plaid holding values on current day (#2212)
* Match Plaid holding values on current day * Fix chart timezone issue * Add timezone tests for syncs * Hide sidebars on trades test
This commit is contained in:
parent
470b753833
commit
2000f05453
16 changed files with 242 additions and 21 deletions
|
@ -23,6 +23,22 @@ class Balance::ReverseCalculatorTest < ActiveSupport::TestCase
|
|||
assert_equal expected, calculated.map(&:balance)
|
||||
end
|
||||
|
||||
test "balance generation respects user timezone and last generated date is current user date" do
|
||||
# Simulate user in EST timezone
|
||||
Time.zone = "America/New_York"
|
||||
|
||||
# Set current time to 1am UTC on Jan 5, 2025
|
||||
# This would be 8pm EST on Jan 4, 2025 (user's time, and the last date we should generate balances for)
|
||||
travel_to Time.utc(2025, 01, 05, 1, 0, 0)
|
||||
|
||||
create_valuation(account: @account, date: "2025-01-03", amount: 17000)
|
||||
|
||||
expected = [ [ "2025-01-02", 17000 ], [ "2025-01-03", 17000 ], [ "2025-01-04", @account.balance ] ]
|
||||
calculated = Balance::ReverseCalculator.new(@account).calculate
|
||||
|
||||
assert_equal expected, calculated.sort_by(&:date).map { |b| [ b.date.to_s, b.balance ] }
|
||||
end
|
||||
|
||||
test "valuations sync" do
|
||||
create_valuation(account: @account, date: 4.days.ago.to_date, amount: 17000)
|
||||
create_valuation(account: @account, date: 2.days.ago.to_date, amount: 19000)
|
||||
|
@ -56,4 +72,52 @@ class Balance::ReverseCalculatorTest < ActiveSupport::TestCase
|
|||
|
||||
assert_equal expected, calculated
|
||||
end
|
||||
|
||||
# When syncing backwards, trades from the past should NOT affect the current balance or previous balances.
|
||||
# They should only affect the *cash* component of the historical balances
|
||||
test "holdings and trades sync" do
|
||||
aapl = securities(:aapl)
|
||||
|
||||
# Account starts with $20,000 total value, $19,000 cash, $1,000 in holdings
|
||||
@account.update!(cash_balance: 19000, balance: 20000)
|
||||
|
||||
# Bought 10 AAPL shares 1 day ago, so cash is $19,000, $1,000 in holdings, total value is $20,000
|
||||
create_trade(aapl, account: @account, qty: 10, date: 1.day.ago.to_date, price: 100)
|
||||
|
||||
Holding.create!(date: Date.current, account: @account, security: aapl, qty: 10, price: 100, amount: 1000, currency: "USD")
|
||||
Holding.create!(date: 1.day.ago.to_date, account: @account, security: aapl, qty: 10, price: 100, amount: 1000, currency: "USD")
|
||||
|
||||
# Given constant prices, overall balance (account value) should be constant
|
||||
# (the single trade doesn't affect balance; it just alters cash vs. holdings composition)
|
||||
expected = [ 20000, 20000, 20000 ]
|
||||
calculated = Balance::ReverseCalculator.new(@account).calculate.sort_by(&:date).map(&:balance)
|
||||
|
||||
assert_equal expected, calculated
|
||||
end
|
||||
|
||||
# A common scenario with Plaid is they'll give us holding records for today, but no trade history for some of them.
|
||||
# This is because they only supply 2 years worth of historical data. Our system must properly handle this.
|
||||
test "properly calculates balances when a holding has no trade history" do
|
||||
aapl = securities(:aapl)
|
||||
msft = securities(:msft)
|
||||
|
||||
# Account starts with $20,000 total value, $19,000 cash, $1,000 in holdings ($500 AAPL, $500 MSFT)
|
||||
@account.update!(cash_balance: 19000, balance: 20000)
|
||||
|
||||
# A holding *with* trade history (5 shares of AAPL, purchased 1 day ago, results in 2 holdings)
|
||||
Holding.create!(date: Date.current, account: @account, security: aapl, qty: 5, price: 100, amount: 500, currency: "USD")
|
||||
Holding.create!(date: 1.day.ago.to_date, account: @account, security: aapl, qty: 5, price: 100, amount: 500, currency: "USD")
|
||||
create_trade(aapl, account: @account, qty: 5, date: 1.day.ago.to_date, price: 100)
|
||||
|
||||
# A holding *without* trade history (5 shares of MSFT, no trade history, results in 1 holding)
|
||||
# We assume if no history is provided, this holding has existed since beginning of account
|
||||
Holding.create!(date: Date.current, account: @account, security: msft, qty: 5, price: 100, amount: 500, currency: "USD")
|
||||
Holding.create!(date: 1.day.ago.to_date, account: @account, security: msft, qty: 5, price: 100, amount: 500, currency: "USD")
|
||||
Holding.create!(date: 2.days.ago.to_date, account: @account, security: msft, qty: 5, price: 100, amount: 500, currency: "USD")
|
||||
|
||||
expected = [ 20000, 20000, 20000 ]
|
||||
calculated = Balance::ReverseCalculator.new(@account).calculate.sort_by(&:date).map(&:balance)
|
||||
|
||||
assert_equal expected, calculated
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue