roi_calculator 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7e290e6bc3925af025fe8725c0ab6bd573dde0c3
4
+ data.tar.gz: db78febf3722059d1fbaf9a60bb920374ec83b73
5
+ SHA512:
6
+ metadata.gz: deded280631b0fa49cbf78771c0bffd2e8479db5a9537e78e356f4330b71b3233b7068e81c8f25fd78ecf3a580c8e5e02d88d257232ceb270c0ff0a0693e27b8
7
+ data.tar.gz: 5f6d733d5e411c3f6c7240e342544a540856bdb1401ed2add769e5f07dd37ba5a4d6731f89d5c41462fbdf554ed79e9b4ca74d0f83b23373cea252a239df90d9
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.2.5
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.5
5
+ before_install: gem install bundler -v 1.13.7
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "math_util", git: 'https://github.com/abacha/math_util.git'
4
+ gem "work_day", git: 'https://github.com/abacha/work_day.git'
5
+
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,62 @@
1
+ GIT
2
+ remote: https://github.com/abacha/math_util.git
3
+ revision: 1e4a8082af06cd5bb83a978eae9de50943844429
4
+ specs:
5
+ math_util (0.1.0)
6
+
7
+ GIT
8
+ remote: https://github.com/abacha/work_day.git
9
+ revision: 1dd12e52dd894e4c169d1966bfaf9545452f4572
10
+ specs:
11
+ work_day (0.1.3)
12
+ activesupport (~> 4.1)
13
+
14
+ PATH
15
+ remote: .
16
+ specs:
17
+ roi_calculator (0.1.2)
18
+ memoist (~> 0.15)
19
+
20
+ GEM
21
+ remote: https://rubygems.org/
22
+ specs:
23
+ activesupport (4.2.7.1)
24
+ i18n (~> 0.7)
25
+ json (~> 1.7, >= 1.7.7)
26
+ minitest (~> 5.1)
27
+ thread_safe (~> 0.3, >= 0.3.4)
28
+ tzinfo (~> 1.1)
29
+ diff-lcs (1.3)
30
+ i18n (0.8.0)
31
+ json (1.8.6)
32
+ memoist (0.15.0)
33
+ minitest (5.10.1)
34
+ rspec (3.5.0)
35
+ rspec-core (~> 3.5.0)
36
+ rspec-expectations (~> 3.5.0)
37
+ rspec-mocks (~> 3.5.0)
38
+ rspec-core (3.5.4)
39
+ rspec-support (~> 3.5.0)
40
+ rspec-expectations (3.5.0)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.5.0)
43
+ rspec-mocks (3.5.0)
44
+ diff-lcs (>= 1.2.0, < 2.0)
45
+ rspec-support (~> 3.5.0)
46
+ rspec-support (3.5.0)
47
+ thread_safe (0.3.5)
48
+ tzinfo (1.2.2)
49
+ thread_safe (~> 0.1)
50
+
51
+ PLATFORMS
52
+ ruby
53
+
54
+ DEPENDENCIES
55
+ bundler (~> 1.13)
56
+ math_util!
57
+ roi_calculator!
58
+ rspec (~> 3.0)
59
+ work_day!
60
+
61
+ BUNDLED WITH
62
+ 1.14.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Adriano Bacha
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # RoiCalculator
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/roi_calculator`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'roi_calculator'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install roi_calculator
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/roi_calculator. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
@@ -0,0 +1,64 @@
1
+ require "work_day"
2
+ require "active_support"
3
+ require "active_support/core_ext/date"
4
+
5
+ module ROI
6
+ class FinancialResult
7
+ attr_reader :start_date, :end_date, :rentabilities
8
+
9
+ def initialize(rentabilities, start_date, end_date)
10
+ @rentabilities = rentabilities
11
+ @start_date = start_date
12
+ @end_date = end_date
13
+ end
14
+
15
+ def current_month
16
+ scope_start = WorkDay.next_after(end_date.beginning_of_month, 0)
17
+ format_financial(scope_start, end_date)
18
+ end
19
+
20
+ def months(amount)
21
+ scope_start = (end_date - amount.months + 1.month).beginning_of_month
22
+ check_gaps_and_format(scope_start, end_date)
23
+ end
24
+
25
+ def current_year
26
+ scope_start = WorkDay.next_after(end_date.beginning_of_year, 0)
27
+ format_financial(scope_start, end_date)
28
+ end
29
+
30
+ def years_ago(amount)
31
+ scope_end = (end_date - amount.year).end_of_year
32
+ scope_start = scope_end.beginning_of_year
33
+ format_financial(scope_start, scope_end)
34
+ end
35
+
36
+ def portfolio
37
+ format_financial(start_date, end_date)
38
+ end
39
+
40
+ private
41
+ def scoped_rentabilities(scope_start, scope_end)
42
+ rentabilities.values.select.each do |line|
43
+ line.date.between?(scope_start, scope_end)
44
+ end
45
+ end
46
+
47
+ def format_financial(scope_start, scope_end)
48
+ return unless scope_start && scope_end
49
+ total = scoped_rentabilities(scope_start, scope_end).sum do |line|
50
+ line.try(:daily_result) || 0
51
+ end.round(2)
52
+ total != 0 ? total : nil
53
+ end
54
+
55
+ def check_gaps_and_format(scope_start, scope_end)
56
+ if scope_start >= start_date
57
+ scoped_rentabilities(scope_start, scope_end).each do |line|
58
+ return if line.try(:have_position) == false
59
+ end
60
+ format_financial(scope_start, scope_end)
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,109 @@
1
+ require "memoist"
2
+ require "roi/financial_result"
3
+ require "roi/rentability_periods"
4
+ require "math_util"
5
+ require "active_support"
6
+ require "active_support/core_ext"
7
+
8
+ module ROI
9
+ class Rentability
10
+ extend Memoist
11
+ include RentabilityPeriods
12
+ attr_accessor :rentabilities
13
+ attr_reader :id, :start_date, :end_date, :skip_on_gap
14
+
15
+ def initialize(options)
16
+ @id = options[:id]
17
+ @start_date = options[:start_date]
18
+ @end_date = options[:end_date] || Date.today - 1
19
+ @skip_on_gap = false
20
+ post_initialize(options)
21
+ end
22
+
23
+ def post_initialize(options)
24
+ end
25
+
26
+ def calculate
27
+ parse_rentabilities
28
+ if @rentabilities.count > 0
29
+ dates = @rentabilities.keys
30
+ @start_date, @end_date = [dates[0], dates[-1]]
31
+ end
32
+ self
33
+ end
34
+
35
+ def financial
36
+ FinancialResult.new(rentabilities, start_date, end_date)
37
+ end
38
+
39
+ def dates_with_position
40
+ rentabilities.select { |_, rentability| rentability.have_position }.keys
41
+ end
42
+
43
+ def rentabilities_array(start_date = nil, end_date = nil)
44
+ start_date ||= @start_date
45
+ end_date ||= @end_date
46
+
47
+ filtered = @rentabilities.values.select do |val|
48
+ val.date.between?(start_date, end_date)
49
+ end
50
+ filtered.map(&:rentability).compact
51
+ end
52
+
53
+ def volatility(months_ago = nil)
54
+ _start = months_ago ?
55
+ (end_date - months_ago.months + 1.month).beginning_of_month : nil
56
+ (volatility_in_window(nil, _start).first || [])[1]
57
+ end
58
+
59
+ def volatility_in_window(days = nil, start_date = nil)
60
+ filtered = rentabilities_array(start_date)
61
+ days ||= filtered.size
62
+
63
+ 0.upto(filtered.size - days).map do |low|
64
+ series = filtered.slice(low, days + 1)
65
+ vol = MathUtil.volatility(series)
66
+ date = dates_with_position.fetch(low + days) { dates_with_position.last }
67
+ [date.to_time.to_i, vol] if date
68
+ end
69
+ end
70
+
71
+ def to_a
72
+ dates_with_position.map do |date|
73
+ if @rentabilities[date]
74
+ [date.to_time.to_i, @rentabilities[date].share]
75
+ end
76
+ end.compact
77
+ end
78
+
79
+ protected
80
+ def before_parse_rentabilities
81
+ end
82
+
83
+ def work_days
84
+ start_date && end_date ? WorkDay.between(start_date, end_date) : []
85
+ end
86
+ memoize :work_days
87
+
88
+ private
89
+ def parse_rentabilities
90
+ @rentabilities = {}
91
+ before_parse_rentabilities
92
+ work_days.each_cons(2) do |last_date, date|
93
+ parse(date, last_date, @rentabilities)
94
+ end
95
+ end
96
+
97
+ def parse(this_date, last_date, rentabilities)
98
+ this_value = rentabilities_source(this_date)
99
+ last_value = rentabilities_source(last_date)
100
+ return rentabilities unless this_value && last_value
101
+
102
+ last_share = rentabilities[last_date].try(:share) || 1
103
+ rentability = (this_value / last_value - 1).round(8)
104
+ new_share = (last_share * (1 + rentability)).round(8)
105
+ line = RentabilityLine.new(this_date, rentability, nil, new_share, true)
106
+ after_parse(this_date, last_date, rentabilities, line)
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,21 @@
1
+ module ROI
2
+ class RentabilityLine < Struct.new(
3
+ :date, :rentability, :daily_result, :share, :have_position, :total_balance)
4
+
5
+ def self.build(date: nil, share: 1, have_position: false)
6
+ self.new(date, nil, nil, share, have_position, nil)
7
+ end
8
+
9
+ def self.create(date, assets, last_balance, last_share)
10
+ line = build(date: date, share: last_share || 1)
11
+ line.total_balance = assets.map(&:value).inject(:+)
12
+ if last_balance.to_f > 0
13
+ line.daily_result = assets.map(&:daily_result).inject(:+)
14
+ line.rentability = line.daily_result / last_balance
15
+ line.share = (last_share * (1 + line.rentability)).round(8)
16
+ line.have_position = true
17
+ end
18
+ line
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,65 @@
1
+ require "work_day"
2
+
3
+ module ROI
4
+ module RentabilityPeriods
5
+ def days(days_amount = 1)
6
+ scoped_start = WorkDay.last_until(end_date - days_amount)
7
+ format(scoped_start, end_date)
8
+ end
9
+
10
+ def current_month
11
+ scoped_start = WorkDay.last_until((end_date - 1.month).end_of_month)
12
+ check_gaps_and_format(adjust_start_date(scoped_start), end_date)
13
+ end
14
+
15
+ def months(amount, date = end_date)
16
+ scoped_start = WorkDay.last_until((date - amount.month).end_of_month)
17
+ scoped_end = adjust_end_date(date)
18
+ check_gaps_and_format(scoped_start, scoped_end)
19
+ end
20
+
21
+ def current_year
22
+ scoped_start = WorkDay.last_until((end_date - 1.year).end_of_year)
23
+ format(adjust_start_date(scoped_start), end_date)
24
+ end
25
+
26
+ def years_ago(amount)
27
+ scoped_end = WorkDay.last_until((end_date - amount.year).end_of_year)
28
+ scoped_start = WorkDay.last_until((scoped_end - 1.year).end_of_year)
29
+ format(adjust_start_date(scoped_start), scoped_end)
30
+ end
31
+
32
+ def portfolio
33
+ format(start_date, end_date)
34
+ end
35
+
36
+ private
37
+ def adjust_start_date(date)
38
+ [start_date, date].compact.max
39
+ end
40
+
41
+ def adjust_end_date(date)
42
+ WorkDay.last_until(date.strftime("%Y-%m") == end_date.strftime("%Y-%m") ?
43
+ end_date : date.end_of_month)
44
+ end
45
+
46
+ def format(start_date, end_date)
47
+ scoped_start, scoped_end = rentabilities.values_at(start_date, end_date)
48
+ if scoped_start && scoped_end
49
+ ((scoped_end.share / scoped_start.share - 1) * 100).round(8)
50
+ end
51
+ end
52
+
53
+ def check_gaps_and_format(scoped_start, scoped_end)
54
+ _start, _end = rentabilities.values_at(scoped_start, scoped_end)
55
+ return unless _start && _end
56
+ if skip_on_gap
57
+ rentabilities.each_value do |line|
58
+ return if line.date.between?(_start.date, _end.date) &&
59
+ !line.have_position
60
+ end
61
+ end
62
+ format(scoped_start, scoped_end)
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,3 @@
1
+ module ROI
2
+ VERSION = "0.1.2"
3
+ end
data/lib/roi.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "roi/rentability"
2
+ require "roi/rentability_line"
3
+ require "roi/rentability_periods"
4
+ require "roi/financial_result"
5
+
6
+ module ROI
7
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'roi/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "roi_calculator"
8
+ spec.version = ROI::VERSION
9
+ spec.authors = ["Adriano Bacha"]
10
+ spec.email = ["abacha@gmail.com"]
11
+
12
+ spec.summary = %q{Rentability and financial calculations}
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.13"
21
+ spec.add_development_dependency "rspec", "~> 3.0"
22
+ spec.add_runtime_dependency "memoist", "~> 0.15"
23
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: roi_calculator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Adriano Bacha
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-03-16 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.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: memoist
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.15'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.15'
55
+ description:
56
+ email:
57
+ - abacha@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".ruby-version"
65
+ - ".travis.yml"
66
+ - Gemfile
67
+ - Gemfile.lock
68
+ - LICENSE.txt
69
+ - README.md
70
+ - lib/roi.rb
71
+ - lib/roi/financial_result.rb
72
+ - lib/roi/rentability.rb
73
+ - lib/roi/rentability_line.rb
74
+ - lib/roi/rentability_periods.rb
75
+ - lib/roi/version.rb
76
+ - roi_calculator.gemspec
77
+ homepage:
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.6.10
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Rentability and financial calculations
101
+ test_files: []