mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-10 07:55:21 +02:00
Better naming
This commit is contained in:
parent
791683aca0
commit
d0a32bcd1e
4 changed files with 23 additions and 25 deletions
|
@ -1,8 +1,8 @@
|
|||
class Account::Balance::ForwardCalculator < Account::Balance::BaseCalculator
|
||||
private
|
||||
def calculate_balances
|
||||
prior_cash_balance = 0
|
||||
current_cash_balance = nil
|
||||
current_cash_balance = 0
|
||||
next_cash_balance = nil
|
||||
|
||||
@balances = []
|
||||
|
||||
|
@ -12,16 +12,15 @@ class Account::Balance::ForwardCalculator < Account::Balance::BaseCalculator
|
|||
holdings_value = holdings.sum(&:amount)
|
||||
valuation = sync_cache.get_valuation(date)
|
||||
|
||||
current_cash_balance = if valuation
|
||||
# Since a valuation means "total balance" (which includes holdings), we back out holdings value to get cash balance
|
||||
next_cash_balance = if valuation
|
||||
valuation.amount - holdings_value
|
||||
else
|
||||
calculate_next_balance(prior_cash_balance, entries, direction: :forward)
|
||||
calculate_next_balance(current_cash_balance, entries, direction: :forward)
|
||||
end
|
||||
|
||||
@balances << build_balance(date, current_cash_balance, holdings_value)
|
||||
@balances << build_balance(date, next_cash_balance, holdings_value)
|
||||
|
||||
prior_cash_balance = current_cash_balance
|
||||
current_cash_balance = next_cash_balance
|
||||
end
|
||||
|
||||
@balances
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
class Account::Balance::ReverseCalculator < Account::Balance::BaseCalculator
|
||||
private
|
||||
def calculate_balances
|
||||
todays_cash_balance = account.cash_balance
|
||||
yesterdays_cash_balance = nil
|
||||
current_cash_balance = account.cash_balance
|
||||
previous_cash_balance = nil
|
||||
|
||||
@balances = []
|
||||
|
||||
|
@ -12,20 +12,19 @@ class Account::Balance::ReverseCalculator < Account::Balance::BaseCalculator
|
|||
holdings_value = holdings.sum(&:amount)
|
||||
valuation = sync_cache.get_valuation(date)
|
||||
|
||||
yesterdays_cash_balance = if valuation
|
||||
# Since a valuation means "total balance" (which includes holdings), we back out holdings value to get cash balance
|
||||
previous_cash_balance = if valuation
|
||||
valuation.amount - holdings_value
|
||||
else
|
||||
calculate_next_balance(todays_cash_balance, entries, direction: :reverse)
|
||||
calculate_next_balance(current_cash_balance, entries, direction: :reverse)
|
||||
end
|
||||
|
||||
if valuation.present?
|
||||
@balances << build_balance(date, yesterdays_cash_balance, holdings_value)
|
||||
@balances << build_balance(date, previous_cash_balance, holdings_value)
|
||||
else
|
||||
@balances << build_balance(date, todays_cash_balance, holdings_value)
|
||||
@balances << build_balance(date, current_cash_balance, holdings_value)
|
||||
end
|
||||
|
||||
todays_cash_balance = yesterdays_cash_balance
|
||||
current_cash_balance = previous_cash_balance
|
||||
end
|
||||
|
||||
@balances
|
||||
|
|
|
@ -1,15 +1,15 @@
|
|||
class Account::Holding::ForwardCalculator < Account::Holding::BaseCalculator
|
||||
private
|
||||
def calculate_holdings
|
||||
todays_portfolio = generate_starting_portfolio
|
||||
tomorrows_portfolio = {}
|
||||
current_portfolio = generate_starting_portfolio
|
||||
next_portfolio = {}
|
||||
@holdings = []
|
||||
|
||||
account.start_date.upto(Date.current).each do |date|
|
||||
trades = portfolio_cache.get_trades(date: date)
|
||||
tomorrows_portfolio = transform_portfolio(todays_portfolio, trades, direction: :forward)
|
||||
@holdings += build_holdings(tomorrows_portfolio, date)
|
||||
todays_portfolio = tomorrows_portfolio
|
||||
next_portfolio = transform_portfolio(current_portfolio, trades, direction: :forward)
|
||||
@holdings += build_holdings(next_portfolio, date)
|
||||
current_portfolio = next_portfolio
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,16 +1,16 @@
|
|||
class Account::Holding::ReverseCalculator < Account::Holding::BaseCalculator
|
||||
private
|
||||
def calculate_holdings
|
||||
todays_portfolio = generate_starting_portfolio
|
||||
yesterdays_portfolio = {}
|
||||
current_portfolio = generate_starting_portfolio
|
||||
previous_portfolio = {}
|
||||
|
||||
@holdings = []
|
||||
|
||||
Date.current.downto(account.start_date).map do |date|
|
||||
today_trades = portfolio_cache.get_trades(date: date)
|
||||
yesterdays_portfolio = transform_portfolio(todays_portfolio, today_trades, direction: :reverse)
|
||||
@holdings += build_holdings(todays_portfolio, date)
|
||||
todays_portfolio = yesterdays_portfolio
|
||||
previous_portfolio = transform_portfolio(current_portfolio, today_trades, direction: :reverse)
|
||||
@holdings += build_holdings(current_portfolio, date)
|
||||
current_portfolio = previous_portfolio
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue