Class: BondCalculator::SpreadToBenchmark

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

Overview

Class that handle the calculation of the spread to benchmark Calculate the yield spread (return) between a corporate bond and its government bond benchmark.

A government bond is a good benchmark if it is as close as possible to the corporate bond in terms of years to maturity (term).

Author:

  • Lucas Sant’ Anna

Since:

  • 0.0.1

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 benchmark file path

Since:

  • 0.0.1



21
22
23
# File 'lib/bond_calculator/spread_to_benchmark.rb', line 21

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

Instance Attribute Details

#bondsObject (readonly)

Since:

  • 0.0.1



16
17
18
# File 'lib/bond_calculator/spread_to_benchmark.rb', line 16

def bonds
  @bonds
end

Instance Method Details

#calculateHash

Execute the bussines rule to calculate the spread to Benchmark

Returns:

  • (Hash)

    :bond_name

  • (Hash)

    :benchmark_name

  • (Hash)

    :spread_to_benchmark

Since:

  • 0.0.1



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/bond_calculator/spread_to_benchmark.rb', line 29

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

  corps.each do |corp|
    benchmark = nil
    govs.each do |gov|
      if benchmark.nil?
        benchmark = best_benchmark(corp, gov)
        next
      end
      if benchmark[:term_spread] > term_spread(corp, gov)
        benchmark.merge(best_benchmark(corp, gov))
      end
    end

    response << { bond_name: corp.name,
                  benchmark_name: benchmark[:candidate].name,
                  spread_to_benchmark: spread_to_benchmark(benchmark, corp) }
  end
  print(response)

  response
end