Class: BondCalculator::SpreadToCurve

Inherits:
BaseCalculator show all
Defined in:
lib/bond_calculator/spread_to_curve.rb

Overview

Class that handle the calculation of the spread to curve

Since the corporate bond term is not exactly the same as its benchmark term, we use linear interpolation to calculate the spread to the curve.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseCalculator

#bonds_by_type, #csv_to_bonds

Constructor Details

#initialize(bond_file_path) ⇒ <void>

Parameters:

  • bond_file_path (File)

    Csv curve file path



18
19
20
# File 'lib/bond_calculator/spread_to_curve.rb', line 18

def initialize(bond_file_path)
  @bonds = csv_to_bonds(bond_file_path)
end

Instance Attribute Details

#bondsObject (readonly)

Returns the value of attribute bonds.



13
14
15
# File 'lib/bond_calculator/spread_to_curve.rb', line 13

def bonds
  @bonds
end

Instance Method Details

#calculateHash

Execute the bussines rule to calculate the spread to curve

Returns:

  • (Hash)

    :bond_name

  • (Hash)

    :spread_to_curve



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/bond_calculator/spread_to_curve.rb', line 25

def calculate
  response = []
  corps = bonds_by_type('corporate', @bonds)
  govs = bonds_by_type('government', @bonds)

  corps.each do |corp|
    upper_bond = nil
    lower_bond = nil

    govs.each do |gov|
      if corp.term_years <= gov.term_years
        lower_bond = gov
        break
      else
        upper_bond = gov
      end
    end

    spread_to_curve = (corp.yield_percent - yield_on_curve(corp, lower_bond, upper_bond)).round(2)
    response << { bond_name: corp.name, spread_to_curve: spread_to_curve }
  end
  print_response(response)

  response
end