Class: Xirr::Cashflow
- Inherits:
-
Array
- Object
- Array
- Xirr::Cashflow
- Defined in:
- lib/xirr/cashflow.rb
Overview
A Cashflow should consist of at least two transactions, one positive and one negative.
Expands [Array] to store a set of transactions which will be used to calculate the XIRR
Instance Attribute Summary collapse
-
#fallback ⇒ Object
readonly
Returns the value of attribute fallback.
-
#iteration_limit ⇒ Object
readonly
Returns the value of attribute iteration_limit.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#raise_exception ⇒ Object
readonly
Returns the value of attribute raise_exception.
Instance Method Summary collapse
- #<<(arg) ⇒ Object
- #compact_cf ⇒ Object
-
#initialize(flow: [], period: Xirr.config.period, **options) ⇒ Cashflow
constructor
A new instance of Cashflow.
-
#invalid? ⇒ Boolean
Check if Cashflow is invalid.
-
#invalid_message ⇒ String
Error message depending on the missing transaction.
-
#irr_guess ⇒ Float
Calculates a simple IRR guess based on period of investment and multiples.
-
#max_date ⇒ Time
Last investment date.
-
#min_date ⇒ Time
First investment date.
- #other_calculation_method(method) ⇒ Object
- #period ⇒ Object
- #process_options(method, options) ⇒ Object
-
#sum ⇒ Float
Sums all amounts in a cashflow.
-
#switch_fallback(method) ⇒ Symbol
If method is defined it will turn off fallback it return either the provided method or the system default.
-
#valid? ⇒ Boolean
Inverse of #invalid?.
- #xirr(guess: nil, method: nil, **options) ⇒ Float
Constructor Details
#initialize(flow: [], period: Xirr.config.period, **options) ⇒ Cashflow
Returns a new instance of Cashflow.
16 17 18 19 20 21 22 |
# File 'lib/xirr/cashflow.rb', line 16 def initialize(flow: [], period: Xirr.config.period, ** ) @period = period @fallback = [:fallback] || Xirr.config.fallback = self << flow flatten! end |
Instance Attribute Details
#fallback ⇒ Object (readonly)
Returns the value of attribute fallback.
7 8 9 |
# File 'lib/xirr/cashflow.rb', line 7 def fallback @fallback end |
#iteration_limit ⇒ Object (readonly)
Returns the value of attribute iteration_limit.
7 8 9 |
# File 'lib/xirr/cashflow.rb', line 7 def iteration_limit @iteration_limit end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/xirr/cashflow.rb', line 7 def end |
#raise_exception ⇒ Object (readonly)
Returns the value of attribute raise_exception.
7 8 9 |
# File 'lib/xirr/cashflow.rb', line 7 def raise_exception @raise_exception end |
Instance Method Details
#<<(arg) ⇒ Object
120 121 122 123 124 |
# File 'lib/xirr/cashflow.rb', line 120 def <<(arg) super arg sort! { |x, y| x.date <=> y.date } self end |
#compact_cf ⇒ Object
96 97 98 99 100 101 |
# File 'lib/xirr/cashflow.rb', line 96 def compact_cf # self compact = Hash.new 0 each { |flow| compact[flow.date] += flow.amount } Cashflow.new flow: compact.map { |key, value| Transaction.new(value, date: key) }, options: , period: period end |
#invalid? ⇒ Boolean
Check if Cashflow is invalid
26 27 28 |
# File 'lib/xirr/cashflow.rb', line 26 def invalid? inflow.empty? || outflows.empty? end |
#invalid_message ⇒ String
Error message depending on the missing transaction
111 112 113 114 |
# File 'lib/xirr/cashflow.rb', line 111 def return 'No positive transaction' if inflow.empty? return 'No negative transaction' if outflows.empty? end |
#irr_guess ⇒ Float
Calculates a simple IRR guess based on period of investment and multiples.
50 51 52 53 54 |
# File 'lib/xirr/cashflow.rb', line 50 def irr_guess return @irr_guess = 0.0 if periods_of_investment.zero? @irr_guess = valid? ? ((multiple**(1 / periods_of_investment)) - 1).round(3) : 0.0 @irr_guess == 1.0 / 0 ? 0.0 : @irr_guess end |
#max_date ⇒ Time
Last investment date
44 45 46 |
# File 'lib/xirr/cashflow.rb', line 44 def max_date @max_date ||= map(&:date).max end |
#min_date ⇒ Time
First investment date
105 106 107 |
# File 'lib/xirr/cashflow.rb', line 105 def min_date @min_date ||= map(&:date).min end |
#other_calculation_method(method) ⇒ Object
92 93 94 |
# File 'lib/xirr/cashflow.rb', line 92 def other_calculation_method(method) method == :newton_method ? :bisection : :newton_method end |
#period ⇒ Object
116 117 118 |
# File 'lib/xirr/cashflow.rb', line 116 def period @temporary_period || @period end |
#process_options(method, options) ⇒ Object
71 72 73 74 75 76 |
# File 'lib/xirr/cashflow.rb', line 71 def (method, ) @temporary_period = [:period] [:raise_exception] ||= [:raise_exception] || Xirr.config.raise_exception [:iteration_limit] ||= [:iteration_limit] || Xirr.config.iteration_limit return switch_fallback(method), end |
#sum ⇒ Float
Sums all amounts in a cashflow
38 39 40 |
# File 'lib/xirr/cashflow.rb', line 38 def sum map(&:amount).sum end |
#switch_fallback(method) ⇒ Symbol
If method is defined it will turn off fallback it return either the provided method or the system default
82 83 84 85 86 87 88 89 90 |
# File 'lib/xirr/cashflow.rb', line 82 def switch_fallback(method) if method @fallback = false method else @fallback = Xirr.config.fallback Xirr.config.default_method end end |
#valid? ⇒ Boolean
Inverse of #invalid?
32 33 34 |
# File 'lib/xirr/cashflow.rb', line 32 def valid? !invalid? end |
#xirr(guess: nil, method: nil, **options) ⇒ Float
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/xirr/cashflow.rb', line 59 def xirr(guess: nil, method: nil, ** ) method, = (method, ) if invalid? raise ArgumentError, if [:raise_exception] BigDecimal(0, Xirr.config.precision) else xirr = choose_(method).send :xirr, guess, xirr = choose_(other_calculation_method(method)).send(:xirr, guess, ) if (xirr.nil? || xirr.nan?) && fallback xirr || Xirr.config.replace_for_nil end end |