roi_calculator 1.0.0 → 2.0.0
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 +4 -4
- data/.ruby-version +1 -1
- data/Gemfile.lock +14 -14
- data/bitbucket-pipelines.yml +13 -15
- data/lib/roi.rb +6 -4
- data/lib/roi/financial_result.rb +10 -8
- data/lib/roi/rentability.rb +26 -22
- data/lib/roi/rentability_line.rb +9 -16
- data/lib/roi/rentability_periods.rb +16 -8
- data/lib/roi/version.rb +3 -1
- data/roi_calculator.gemspec +15 -13
- metadata +9 -11
- data/.ruby-gemset +0 -1
- data/circle.yml +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fc63825e0b9a50e16646d8a1b918ade35e0a507ae2b5da79a53d0b9759acd010
|
4
|
+
data.tar.gz: 8e2cde87e2205d49f98f372c07797bbc2764699c9f77f5e07c8c44daac7178c1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: edba4d7742f9cc1e5903a6d4d141e4be39a5cf9c21aa888e041cfa49953adf41819e41cb6b96785362602cfc48b8f3c9bceaf0b74334bfe8492e3f39dd7b1c11
|
7
|
+
data.tar.gz: e09f232462b96ee5b2fc964447210322551b8c5b34959dfdbb7468cc4a7beb5a73c68fb8372b56e19961eda3c577cddea7db74dcfe0d6f563bc5fcf5b2927112
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
ruby-2.
|
1
|
+
ruby-2.5.0
|
data/Gemfile.lock
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
roi_calculator (
|
4
|
+
roi_calculator (2.0.0)
|
5
5
|
math_util (~> 0.1.0)
|
6
6
|
memoist (~> 0.15)
|
7
|
-
work_day (~> 0.
|
7
|
+
work_day (~> 1.0.0)
|
8
8
|
|
9
9
|
GEM
|
10
10
|
remote: https://rubygems.org/
|
@@ -21,23 +21,23 @@ GEM
|
|
21
21
|
math_util (0.1.0)
|
22
22
|
memoist (0.16.0)
|
23
23
|
minitest (5.11.3)
|
24
|
-
rspec (3.
|
25
|
-
rspec-core (~> 3.
|
26
|
-
rspec-expectations (~> 3.
|
27
|
-
rspec-mocks (~> 3.
|
28
|
-
rspec-core (3.
|
29
|
-
rspec-support (~> 3.
|
30
|
-
rspec-expectations (3.
|
24
|
+
rspec (3.7.0)
|
25
|
+
rspec-core (~> 3.7.0)
|
26
|
+
rspec-expectations (~> 3.7.0)
|
27
|
+
rspec-mocks (~> 3.7.0)
|
28
|
+
rspec-core (3.7.1)
|
29
|
+
rspec-support (~> 3.7.0)
|
30
|
+
rspec-expectations (3.7.0)
|
31
31
|
diff-lcs (>= 1.2.0, < 2.0)
|
32
|
-
rspec-support (~> 3.
|
33
|
-
rspec-mocks (3.
|
32
|
+
rspec-support (~> 3.7.0)
|
33
|
+
rspec-mocks (3.7.0)
|
34
34
|
diff-lcs (>= 1.2.0, < 2.0)
|
35
|
-
rspec-support (~> 3.
|
36
|
-
rspec-support (3.
|
35
|
+
rspec-support (~> 3.7.0)
|
36
|
+
rspec-support (3.7.1)
|
37
37
|
thread_safe (0.3.6)
|
38
38
|
tzinfo (1.2.5)
|
39
39
|
thread_safe (~> 0.1)
|
40
|
-
work_day (0.
|
40
|
+
work_day (1.0.0)
|
41
41
|
activesupport (>= 4.0)
|
42
42
|
|
43
43
|
PLATFORMS
|
data/bitbucket-pipelines.yml
CHANGED
@@ -1,22 +1,20 @@
|
|
1
|
-
# This is a sample build configuration for Ruby.
|
2
|
-
# Check our guides at https://confluence.atlassian.com/x/8r-5Mw for more examples.
|
3
|
-
# Only use spaces to indent your .yml configuration.
|
4
|
-
# -----
|
5
|
-
# You can specify a custom docker image from Docker Hub as your build environment.
|
6
1
|
image: ruby:2.5.0
|
7
2
|
|
8
3
|
pipelines:
|
9
4
|
default:
|
10
5
|
- step:
|
11
|
-
|
12
|
-
-
|
13
|
-
script: # Modify the commands below to build your repository.
|
14
|
-
- gem install rubocop
|
6
|
+
script:
|
7
|
+
- git archive --remote=git@bitbucket.org:guideinvestimentos/rails_defaults.git HEAD .rubocop.yml | tar -x
|
15
8
|
- bundle install
|
16
|
-
-
|
17
|
-
-
|
18
|
-
-
|
9
|
+
- gem install rubocop
|
10
|
+
- rspec -fdoc
|
11
|
+
- rubocop .
|
19
12
|
|
20
|
-
|
21
|
-
|
22
|
-
|
13
|
+
branches:
|
14
|
+
master:
|
15
|
+
- step:
|
16
|
+
script:
|
17
|
+
- gem build roi_calculator.gemspec
|
18
|
+
- curl -u $RUBYGEMS_USERNAME:$RUBYGEMS_PASSWORD https://rubygems.org/api/v1/api_key.yaml > ~/.gem/credentials
|
19
|
+
- chmod 0600 ~/.gem/credentials
|
20
|
+
- gem push $(ls *.gem)
|
data/lib/roi.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
4
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'roi/rentability'
|
4
|
+
require 'roi/rentability_line'
|
5
|
+
require 'roi/rentability_periods'
|
6
|
+
require 'roi/financial_result'
|
5
7
|
|
6
8
|
module ROI
|
7
9
|
end
|
data/lib/roi/financial_result.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'work_day'
|
4
|
+
require 'active_support'
|
5
|
+
require 'active_support/core_ext/date'
|
4
6
|
|
5
7
|
module ROI
|
6
8
|
class FinancialResult
|
@@ -38,6 +40,7 @@ module ROI
|
|
38
40
|
end
|
39
41
|
|
40
42
|
private
|
43
|
+
|
41
44
|
def scoped_rentabilities(scope_start, scope_end)
|
42
45
|
rentabilities.values.select.each do |line|
|
43
46
|
line.date.between?(scope_start, scope_end)
|
@@ -53,12 +56,11 @@ module ROI
|
|
53
56
|
end
|
54
57
|
|
55
58
|
def check_gaps_and_format(scope_start, scope_end)
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
end
|
60
|
-
format_financial(scope_start, scope_end)
|
59
|
+
return unless scope_start >= start_date
|
60
|
+
scoped_rentabilities(scope_start, scope_end).each do |line|
|
61
|
+
return nil if line.try(:have_position) == false
|
61
62
|
end
|
63
|
+
format_financial(scope_start, scope_end)
|
62
64
|
end
|
63
65
|
end
|
64
66
|
end
|
data/lib/roi/rentability.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'memoist'
|
4
|
+
require 'roi/financial_result'
|
5
|
+
require 'roi/rentability_periods'
|
6
|
+
require 'math_util'
|
7
|
+
require 'active_support'
|
8
|
+
require 'active_support/core_ext'
|
7
9
|
|
8
10
|
module ROI
|
9
11
|
class Rentability
|
@@ -20,14 +22,14 @@ module ROI
|
|
20
22
|
post_initialize(options)
|
21
23
|
end
|
22
24
|
|
23
|
-
def post_initialize(options)
|
24
|
-
end
|
25
|
+
def post_initialize(options); end
|
25
26
|
|
26
27
|
def calculate
|
27
28
|
parse_rentabilities
|
28
|
-
if @rentabilities.count
|
29
|
+
if @rentabilities.count.positive?
|
29
30
|
dates = @rentabilities.keys
|
30
|
-
@start_date
|
31
|
+
@start_date = dates[0]
|
32
|
+
@end_date = dates[-1]
|
31
33
|
end
|
32
34
|
self
|
33
35
|
end
|
@@ -51,9 +53,10 @@ module ROI
|
|
51
53
|
end
|
52
54
|
|
53
55
|
def volatility(months_ago = nil)
|
54
|
-
|
55
|
-
(end_date - months_ago.months + 1.month).beginning_of_month
|
56
|
-
|
56
|
+
if months_ago
|
57
|
+
start = (end_date - months_ago.months + 1.month).beginning_of_month
|
58
|
+
end
|
59
|
+
(volatility_in_window(nil, start).first || [])[1]
|
57
60
|
end
|
58
61
|
|
59
62
|
def volatility_in_window(days = nil, start_date = nil)
|
@@ -62,23 +65,23 @@ module ROI
|
|
62
65
|
|
63
66
|
0.upto(filtered.size - days).map do |low|
|
64
67
|
series = filtered.slice(low, days + 1)
|
65
|
-
|
66
|
-
|
67
|
-
[date,
|
68
|
+
date =
|
69
|
+
dates_with_position.fetch(low + days) { dates_with_position.last }
|
70
|
+
[date, MathUtil.volatility(series)] if date
|
68
71
|
end
|
69
72
|
end
|
70
73
|
|
71
74
|
def to_a
|
72
75
|
dates_with_position.map do |date|
|
73
76
|
if @rentabilities[date]
|
74
|
-
[date.strftime(
|
77
|
+
[date.strftime('%Y-%m-%d'), @rentabilities[date].quota - 1]
|
75
78
|
end
|
76
79
|
end.compact
|
77
80
|
end
|
78
81
|
|
79
82
|
protected
|
80
|
-
|
81
|
-
end
|
83
|
+
|
84
|
+
def before_parse_rentabilities; end
|
82
85
|
|
83
86
|
def work_days
|
84
87
|
start_date && end_date ? WorkDay.between(start_date, end_date) : []
|
@@ -86,6 +89,7 @@ module ROI
|
|
86
89
|
memoize :work_days
|
87
90
|
|
88
91
|
private
|
92
|
+
|
89
93
|
def parse_rentabilities
|
90
94
|
@rentabilities = {}
|
91
95
|
before_parse_rentabilities
|
@@ -99,10 +103,10 @@ module ROI
|
|
99
103
|
last_value = rentabilities_source(last_date)
|
100
104
|
return rentabilities unless this_value && last_value
|
101
105
|
|
102
|
-
|
106
|
+
last_quota = rentabilities[last_date].try(:quota) || 1
|
103
107
|
rentability = (this_value / last_value - 1).round(8)
|
104
|
-
|
105
|
-
line = RentabilityLine.new(this_date, rentability,
|
108
|
+
new_quota = (last_quota * (1 + rentability)).round(8)
|
109
|
+
line = RentabilityLine.new(this_date, rentability, new_quota, true)
|
106
110
|
after_parse(this_date, last_date, rentabilities, line)
|
107
111
|
end
|
108
112
|
end
|
data/lib/roi/rentability_line.rb
CHANGED
@@ -1,21 +1,14 @@
|
|
1
|
-
|
2
|
-
class RentabilityLine < Struct.new(
|
3
|
-
:date, :rentability, :daily_result, :share, :have_position, :total_balance)
|
1
|
+
# frozen_string_literal: true
|
4
2
|
|
5
|
-
|
6
|
-
|
7
|
-
|
3
|
+
module ROI
|
4
|
+
class RentabilityLine
|
5
|
+
attr_reader :date, :rentability, :daily_result, :quota, :have_position
|
8
6
|
|
9
|
-
def
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
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
|
7
|
+
def initialize(date, rentability, quota, have_position)
|
8
|
+
@date = date
|
9
|
+
@rentability = rentability
|
10
|
+
@quota = quota
|
11
|
+
@have_position = have_position
|
19
12
|
end
|
20
13
|
end
|
21
14
|
end
|
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'work_day'
|
2
4
|
|
3
5
|
module ROI
|
4
6
|
module RentabilityPeriods
|
@@ -34,29 +36,35 @@ module ROI
|
|
34
36
|
end
|
35
37
|
|
36
38
|
private
|
39
|
+
|
37
40
|
def adjust_start_date(date)
|
38
41
|
[start_date, date].compact.max
|
39
42
|
end
|
40
43
|
|
41
44
|
def adjust_end_date(date)
|
42
|
-
|
43
|
-
|
45
|
+
scoped_end =
|
46
|
+
if date.strftime('%Y-%m') == end_date.strftime('%Y-%m')
|
47
|
+
end_date
|
48
|
+
else
|
49
|
+
date.end_of_month
|
50
|
+
end
|
51
|
+
WorkDay.last_until(scoped_end)
|
44
52
|
end
|
45
53
|
|
46
54
|
def format(start_date, end_date)
|
47
55
|
scoped_start, scoped_end = rentabilities.values_at(start_date, end_date)
|
48
56
|
if scoped_start && scoped_end
|
49
|
-
((scoped_end.
|
57
|
+
((scoped_end.quota / scoped_start.quota - 1) * 100).round(8)
|
50
58
|
end
|
51
59
|
end
|
52
60
|
|
53
61
|
def check_gaps_and_format(scoped_start, scoped_end)
|
54
|
-
|
55
|
-
return unless
|
62
|
+
local_start, local_end = rentabilities.values_at(scoped_start, scoped_end)
|
63
|
+
return unless local_start && local_end
|
56
64
|
if skip_on_gap
|
57
65
|
rentabilities.each_value do |line|
|
58
|
-
return if line.date.between?(
|
59
|
-
|
66
|
+
return nil if line.date.between?(local_start.date, local_end.date) &&
|
67
|
+
!line.have_position
|
60
68
|
end
|
61
69
|
end
|
62
70
|
format(scoped_start, scoped_end)
|
data/lib/roi/version.rb
CHANGED
data/roi_calculator.gemspec
CHANGED
@@ -1,25 +1,27 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
lib = File.expand_path('lib', __dir__)
|
3
5
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
6
|
require 'roi/version'
|
5
7
|
|
6
8
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
9
|
+
spec.name = 'roi_calculator'
|
8
10
|
spec.version = ROI::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
11
|
+
spec.authors = ['Adriano Bacha']
|
12
|
+
spec.email = ['abacha@gmail.com']
|
11
13
|
|
12
|
-
spec.summary =
|
13
|
-
spec.license =
|
14
|
+
spec.summary = 'Rentability and financial calculations'
|
15
|
+
spec.license = 'MIT'
|
14
16
|
|
15
17
|
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
16
18
|
f.match(%r{^(test|spec|features)/})
|
17
19
|
end
|
18
|
-
spec.require_paths = [
|
20
|
+
spec.require_paths = ['lib']
|
19
21
|
|
20
|
-
spec.add_development_dependency
|
21
|
-
spec.add_development_dependency
|
22
|
-
spec.add_runtime_dependency
|
23
|
-
spec.add_runtime_dependency
|
24
|
-
spec.add_runtime_dependency
|
22
|
+
spec.add_development_dependency 'bundler', '~> 1.13'
|
23
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
24
|
+
spec.add_runtime_dependency 'math_util', '~> 0.1.0'
|
25
|
+
spec.add_runtime_dependency 'memoist', '~> 0.15'
|
26
|
+
spec.add_runtime_dependency 'work_day', '~> 1.0.0'
|
25
27
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: roi_calculator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adriano Bacha
|
@@ -39,47 +39,47 @@ dependencies:
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '3.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: math_util
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
47
|
+
version: 0.1.0
|
48
48
|
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
54
|
+
version: 0.1.0
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: memoist
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: 0.
|
61
|
+
version: '0.15'
|
62
62
|
type: :runtime
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: 0.
|
68
|
+
version: '0.15'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: work_day
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
73
|
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: 0.
|
75
|
+
version: 1.0.0
|
76
76
|
type: :runtime
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: 0.
|
82
|
+
version: 1.0.0
|
83
83
|
description:
|
84
84
|
email:
|
85
85
|
- abacha@gmail.com
|
@@ -89,7 +89,6 @@ extra_rdoc_files: []
|
|
89
89
|
files:
|
90
90
|
- ".gitignore"
|
91
91
|
- ".rspec"
|
92
|
-
- ".ruby-gemset"
|
93
92
|
- ".ruby-version"
|
94
93
|
- ".travis.yml"
|
95
94
|
- Gemfile
|
@@ -97,7 +96,6 @@ files:
|
|
97
96
|
- LICENSE.txt
|
98
97
|
- README.md
|
99
98
|
- bitbucket-pipelines.yml
|
100
|
-
- circle.yml
|
101
99
|
- lib/roi.rb
|
102
100
|
- lib/roi/financial_result.rb
|
103
101
|
- lib/roi/rentability.rb
|
data/.ruby-gemset
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
bigbang_services
|
data/circle.yml
DELETED