Class: BondCalculator::SpreadToBenchmark
- Inherits:
-
BaseCalculator
- Object
- BaseCalculator
- BondCalculator::SpreadToBenchmark
- 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).
Instance Attribute Summary collapse
- #bonds ⇒ Object readonly
Instance Method Summary collapse
-
#calculate ⇒ Hash
Execute the bussines rule to calculate the spread to Benchmark.
- #initialize(bond_file_path) ⇒ <void> constructor
Methods inherited from BaseCalculator
Constructor Details
#initialize(bond_file_path) ⇒ <void>
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
#bonds ⇒ Object (readonly)
16 17 18 |
# File 'lib/bond_calculator/spread_to_benchmark.rb', line 16 def bonds @bonds end |
Instance Method Details
#calculate ⇒ Hash
Execute the bussines rule to calculate the spread to Benchmark
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 |