mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-18 20:59:39 +02:00
Improve holding records generation with security preloading
This commit is contained in:
parent
f57fa526af
commit
dd1a7fa8cb
1 changed files with 5 additions and 1 deletions
|
@ -48,8 +48,9 @@ class Account::HoldingCalculator
|
|||
def generate_holding_records(portfolio, date)
|
||||
portfolio.map do |security_id, qty|
|
||||
security = securities_cache[security_id]
|
||||
price = security.dig(:prices)&.find { |p| p.date == date }
|
||||
next if security.nil?
|
||||
|
||||
price = security.dig(:prices)&.find { |p| p.date == date }
|
||||
next if price.blank?
|
||||
|
||||
converted_price = Money.new(price.price, price.currency).exchange_to(account.currency, fallback_rate: 1).amount
|
||||
|
@ -106,7 +107,10 @@ class Account::HoldingCalculator
|
|||
end
|
||||
|
||||
def preload_securities
|
||||
# Get securities from trades and current holdings
|
||||
securities = trades.map(&:entryable).map(&:security).uniq
|
||||
securities += account.holdings.where(date: Date.current).map(&:security)
|
||||
securities.uniq!
|
||||
|
||||
securities.each do |security|
|
||||
prices = Security::Price.find_prices(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue