class ROI::Rentability

Attributes

end_date[R]
id[R]
rentabilities[RW]
skip_on_gap[R]
start_date[R]

Public Class Methods

new(options) click to toggle source
# 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

Public Instance Methods

calculate() click to toggle source
# 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_position() click to toggle source
# File lib/roi/rentability.rb, line 41
def dates_with_position
  rentabilities.select { |_, rentability| rentability.have_position }.keys
end
gross_profit() click to toggle source
# File lib/roi/rentability.rb, line 37
def gross_profit
  GrossProfit.new(rentabilities, start_date, end_date)
end
post_initialize(options) click to toggle source
# File lib/roi/rentability.rb, line 25
def post_initialize(options); end
rentabilities_array(start_date = nil, end_date = nil) click to toggle source
# 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_a() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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

Protected Instance Methods

before_parse_rentabilities() click to toggle source
# File lib/roi/rentability.rb, line 80
def before_parse_rentabilities; end
work_days() click to toggle source
# File lib/roi/rentability.rb, line 82
def work_days
  start_date && end_date ? WorkDay.between(start_date, end_date) : []
end

Private Instance Methods

parse(this_date, last_date, rentabilities) click to toggle source
# File lib/roi/rentability.rb, line 97
def parse(this_date, last_date, rentabilities)
  this_value = rentabilities_source(this_date)
  last_value = rentabilities_source(last_date)
  return rentabilities unless this_value && last_value

  last_quota = rentabilities[last_date].try(:quota) || 1
  rentability = (this_value / last_value - 1).round(8)
  new_quota = (last_quota * (1 + rentability)).round(8)
  line = RentabilityLine.new(this_date, rentability, new_quota, true)
  after_parse(this_date, last_date, rentabilities, line)
end
parse_rentabilities() click to toggle source
# File lib/roi/rentability.rb, line 89
def parse_rentabilities
  @rentabilities = {}
  before_parse_rentabilities
  work_days.each_cons(2) do |last_date, date|
    parse(date, last_date, @rentabilities)
  end
end