mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-05 13:35:21 +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
|
@ -40,12 +40,11 @@ class Holding::BaseCalculator
|
|||
new_quantities
|
||||
end
|
||||
|
||||
def build_holdings(portfolio, date)
|
||||
def build_holdings(portfolio, date, price_source: nil)
|
||||
portfolio.map do |security_id, qty|
|
||||
price = portfolio_cache.get_price(security_id, date)
|
||||
price = portfolio_cache.get_price(security_id, date, source: price_source)
|
||||
|
||||
if price.nil?
|
||||
Rails.logger.warn "No price found for security #{security_id} on #{date}"
|
||||
next
|
||||
end
|
||||
|
||||
|
|
|
@ -21,11 +21,15 @@ class Holding::PortfolioCache
|
|||
end
|
||||
end
|
||||
|
||||
def get_price(security_id, date)
|
||||
def get_price(security_id, date, source: nil)
|
||||
security = @security_cache[security_id]
|
||||
raise SecurityNotFound.new(security_id, account.id) unless security
|
||||
|
||||
price = security[:prices].select { |p| p.price.date == date }.min_by(&:priority)&.price
|
||||
if source.present?
|
||||
price = security[:prices].select { |p| p.price.date == date && p.source == source }.min_by(&:priority)&.price
|
||||
else
|
||||
price = security[:prices].select { |p| p.price.date == date }.min_by(&:priority)&.price
|
||||
end
|
||||
|
||||
return nil unless price
|
||||
|
||||
|
@ -46,7 +50,7 @@ class Holding::PortfolioCache
|
|||
end
|
||||
|
||||
private
|
||||
PriceWithPriority = Data.define(:price, :priority)
|
||||
PriceWithPriority = Data.define(:price, :priority, :source)
|
||||
|
||||
def trades
|
||||
@trades ||= account.entries.includes(entryable: :security).trades.chronological.to_a
|
||||
|
@ -86,7 +90,8 @@ class Holding::PortfolioCache
|
|||
db_prices = security.prices.where(date: account.start_date..Date.current).map do |price|
|
||||
PriceWithPriority.new(
|
||||
price: price,
|
||||
priority: 1
|
||||
priority: 1,
|
||||
source: "db"
|
||||
)
|
||||
end
|
||||
|
||||
|
@ -101,7 +106,8 @@ class Holding::PortfolioCache
|
|||
currency: trade.entryable.currency,
|
||||
date: trade.date
|
||||
),
|
||||
priority: 2
|
||||
priority: 2,
|
||||
source: "trade"
|
||||
)
|
||||
end
|
||||
|
||||
|
@ -115,7 +121,8 @@ class Holding::PortfolioCache
|
|||
currency: holding.currency,
|
||||
date: holding.date
|
||||
),
|
||||
priority: 3
|
||||
priority: 3,
|
||||
source: "holding"
|
||||
)
|
||||
end
|
||||
else
|
||||
|
|
|
@ -16,7 +16,9 @@ class Holding::ReverseCalculator < Holding::BaseCalculator
|
|||
Date.current.downto(account.start_date).each do |date|
|
||||
today_trades = portfolio_cache.get_trades(date: date)
|
||||
previous_portfolio = transform_portfolio(current_portfolio, today_trades, direction: :reverse)
|
||||
holdings += build_holdings(current_portfolio, date)
|
||||
|
||||
# If current day, always use holding prices (since that's what Plaid gives us). For historical values, use market data (since Plaid doesn't supply historical prices)
|
||||
holdings += build_holdings(current_portfolio, date, price_source: date == Date.current ? "holding" : nil)
|
||||
current_portfolio = previous_portfolio
|
||||
end
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue