Class: Apy::Interest
Overview
An interest object; Given an apy & date range, prepares various methods to calculate with
Instance Attribute Summary collapse
-
#apy ⇒ Object
readonly
Returns the value of attribute apy.
Class Method Summary collapse
-
.get_term_size(start_date, end_date, days_per_term) ⇒ Integer
The actual number of terms completed.
Instance Method Summary collapse
-
#dca(in_per_split, times: 1) ⇒ Object
Given a series of investments, calculate the DCA return.
-
#initialize(apy:, start_date: Date.today, end_date: Date.today.next_year, days_per_term: 365) ⇒ Interest
constructor
A new instance of Interest.
-
#total(principal, times: 1) ⇒ Object
Given a principal amount, return the ending balance.
Methods included from Calculable
#compound, #dca_compound, #weighted_harmonic_mean
Constructor Details
#initialize(apy:, start_date: Date.today, end_date: Date.today.next_year, days_per_term: 365) ⇒ Interest
Returns a new instance of Interest.
30 31 32 33 34 35 |
# File 'lib/apy/interest.rb', line 30 def initialize(apy:, start_date: Date.today, end_date: Date.today.next_year, days_per_term: 365) fail(ArgumentError, "apy must be a positive Float") unless apy.positive? && apy.is_a?(Float) @apy = apy @terms = Interest.get_term_size(start_date, end_date, days_per_term) end |
Instance Attribute Details
#apy ⇒ Object (readonly)
Returns the value of attribute apy.
11 12 13 |
# File 'lib/apy/interest.rb', line 11 def apy @apy end |
Class Method Details
.get_term_size(start_date, end_date, days_per_term) ⇒ Integer
Returns The actual number of terms completed.
40 41 42 |
# File 'lib/apy/interest.rb', line 40 def get_term_size(start_date, end_date, days_per_term) ((end_date - start_date) / days_per_term).round end |
Instance Method Details
#dca(in_per_split, times: 1) ⇒ Object
This assumes you wish to maximize dca returns; the in_per_split is deposited before the interest is calculated
If this method is too inflexible for your use case, you should use the Calculable module directly
Given a series of investments, calculate the DCA return
69 70 71 72 73 74 75 |
# File 'lib/apy/interest.rb', line 69 def dca(in_per_split, times: 1) split = @terms * times adjusted_apy = apy / times range = split.times.map { [in_per_split, adjusted_apy] } dca_compound(range, times: times) end |
#total(principal, times: 1) ⇒ Object
Given a principal amount, return the ending balance
49 50 51 |
# File 'lib/apy/interest.rb', line 49 def total(principal, times: 1) compound(principal, rate: apy, times: times, terms: @terms) end |