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
|
|
|
|
|
|
|
|
validates :qty, presence: true, numericality: { other_than: 0 }
|
2024-08-09 11:22:57 -04:00
|
|
|
validates :price, :currency, presence: true
|
2024-07-16 09:26:49 -04:00
|
|
|
|
|
|
|
class << self
|
|
|
|
def search(_params)
|
|
|
|
all
|
|
|
|
end
|
|
|
|
|
|
|
|
def requires_search?(_params)
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def sell?
|
|
|
|
qty < 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def buy?
|
|
|
|
qty > 0
|
|
|
|
end
|
2024-10-09 14:59:18 -04:00
|
|
|
|
2024-11-04 20:27:31 -05:00
|
|
|
def name
|
|
|
|
prefix = sell? ? "Sell " : "Buy "
|
|
|
|
generated = prefix + "#{qty.abs} shares of #{security.ticker}"
|
|
|
|
entry.name || generated
|
|
|
|
end
|
|
|
|
|
2024-10-09 14:59:18 -04:00
|
|
|
def unrealized_gain_loss
|
|
|
|
return nil if sell?
|
|
|
|
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
|