Sha256: 3e25216ef70ff67326be12357b7dbeda47e33a9fed624430f32b9a8d08afd981

Contents?: true

Size: 730 Bytes

Versions: 15

Compression:

Stored size: 730 Bytes

Contents

module ROI
  class RentabilityLine < Struct.new(
    :date, :rentability, :daily_result, :share, :have_position, :total_balance)

    def self.build(date: nil, share: 1, have_position: false)
      self.new(date, nil, nil, share, have_position, nil)
    end

    def self.create(date, assets, last_balance, last_share)
      line = build(date: date, share: last_share || 1)
      line.total_balance = assets.map(&:value).inject(:+)
      if last_balance.to_f > 0
        line.daily_result = assets.map(&:daily_result).inject(:+)
        line.rentability = line.daily_result / last_balance
        line.share = (last_share * (1 + line.rentability)).round(8)
        line.have_position = true
      end
      line
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
roi_calculator-0.3.7 lib/roi/rentability_line.rb
roi_calculator-0.3.6 lib/roi/rentability_line.rb
roi_calculator-0.3.5 lib/roi/rentability_line.rb
roi_calculator-0.3.4 lib/roi/rentability_line.rb
roi_calculator-0.3.3 lib/roi/rentability_line.rb
roi_calculator-0.3.2 lib/roi/rentability_line.rb
roi_calculator-0.3.1 lib/roi/rentability_line.rb
roi_calculator-0.3.0 lib/roi/rentability_line.rb
roi_calculator-1.0.0 lib/roi/rentability_line.rb
roi_calculator-0.2.0 lib/roi/rentability_line.rb
roi_calculator-0.1.6 lib/roi/rentability_line.rb
roi_calculator-0.1.5 lib/roi/rentability_line.rb
roi_calculator-0.1.4 lib/roi/rentability_line.rb
roi_calculator-0.1.3 lib/roi/rentability_line.rb
roi_calculator-0.1.2 lib/roi/rentability_line.rb