mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-02 20:15:22 +02:00
Fix holding avg cost calculation
This commit is contained in:
parent
800eb4c146
commit
027c18297b
3 changed files with 15 additions and 3 deletions
|
@ -26,8 +26,12 @@ class Account::Holding < ApplicationRecord
|
|||
|
||||
# Basic approximation of cost-basis
|
||||
def avg_cost
|
||||
avg_cost = account.holdings.for(security).where(currency: currency).where("date <= ?", date).average(:price)
|
||||
Money.new(avg_cost, currency)
|
||||
avg_cost = account.entries.account_trades
|
||||
.joins("INNER JOIN account_trades ON account_trades.id = account_entries.entryable_id")
|
||||
.where("account_trades.security_id = ? AND account_trades.qty > 0 AND account_entries.date <= ?", security.id, date)
|
||||
.average(:price)
|
||||
|
||||
Money.new(avg_cost || price, currency)
|
||||
end
|
||||
|
||||
def trend
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue