xirr 0.5.3 → 0.5.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9c796231bbce66c746f671780a5852d5f17460af
4
- data.tar.gz: b3b3df3ef32bd3c214dfc03d705a2879b29671d1
3
+ metadata.gz: 19f0ca0058f57982916f0a8e2b96ceb6717a6dc4
4
+ data.tar.gz: e2467167fa94e1f7132815933034502a0a999b9a
5
5
  SHA512:
6
- metadata.gz: 270c0fc99c7796f2dba8102762b3fb3f456c49a406f36c0d422b3df0a7c8ec6b34cb212e882080d260d06b6b28376e63d24a24544f927f44ad2649d22757e9af
7
- data.tar.gz: 8f84c875fd3f3e64ead299239c40757144175d7695cf4ab5c8a14c75633f329e187956b4d1225b1f78a57f407405f2d0de320c4f4a750b33fa8327dccf337748
6
+ metadata.gz: d6ab5e1fe60ef7859c938938b23ca06e01b148e2c1e5cb9cd8fb82f4d15e9ba30818abea6004440da26ad90016c31191bab62cde26c7e635d78ac64b3a3b0a0f
7
+ data.tar.gz: 38da74ebbfe057d53139d13d25d73553db8edb1b6fbe5492dfac8816920ee0efab77ae2fb21a7edc5dfc7039cb5b77a6e7ec0a91f1b0c08d084db23affb700e8
data/.gitignore CHANGED
Binary file
@@ -1,3 +1,6 @@
1
+ ## Version 0.5.4
2
+ * Fallsback If Newton Methods returns NaN
3
+
1
4
  ## Version 0.5.3
2
5
  * Better tests
3
6
  * added period option to Xirr
@@ -62,7 +62,7 @@ module Xirr
62
62
  BigDecimal.new(0, Xirr::PRECISION)
63
63
  else
64
64
  xirr = choose_(method).send :xirr, guess, options
65
- xirr = choose_(other_calculation_method(method)).send(:xirr, guess, options) if xirr.nil? && fallback
65
+ xirr = choose_(other_calculation_method(method)).send(:xirr, guess, options) if (xirr.nil? || xirr.nan?) && fallback
66
66
  xirr || Xirr::REPLACE_FOR_NIL
67
67
  end
68
68
  end
@@ -116,6 +116,12 @@ module Xirr
116
116
  @temporary_period || @period
117
117
  end
118
118
 
119
+ def << arg
120
+ super arg
121
+ self.sort! { |x, y| x.date <=> y.date }
122
+ self
123
+ end
124
+
119
125
  private
120
126
 
121
127
  # @param method [Symbol]
@@ -138,8 +144,8 @@ module Xirr
138
144
  # This implies the first transaction is a disbursement
139
145
  # @return [Integer]
140
146
  def first_transaction_direction
141
- self.sort! { |x, y| x.date <=> y.date }
142
- self.first.amount / self.first.amount.abs
147
+ # self.sort! { |x, y| x.date <=> y.date }
148
+ @first_transaction_direction ||= self.first.amount / self.first.amount.abs
143
149
  end
144
150
 
145
151
  # Based on the direction of the first investment finds the multiple cash-on-cash
@@ -148,8 +154,7 @@ module Xirr
148
154
  # @api private
149
155
  # @return [Float]
150
156
  def multiple
151
- result = inflow.sum(&:amount) / -outflows.sum(&:amount)
152
- first_transaction_positive? ? result : 1 / result
157
+ inflow.sum(&:amount).abs / outflows.sum(&:amount).abs
153
158
  end
154
159
 
155
160
  def first_transaction_positive?
@@ -168,7 +173,7 @@ module Xirr
168
173
  # @see #outflows
169
174
  # Selects all positives transactions from Cashflow
170
175
  def inflow
171
- self.select { |x| x.amount < 0 }
176
+ self.select { |x| x.amount * first_transaction_direction < 0 }
172
177
  end
173
178
 
174
179
  # @api private
@@ -176,7 +181,7 @@ module Xirr
176
181
  # @see #inflow
177
182
  # Selects all negatives transactions from Cashflow
178
183
  def outflows
179
- self.select { |x| x.amount > 0 }
184
+ self.select { |x| x.amount * first_transaction_direction > 0 }
180
185
  end
181
186
 
182
187
  end
@@ -48,7 +48,8 @@ module Xirr
48
48
  rate = [guess || cf.irr_guess]
49
49
  begin
50
50
  nlsolve(func, rate)
51
- rate[0] <= -1 ? nil : rate[0].round(Xirr::PRECISION)
51
+ (rate[0] <= -1 || rate[0].nan?) ? nil : rate[0].round(Xirr::PRECISION)
52
+
52
53
  # rate[0].round(Xirr::PRECISION)
53
54
  rescue
54
55
  nil
@@ -1,4 +1,4 @@
1
1
  module Xirr
2
2
  # Version of the Gem
3
- VERSION = '0.5.3'
3
+ VERSION = '0.5.4'
4
4
  end
@@ -289,7 +289,15 @@ describe 'Cashflows' do
289
289
  cf << Transaction.new(500, date: '2014-10-19'.to_date)
290
290
  assert_equal '-0.996814607'.to_f.round(3), cf.xirr.to_f.round(3)
291
291
  end
292
+ end
293
+ describe 'marano' do
292
294
 
295
+ it 'it matchs Excel' do
296
+ cf = Cashflow.new
297
+ cf << Transaction.new(900.0, date: '2014-11-07'.to_date)
298
+ cf << Transaction.new(-13.5, date: '2015-05-06'.to_date)
299
+ assert_equal '-0.9998'.to_f.round(4), cf.xirr.to_f.round(4)
300
+ end
293
301
  end
294
302
 
295
303
  describe 'period' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xirr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.3
4
+ version: 0.5.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - tubedude
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-28 00:00:00.000000000 Z
11
+ date: 2015-05-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -102,7 +102,6 @@ executables: []
102
102
  extensions: []
103
103
  extra_rdoc_files: []
104
104
  files:
105
- - ".DS_Store"
106
105
  - ".coveralls.yml"
107
106
  - ".gitignore"
108
107
  - ".ruby-gemset"
data/.DS_Store DELETED
Binary file