xirr 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f785329061a8687ef45c8877a36e8e831e6d0c5a
4
+ data.tar.gz: 595c1f7b779ee1b533ef738b514d904415a7e557
5
+ SHA512:
6
+ metadata.gz: 8f25151117351fa4c7aa083fbe6ec94bbddf8fe27630a15f9e03ad37fda01a574aca5703dddcba3a8c25662312cc01dd8b2822c7575b9d456f89bd1f60f13564
7
+ data.tar.gz: 30bb4e3ebeb6993fd1fc8bfeeca1a155f5b675983bc226bd81482629b1abdb71f804bafdb213c6c41fba498e168c5d81f01d7bba3cbdd4a3b7bbc2ffd4af395d
data/.coveralls.yml ADDED
@@ -0,0 +1,2 @@
1
+ service_name: travis-ci
2
+ repo_token: WWGlMyxTY9oj85CzGICcyYqlpXPG29UsN
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - '2.1.2'
4
+ - '2.0.0'
5
+ - '1.9.3'
6
+ script:
7
+ - bundle exec rake test_units
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in xirr.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 tubedude
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Xirr
2
+ [![Build Status](https://travis-ci.org/tubedude/xirr.svg)](https://travis-ci.org/tubedude/xirr)[![Coverage Status](https://img.shields.io/coveralls/tubedude/xirr.svg)](https://coveralls.io/r/tubedude/xirr)[![Code Climate](https://codeclimate.com/github/tubedude/xirr/badges/gpa.svg)](https://codeclimate.com/github/tubedude/xirr)
3
+
4
+ This is a simple gem to calculate XIRR on Bisection Method based on this gist http://puneinvestor.wordpress.com/2013/10/01/calculate-xirr-in-ruby-bisection-method/
5
+ and ont Finance gem https://github.com/wkranec/finance
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'xirr'
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install xirr
20
+
21
+ ## Usage
22
+
23
+
24
+ include Xirr
25
+
26
+ cf = Cashflow.new
27
+ cf << Transaction.new(-1000, date: '2014-01-01'.to_time(:utc))
28
+ cf << Transaction.new(-2000, date: '2014-03-01'.to_time(:utc))
29
+ cf << Transaction.new( 4500, date: '2015-12-01'.to_time(:utc))
30
+ cf.xirr
31
+ # 0.25159694345042327 # [Float]
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it ( https://github.com/tubedude/xirr/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake'
4
+ require 'rake/testtask'
5
+
6
+ task :default => [:test_units]
7
+
8
+ Rake::TestTask.new("test_units") do |t|
9
+ t.pattern = 'test/*.rb'
10
+ t.verbose = false
11
+ t.warning = false
12
+ end
13
+
data/lib/xirr.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'xirr/version'
2
+ require 'active_support/configurable'
3
+ require 'xirr/config'
4
+ require 'xirr/main'
5
+
6
+ module Xirr
7
+
8
+ autoload :Transaction, 'xirr/transaction'
9
+ autoload :Cashflow, 'xirr/cashflow'
10
+
11
+ end
@@ -0,0 +1,86 @@
1
+ module Xirr
2
+ class Cashflow < Array
3
+ include Xirr::Main
4
+
5
+ def initialize(*args) # :nodoc:
6
+ args.each { |a| self << a }
7
+ self.flatten!
8
+ end
9
+
10
+ # Check if Cashflow is invalid and raises ArgumentError
11
+ # retuns [Boolean]
12
+ def invalid?
13
+ if positives.empty? || negatives.empty?
14
+ raise ArgumentError, invalid_message
15
+ else
16
+ false
17
+ end
18
+ end
19
+
20
+ # Inverse of #invalid?
21
+ # returns [Boolean]
22
+ def valid?
23
+ !invalid?
24
+ end
25
+
26
+
27
+ # calculates a simple IRR guess based on period of investment and multiples
28
+ # returns [Float]
29
+ def irr_guess
30
+ ((multiple ** (1 / years_of_investment)) - 1).round(3)
31
+ end
32
+
33
+ def sum # :nodoc:
34
+ self.map(&:amount).sum
35
+ end
36
+
37
+ private
38
+
39
+ def first_transaction_direction # :nodoc:
40
+ self.sort! { |x,y| x.date <=> y.date }
41
+ self.first.amount / self.first.amount.abs
42
+ end
43
+
44
+ # Based on the direction of the first investment we create the multiple
45
+ def multiple # :nodoc:
46
+ if first_transaction_direction > 0
47
+ positives.sum(&:amount) / -negatives.sum(&:amount)
48
+ else
49
+ -negatives.sum(&:amount) / positives.sum(&:amount)
50
+ end
51
+ end
52
+
53
+ def years_of_investment # :nodoc:
54
+ (max_date - min_date) / (365 * 24 * 60 * 60).to_f
55
+ end
56
+
57
+ def max_date # :nodoc:
58
+ @max_date ||= self.map(&:date).max
59
+ end
60
+
61
+ def min_date # :nodoc:
62
+ @min_date ||= self.map(&:date).min
63
+ end
64
+
65
+ def positives # :nodoc:
66
+ split_transactions
67
+ @positives
68
+ end
69
+
70
+ def negatives # :nodoc:
71
+ split_transactions
72
+ @negatives
73
+ end
74
+
75
+ def split_transactions # :nodoc:
76
+ @negatives, @positives = self.partition { |x| x.amount >= 0 } # Inverted as negative amount is good
77
+ end
78
+
79
+ def invalid_message # :nodoc:
80
+ return 'No positive transaction' if positives.empty?
81
+ return 'No negatives transaction' if negatives.empty?
82
+ end
83
+
84
+ end
85
+
86
+ end
@@ -0,0 +1,12 @@
1
+ module Xirr
2
+ include ActiveSupport::Configurable
3
+
4
+ default_values = {
5
+ eps: '1.0e-12',
6
+ days_in_year: 365,
7
+ }
8
+
9
+ default_values.each do |key, value|
10
+ self.config.send("#{key.to_sym}=", value)
11
+ end
12
+ end
data/lib/xirr/main.rb ADDED
@@ -0,0 +1,74 @@
1
+ require 'active_support/concern'
2
+
3
+ module Xirr
4
+
5
+ module Main
6
+ extend ActiveSupport::Concern
7
+
8
+ # Calculates yearly Internal Rate of Return
9
+ # returns [Float]
10
+ def xirr(guess = nil)
11
+
12
+ self.valid?
13
+
14
+ # Bisection method finding the rate to zero nfv
15
+
16
+ days_in_year = Xirr.config.days_in_year.to_f
17
+
18
+ left = -0.99/days_in_year
19
+ right = 9.99/days_in_year
20
+ epsilon = Xirr.config.eps.to_f
21
+
22
+ guess = self.irr_guess.to_f
23
+
24
+ while ((right-left).abs > 2 * epsilon) do
25
+
26
+ midpoint = guess || (right + left)/2
27
+ guess = nil
28
+
29
+ if (nfv(left) * nfv(midpoint) > 0)
30
+
31
+ left = midpoint
32
+
33
+ else
34
+
35
+ right = midpoint
36
+
37
+ end
38
+
39
+ end
40
+
41
+ # Irr for daily cashflow (not in percentage format)
42
+ irr = (right+left) / 2
43
+ # Irr for daily cashflow multiplied by 365 to get yearly return
44
+ irr = irr * days_in_year
45
+ # Annualized yield (return) reflecting compounding effect of daily returns
46
+ irr = (1 + irr / days_in_year) ** days_in_year - 1
47
+
48
+ return irr
49
+
50
+ end
51
+
52
+ private
53
+
54
+ def nfv(rate) # :nodoc:
55
+
56
+ today = self.map(&:date).max.to_date
57
+ nfv = 0
58
+ self.each do |t|
59
+ cf, date = t.amount, t.date
60
+
61
+ datestring = date.to_s
62
+ formatteddate = Date.parse(datestring).to_date
63
+ t_in_days = (today - formatteddate).numerator / (today - formatteddate).denominator
64
+ nfv = nfv + cf * ((1 + rate) ** t_in_days)
65
+
66
+ end
67
+ return nfv
68
+
69
+ end
70
+
71
+ end
72
+
73
+ end
74
+
@@ -0,0 +1,43 @@
1
+ module Xirr
2
+
3
+ class Transaction
4
+ attr_reader :amount
5
+ attr_accessor :date
6
+
7
+ def initialize(amount, opts={})
8
+ @amount = amount
9
+ @original = amount
10
+
11
+ # Set optional attributes..
12
+ opts.each do |key, value|
13
+ send("#{key}=", value)
14
+ end
15
+ end
16
+
17
+ def amount=(value)
18
+ @amount = value.to_f || 0
19
+ end
20
+
21
+ def inspect
22
+ "T(#{@amount},#{@date})"
23
+ end
24
+
25
+ def description
26
+ investment ? "#{self.investment.transaction_type_name}: #{round.description}" : @description
27
+ end
28
+
29
+ def round
30
+ @investment.nil? ? nil : investment.round
31
+ end
32
+
33
+ def company
34
+ @company || investment.company
35
+ end
36
+
37
+ def shareholder
38
+ @shareholder || investment.shareholder
39
+ end
40
+
41
+ end
42
+
43
+ end
@@ -0,0 +1,3 @@
1
+ module Xirr
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,32 @@
1
+ require_relative 'test_helper'
2
+
3
+ describe 'Cashflows' do
4
+
5
+ describe 'an array of Transactions' do
6
+ before(:all) do
7
+ @xactions = Cashflow.new
8
+ @xactions << Transaction.new( 1000, date: '1985-01-01'.to_time(:utc))
9
+ @xactions << Transaction.new( -600, date: '1990-01-01'.to_time(:utc))
10
+ @xactions << Transaction.new(-6000, date: '1995-01-01'.to_time(:utc))
11
+ end
12
+
13
+ it 'haves an Internal Rate of Return' do
14
+ assert_equal '0.225683'.to_f, @xactions.xirr.round(6)
15
+ end
16
+
17
+ it 'has an educated guess' do
18
+ assert_equal '0.196'.to_f, @xactions.irr_guess.round(6)
19
+ end
20
+
21
+ describe 'an invalid array of Transactions' do
22
+ it 'should have an Internal Rate of Return' do
23
+ @xactions = Cashflow.new
24
+ @xactions << Transaction.new(-600, date: '1990-01-01'.to_time(:utc))
25
+ @xactions << Transaction.new(-600, date: '1995-01-01'.to_time(:utc))
26
+ assert_raises(ArgumentError) { @xactions.valid? }
27
+ end
28
+ end
29
+
30
+ end
31
+
32
+ end
@@ -0,0 +1,13 @@
1
+ require 'coveralls'
2
+ Coveralls.wear!
3
+
4
+ require 'minitest/autorun'
5
+ require 'minitest/spec'
6
+
7
+ require 'active_support/all'
8
+
9
+ require 'xirr/config.rb'
10
+ require 'xirr/main.rb'
11
+ require 'xirr/cashflow.rb'
12
+ require 'xirr/transaction.rb'
13
+ include Xirr
data/xirr.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'xirr/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'xirr'
8
+ spec.version = Xirr::VERSION
9
+ spec.authors = ['tubedude']
10
+ spec.email = ['beto@trevisan.me']
11
+ spec.summary = %q{Calculates XIRR (Bisection method) of a cashflow}
12
+ spec.description = %q{This gem calculates XIRR of a cashflow on the (Bisection Method). It was heavily based the much more complete https://github.com/wkranec/finance and http://puneinvestor.wordpress.com/2013/10/01/calculate-xirr-in-ruby-bisection-method/. }
13
+ spec.homepage = 'https://github.com/tubedude/xirr'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.6'
22
+ spec.add_development_dependency 'rake','~> 0'
23
+
24
+ spec.required_ruby_version = '>=1.9'
25
+ spec.add_dependency 'activesupport', '~> 4.0'
26
+ spec.add_development_dependency 'minitest', '~> 4.7'
27
+ spec.add_development_dependency 'coveralls','~> 0'
28
+
29
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: xirr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - tubedude
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '4.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '4.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '4.7'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '4.7'
69
+ - !ruby/object:Gem::Dependency
70
+ name: coveralls
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: 'This gem calculates XIRR of a cashflow on the (Bisection Method). It
84
+ was heavily based the much more complete https://github.com/wkranec/finance and
85
+ http://puneinvestor.wordpress.com/2013/10/01/calculate-xirr-in-ruby-bisection-method/. '
86
+ email:
87
+ - beto@trevisan.me
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".coveralls.yml"
93
+ - ".gitignore"
94
+ - ".travis.yml"
95
+ - Gemfile
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - lib/xirr.rb
100
+ - lib/xirr/cashflow.rb
101
+ - lib/xirr/config.rb
102
+ - lib/xirr/main.rb
103
+ - lib/xirr/transaction.rb
104
+ - lib/xirr/version.rb
105
+ - test/test_cashflow.rb
106
+ - test/test_helper.rb
107
+ - xirr.gemspec
108
+ homepage: https://github.com/tubedude/xirr
109
+ licenses:
110
+ - MIT
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '1.9'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.4.1
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: Calculates XIRR (Bisection method) of a cashflow
132
+ test_files:
133
+ - test/test_cashflow.rb
134
+ - test/test_helper.rb