mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-07 06:25:19 +02:00
Handle holding quantity generation for reverse syncs correctly when not all holdings are generated for current day (#2417)
* Handle reverse calculator starting portfolio generation correctly * Fix current_holdings to handle different dates and hide zero quantities - Use DISTINCT ON to get most recent holding per security instead of assuming same date - Filter out zero quantity holdings from UI display - Maintain cash display regardless of zero balance - Use single efficient query with proper Rails syntax * Continue to process holdings even if one is not resolvable * Lint fixes
This commit is contained in:
parent
e60b5df442
commit
8db95623cf
8 changed files with 281 additions and 39 deletions
|
@ -1,8 +1,9 @@
|
|||
class Holding::ReverseCalculator
|
||||
attr_reader :account
|
||||
attr_reader :account, :portfolio_snapshot
|
||||
|
||||
def initialize(account)
|
||||
def initialize(account, portfolio_snapshot:)
|
||||
@account = account
|
||||
@portfolio_snapshot = portfolio_snapshot
|
||||
end
|
||||
|
||||
def calculate
|
||||
|
@ -21,7 +22,8 @@ class Holding::ReverseCalculator
|
|||
end
|
||||
|
||||
def calculate_holdings
|
||||
current_portfolio = generate_starting_portfolio
|
||||
# Start with the portfolio snapshot passed in from the materializer
|
||||
current_portfolio = portfolio_snapshot.to_h
|
||||
previous_portfolio = {}
|
||||
|
||||
holdings = []
|
||||
|
@ -38,24 +40,6 @@ class Holding::ReverseCalculator
|
|||
holdings
|
||||
end
|
||||
|
||||
def empty_portfolio
|
||||
securities = portfolio_cache.get_securities
|
||||
securities.each_with_object({}) { |security, hash| hash[security.id] = 0 }
|
||||
end
|
||||
|
||||
# Since this is a reverse sync, we start with today's holdings
|
||||
def generate_starting_portfolio
|
||||
holding_quantities = empty_portfolio
|
||||
|
||||
todays_holdings = account.holdings.where(date: Date.current)
|
||||
|
||||
todays_holdings.each do |holding|
|
||||
holding_quantities[holding.security_id] = holding.qty
|
||||
end
|
||||
|
||||
holding_quantities
|
||||
end
|
||||
|
||||
def transform_portfolio(previous_portfolio, trade_entries, direction: :forward)
|
||||
new_quantities = previous_portfolio.dup
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue