Class: ROI::Rentability
Instance Attribute Summary collapse
Instance Method Summary
collapse
#current_month, #current_year, #days, #months, #portfolio, #years_ago
Constructor Details
#initialize(options) ⇒ Rentability
Returns a new instance of Rentability.
15
16
17
18
19
20
21
|
# File 'lib/roi/rentability.rb', line 15
def initialize(options)
@id = options[:id].try(:to_i)
@start_date = options[:start_date]
@end_date = options[:end_date] || Date.today - 1
@skip_on_gap = false
post_initialize(options)
end
|
Instance Attribute Details
#end_date ⇒ Object
Returns the value of attribute end_date.
13
14
15
|
# File 'lib/roi/rentability.rb', line 13
def end_date
@end_date
end
|
#id ⇒ Object
Returns the value of attribute id.
13
14
15
|
# File 'lib/roi/rentability.rb', line 13
def id
@id
end
|
#rentabilities ⇒ Object
Returns the value of attribute rentabilities.
12
13
14
|
# File 'lib/roi/rentability.rb', line 12
def rentabilities
@rentabilities
end
|
#skip_on_gap ⇒ Object
Returns the value of attribute skip_on_gap.
13
14
15
|
# File 'lib/roi/rentability.rb', line 13
def skip_on_gap
@skip_on_gap
end
|
#start_date ⇒ Object
Returns the value of attribute start_date.
13
14
15
|
# File 'lib/roi/rentability.rb', line 13
def start_date
@start_date
end
|
Instance Method Details
#calculate ⇒ Object
26
27
28
29
30
31
32
33
|
# File 'lib/roi/rentability.rb', line 26
def calculate
parse_rentabilities
if @rentabilities.count > 0
dates = @rentabilities.keys
@start_date, @end_date = [dates[0], dates[-1]]
end
self
end
|
#dates_with_position ⇒ Object
39
40
41
|
# File 'lib/roi/rentability.rb', line 39
def dates_with_position
rentabilities.select { |_, rentability| rentability.have_position }.keys
end
|
#financial ⇒ Object
35
36
37
|
# File 'lib/roi/rentability.rb', line 35
def financial
FinancialResult.new(rentabilities, start_date, end_date)
end
|
#post_initialize(options) ⇒ Object
23
24
|
# File 'lib/roi/rentability.rb', line 23
def post_initialize(options)
end
|
#rentabilities_array(start_date = nil, end_date = nil) ⇒ Object
43
44
45
46
47
48
49
50
51
|
# File 'lib/roi/rentability.rb', line 43
def rentabilities_array(start_date = nil, end_date = nil)
start_date ||= @start_date
end_date ||= @end_date
filtered = @rentabilities.values.select do |val|
val.date.between?(start_date, end_date)
end
filtered.map(&:rentability).compact
end
|
#to_a ⇒ Object
71
72
73
74
75
76
77
|
# File 'lib/roi/rentability.rb', line 71
def to_a
dates_with_position.map do |date|
if @rentabilities[date]
[date.to_time.to_i, @rentabilities[date].share]
end
end.compact
end
|
#volatility(months_ago = nil) ⇒ Object
53
54
55
56
57
|
# File 'lib/roi/rentability.rb', line 53
def volatility(months_ago = nil)
_start = months_ago ?
(end_date - months_ago.months + 1.month).beginning_of_month : nil
(volatility_in_window(nil, _start).first || [])[1]
end
|
#volatility_in_window(days = nil, start_date = nil) ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/roi/rentability.rb', line 59
def volatility_in_window(days = nil, start_date = nil)
filtered = rentabilities_array(start_date)
days ||= filtered.size
0.upto(filtered.size - days).map do |low|
series = filtered.slice(low, days + 1)
vol = MathUtil.volatility(series)
date = dates_with_position.fetch(low + days) { dates_with_position.last }
[date, vol] if date
end
end
|