Class: Gnucash::Security

Inherits:
Object
  • Object
show all
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.

Since:

  • 1.6.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Gnucash::Support::LightInspect

#inspect

Constructor Details

#initialize(book, space, id) ⇒ Security

Returns a new instance of Security.

Parameters:

  • book (Book)

    Parent book.

  • space (String)

    Commodity space.

  • id (String)

    Commodity id.

Since:

  • 1.6.0



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

#idString (readonly)

Returns Commodity id (e.g. ticker or currency code).

Returns:

  • (String)

    Commodity id (e.g. ticker or currency code).

Since:

  • 1.6.0



46
47
48
# File 'lib/gnucash/security.rb', line 46

def id
  @id
end

#spaceString (readonly)

Returns Commodity namespace (e.g. “NASDAQ”, “ISO4217”).

Returns:

  • (String)

    Commodity namespace (e.g. “NASDAQ”, “ISO4217”).

Since:

  • 1.6.0



43
44
45
# File 'lib/gnucash/security.rb', line 43

def space
  @space
end

Instance Method Details

#attributesObject

Since:

  • 1.6.0



112
113
114
# File 'lib/gnucash/security.rb', line 112

def attributes
  %i[space id isin]
end

#isinString?

Returns ISIN from the commodity definition (cmdty:xcode or ISIN slot), normalized.

Returns:

  • (String, nil)

    ISIN from the commodity definition (cmdty:xcode or ISIN slot), normalized.

Since:

  • 1.6.0



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.

Parameters:

  • date (String, Date)

    Valuation date.

  • currency_space (String, nil) (defaults to: nil)

    Restrict to this quote currency space.

  • currency_id (String, nil) (defaults to: nil)

    Restrict to this quote currency id.

Returns:

  • (SecurityQuote, nil)

    Quote used for valuation, or nil if none applies.

Since:

  • 1.6.0



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