Class: ROI::Rentability

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
RentabilityPeriods
Defined in:
lib/roi/rentability.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RentabilityPeriods

#current_month, #current_year, #days, #months, #portfolio, #years_ago

Constructor Details

#initialize(options) ⇒ Rentability

Returns a new instance of Rentability.



17
18
19
20
21
22
23
# File 'lib/roi/rentability.rb', line 17

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_dateObject (readonly)

Returns the value of attribute end_date.



15
16
17
# File 'lib/roi/rentability.rb', line 15

def end_date
  @end_date
end

#idObject (readonly)

Returns the value of attribute id.



15
16
17
# File 'lib/roi/rentability.rb', line 15

def id
  @id
end

#rentabilitiesObject

Returns the value of attribute rentabilities.



14
15
16
# File 'lib/roi/rentability.rb', line 14

def rentabilities
  @rentabilities
end

#skip_on_gapObject (readonly)

Returns the value of attribute skip_on_gap.



15
16
17
# File 'lib/roi/rentability.rb', line 15

def skip_on_gap
  @skip_on_gap
end

#start_dateObject (readonly)

Returns the value of attribute start_date.



15
16
17
# File 'lib/roi/rentability.rb', line 15

def start_date
  @start_date
end

Instance Method Details

#calculateObject



27
28
29
30
31
32
33
34
35
# File 'lib/roi/rentability.rb', line 27

def calculate
  parse_rentabilities
  if @rentabilities.count.positive?
    dates = @rentabilities.keys
    @start_date = dates[0]
    @end_date = dates[-1]
  end
  self
end

#dates_with_positionObject



41
42
43
# File 'lib/roi/rentability.rb', line 41

def dates_with_position
  rentabilities.select { |_, rentability| rentability.have_position }.keys
end

#gross_profitObject



37
38
39
# File 'lib/roi/rentability.rb', line 37

def gross_profit
  GrossProfit.new(rentabilities, start_date, end_date)
end

#post_initialize(options) ⇒ Object



25
# File 'lib/roi/rentability.rb', line 25

def post_initialize(options); end

#rentabilities_array(start_date = nil, end_date = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/roi/rentability.rb', line 45

def rentabilities_array(start_date = nil, end_date = nil)
  start_date ||= @start_date
  end_date ||= @end_date

  filtered = @rentabilities.values.select do |val|
    val.reference_date.between?(start_date, end_date)
  end
  filtered.map(&:rentability).compact
end

#to_aObject



72
73
74
75
76
# File 'lib/roi/rentability.rb', line 72

def to_a
  dates_with_position.map do |date|
    [date.strftime('%Y-%m-%d'), @rentabilities[date].quota - 1] if @rentabilities[date]
  end.compact
end

#volatility(months_ago = nil) ⇒ Object



55
56
57
58
# File 'lib/roi/rentability.rb', line 55

def volatility(months_ago = nil)
  start = (end_date - months_ago.months + 1.month).beginning_of_month if months_ago
  (volatility_in_window(nil, start).first || [])[1]
end

#volatility_in_window(days = nil, start_date = nil) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/roi/rentability.rb', line 60

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)
    date =
      dates_with_position.fetch(low + days) { dates_with_position.last }
    [date, MathUtil.volatility(series)] if date
  end
end