Class: Gnucash::Security
- Inherits:
-
Object
- Object
- Gnucash::Security
- Includes:
- Gnucash::Support::LightInspect
- Defined in:
- lib/gnucash/security.rb
Overview
A security (stock, fund, etc.) identified by its GnuCash commodity space and id, with prices loaded from the book’s price database.
Instance Attribute Summary collapse
-
#id ⇒ String
readonly
Commodity id (e.g. ticker or currency code).
-
#space ⇒ String
readonly
Commodity namespace (e.g. “NASDAQ”, “ISO4217”).
Instance Method Summary collapse
- #attributes ⇒ Object
-
#initialize(book, space, id) ⇒ Security
constructor
A new instance of Security.
-
#isin ⇒ String?
ISIN from the commodity definition (
cmdty:xcodeor ISIN slot), normalized. -
#value_on(date, currency_space: nil, currency_id: nil) ⇒ SecurityQuote?
Return the most recent price quote whose date is on or before the given valuation date.
Methods included from Gnucash::Support::LightInspect
Constructor Details
#initialize(book, space, id) ⇒ Security
Returns a new instance of Security.
51 52 53 54 55 |
# File 'lib/gnucash/security.rb', line 51 def initialize(book, space, id) @book = book @space = space @id = id end |
Instance Attribute Details
#id ⇒ String (readonly)
Returns Commodity id (e.g. ticker or currency code).
46 47 48 |
# File 'lib/gnucash/security.rb', line 46 def id @id end |
#space ⇒ String (readonly)
Returns Commodity namespace (e.g. “NASDAQ”, “ISO4217”).
43 44 45 |
# File 'lib/gnucash/security.rb', line 43 def space @space end |
Instance Method Details
#attributes ⇒ Object
112 113 114 |
# File 'lib/gnucash/security.rb', line 112 def attributes %i[space id isin] end |
#isin ⇒ String?
Returns ISIN from the commodity definition (cmdty:xcode or ISIN slot), normalized.
58 59 60 |
# File 'lib/gnucash/security.rb', line 58 def isin @book.isin_for_commodity(@space, @id) end |
#value_on(date, currency_space: nil, currency_id: nil) ⇒ SecurityQuote?
Return the most recent price quote whose date is on or before the given valuation date.
If the security has multiple quote currencies, currency_space and currency_id select one; if omitted, USD (ISO4217 / USD) is preferred when present, otherwise an arbitrary quote chain is used.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/gnucash/security.rb', line 74 def value_on(date, currency_space: nil, currency_id: nil) date = Date.parse(date) if date.is_a?(String) if (currency_space.nil? ^ currency_id.nil?) raise ArgumentError, "currency_space and currency_id must both be set or both omitted" end quotes = @book.quotes_for_commodity(@space, @id) return nil if quotes.empty? filtered = if currency_space quotes.select { |q| q.currency_space == currency_space && q.currency_id == currency_id } else quotes end return nil if filtered.empty? pick_currency = lambda do |list| usd = list.select { |q| q.currency_space == "ISO4217" && q.currency_id == "USD" } (usd.empty? ? list : usd) end candidates = currency_space ? filtered : pick_currency.call(filtered) return nil if candidates.empty? candidates = candidates.select { |q| q.date <= date } return nil if candidates.empty? best = candidates.max_by(&:date) SecurityQuote.new( value: best.value, currency_space: best.currency_space, currency_id: best.currency_id, date: best.date ) end |