mirror of
https://github.com/we-promise/sure.git
synced 2026-05-29 15:34:58 +00:00
Fix: Use historical exchange rate for historical prices
This commit is contained in:
@@ -40,7 +40,7 @@ class Holding::PortfolioCache
|
|||||||
price_money = Money.new(price.price, price.currency)
|
price_money = Money.new(price.price, price.currency)
|
||||||
|
|
||||||
begin
|
begin
|
||||||
converted_amount = price_money.exchange_to(account.currency).amount
|
converted_amount = price_money.exchange_to(account.currency, date: date).amount
|
||||||
rescue Money::ConversionError
|
rescue Money::ConversionError
|
||||||
converted_amount = price.price
|
converted_amount = price.price
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -56,4 +56,40 @@ class Holding::PortfolioCacheTest < ActiveSupport::TestCase
|
|||||||
cache = Holding::PortfolioCache.new(@account, use_holdings: true)
|
cache = Holding::PortfolioCache.new(@account, use_holdings: true)
|
||||||
assert_equal holding.price, cache.get_price(@security.id, holding.date).price
|
assert_equal holding.price, cache.get_price(@security.id, holding.date).price
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "converts historical prices using the requested date exchange rate" do
|
||||||
|
account = families(:empty).accounts.create!(
|
||||||
|
name: "CHF Brokerage",
|
||||||
|
balance: 10000,
|
||||||
|
currency: "CHF",
|
||||||
|
accountable: Investment.new
|
||||||
|
)
|
||||||
|
holding_date = 2.days.ago.to_date
|
||||||
|
|
||||||
|
ExchangeRate.create!(from_currency: "USD", to_currency: "CHF", date: holding_date, rate: 0.80)
|
||||||
|
ExchangeRate.create!(from_currency: "USD", to_currency: "CHF", date: Date.current, rate: 0.95)
|
||||||
|
|
||||||
|
Holding.create!(
|
||||||
|
security: @security,
|
||||||
|
account: account,
|
||||||
|
date: holding_date,
|
||||||
|
qty: 1,
|
||||||
|
price: 100,
|
||||||
|
amount: 100,
|
||||||
|
currency: "USD"
|
||||||
|
)
|
||||||
|
|
||||||
|
Security::Price.create!(
|
||||||
|
security: @security,
|
||||||
|
date: holding_date,
|
||||||
|
price: 100,
|
||||||
|
currency: "USD"
|
||||||
|
)
|
||||||
|
|
||||||
|
cache = Holding::PortfolioCache.new(account, use_holdings: true)
|
||||||
|
converted_price = cache.get_price(@security.id, holding_date)
|
||||||
|
|
||||||
|
assert_equal BigDecimal("80.0"), converted_price.price
|
||||||
|
assert_equal "CHF", converted_price.currency
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user