2024-07-16 09:26:49 -04:00
|
|
|
class Account::Trade < ApplicationRecord
|
2024-08-09 11:22:57 -04:00
|
|
|
include Account::Entryable, Monetizable
|
|
|
|
|
|
|
|
monetize :price
|
2024-07-16 09:26:49 -04:00
|
|
|
|
|
|
|
belongs_to :security
|
|
|
|
|
2024-11-25 13:28:31 -05:00
|
|
|
validates :qty, presence: true
|
2024-08-09 11:22:57 -04:00
|
|
|
validates :price, :currency, presence: true
|
2024-07-16 09:26:49 -04:00
|
|
|
|
2024-10-09 14:59:18 -04:00
|
|
|
def unrealized_gain_loss
|
2024-12-20 11:37:26 -05:00
|
|
|
return nil if qty.negative?
|
2024-10-09 14:59:18 -04:00
|
|
|
current_price = security.current_price
|
|
|
|
return nil if current_price.nil?
|
|
|
|
|
|
|
|
current_value = current_price * qty.abs
|
|
|
|
cost_basis = price_money * qty.abs
|
|
|
|
|
|
|
|
TimeSeries::Trend.new(current: current_value, previous: cost_basis)
|
|
|
|
end
|
2024-07-16 09:26:49 -04:00
|
|
|
end
|