| 1 | package Quant::Framework::InterestRate; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use Data::Chronicle::Reader; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use Data::Chronicle::Writer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | =head1 NAME | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | Quant::Framework::InterestRate - A module to save/load interest rates for currencies | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | This module saves/loads interest rate data to/from Chronicle. | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | my $ir_data = Quant::Framework::InterestRate->new(symbol => 'USD', | ||||||||||||||||||||||||||||
| 15 | rates => { 7 => 0.5, 30 => 1.2, 90 => 2.4 }); | ||||||||||||||||||||||||||||
| 16 | $ir_data->save; | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | To read interest rates for a currency: | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | my $ir_data = Quant::MarketData::InterestRate->new(symbol => 'USD'); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | my $rates = $ir_data->rates; | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =cut | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 2 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | extends 'Quant::Framework::Utils::Rates'; | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 2 | ▶ | use Math::Function::Interpolator; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | =head2 for_date | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | The date for which we wish data | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =cut | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | has for_date => ( | ||||||||||||||||||||||||||||
| 38 | is => 'ro', | ||||||||||||||||||||||||||||
| 39 | isa => 'Maybe[Date::Utility]', | ||||||||||||||||||||||||||||
| 40 | default => undef, | ||||||||||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =head2 underlying_config | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | UnderlyingConfig used to create/initialize Q::F modules | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =cut | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | has underlying_config => ( | ||||||||||||||||||||||||||||
| 50 | is => 'ro', | ||||||||||||||||||||||||||||
| 51 | isa => 'Quant::Framework::Utils::UnderlyingConfig', | ||||||||||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | around _document_content => sub { | ||||||||||||||||||||||||||||
| 55 | my $orig = shift; | ||||||||||||||||||||||||||||
| 56 | my $self = shift; | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | return { | ||||||||||||||||||||||||||||
| 59 | %{$self->$orig}, | ||||||||||||||||||||||||||||
| 60 | type => $self->type, | ||||||||||||||||||||||||||||
| 61 | rates => $self->rates | ||||||||||||||||||||||||||||
| 62 | }; | ||||||||||||||||||||||||||||
| 63 | }; | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | has chronicle_reader => ( | ||||||||||||||||||||||||||||
| 66 | is => 'ro', | ||||||||||||||||||||||||||||
| 67 | isa => 'Data::Chronicle::Reader', | ||||||||||||||||||||||||||||
| 68 | ); | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | has chronicle_writer => ( | ||||||||||||||||||||||||||||
| 71 | is => 'ro', | ||||||||||||||||||||||||||||
| 72 | isa => 'Data::Chronicle::Writer', | ||||||||||||||||||||||||||||
| 73 | ); | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =head2 document | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | The document that this object is tied to. | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =cut | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | has document => ( | ||||||||||||||||||||||||||||
| 82 | is => 'rw', | ||||||||||||||||||||||||||||
| 83 | lazy_build => 1, | ||||||||||||||||||||||||||||
| 84 | ); | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | sub _build_document { | ||||||||||||||||||||||||||||
| 87 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_build_document: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | my $document = $self->chronicle_reader->get('interest_rates', $self->symbol); | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | ▶ | if ($self->for_date and $self->for_date->epoch < Date::Utility->new($document->{date})->epoch) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->for_date and $self->for_date->epoch < "Date::Utility"->new($document->{"date"})->epoch
MC/DC0%0/2
$self->for_date and $self->for_date->epoch < "Date::Utility"->new($document->{"date"})->epoch
$self->for_date
$self->for_date->epoch < "Date::Utility"->new($document->{"date"})->epoch
Truth table0%0/3
A and B
A= $self->for_dateB= $self->for_date->epoch < "Date::Utility"->new($document->{"date"})->epoch
| |||||||||||||||||||||||||||||
| 92 | 0 | $document = $self->chronicle_reader->get_for('interest_rates', $self->symbol, $self->for_date->epoch); | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | #Assume empty data in case there is nothing in the database | ||||||||||||||||||||||||||||
| 95 | 0 | ▶ | $document //= {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$document //= {}
MC/DC0%0/1
$document //= {}
$document
Truth table0%0/2
A
A= $document
| |||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | return $document; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =head2 save | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | Save the document in the Chronicle data storage | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =cut | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | sub save { | ||||||||||||||||||||||||||||
| 108 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
save: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | ▶ | if (not defined $self->chronicle_reader->get('interest_rates', $self->symbol)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 0 | $self->chronicle_writer->set('interest_rates', $self->symbol, {}); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | return $self->chronicle_writer->set('interest_rates', $self->symbol, $self->_document_content, $self->recorded_date); | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | has type => ( | ||||||||||||||||||||||||||||
| 118 | is => 'ro', | ||||||||||||||||||||||||||||
| 119 | isa => 'qf_interest_rate_type', | ||||||||||||||||||||||||||||
| 120 | default => 'market', | ||||||||||||||||||||||||||||
| 121 | ); | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =head2 interest_rate_for | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | Get the interest rate for this underlying over a given time period (expressed in timeinyears.) | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =cut | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | sub interest_rate_for { | ||||||||||||||||||||||||||||
| 130 | 0 | ▶ | my ($self, $tiy) = @_; | ||||||||||||||||||||||||||
|
Subroutine
interest_rate_for: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # timeinyears cannot be undef | ||||||||||||||||||||||||||||
| 133 | 0 | ▶ | $tiy ||= 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
$tiy ||= 0
MC/DC0%0/1
$tiy ||= 0
$tiy
Truth table0%0/2
A
A= $tiy
| |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 0 | ▶ | return $self->underlying_config->default_interest_rate if defined $self->underlying_config->default_interest_rate; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | my $quoted_currency = Quant::Framework::Currency->new({ | |||||||||||||||||||||||||||
| 138 | symbol => $self->underlying_config->quoted_currency_symbol, | ||||||||||||||||||||||||||||
| 139 | for_date => $self->for_date, | ||||||||||||||||||||||||||||
| 140 | chronicle_reader => $self->chronicle_reader, | ||||||||||||||||||||||||||||
| 141 | chronicle_writer => $self->chronicle_writer, | ||||||||||||||||||||||||||||
| 142 | }); | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 0 | my $rate; | |||||||||||||||||||||||||||
| 145 | 0 | ▶ | if ($self->underlying_config->uses_implied_rate_for_quoted_currency) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 0 | $rate = $quoted_currency->rate_implied_from($self->underlying_config->rate_to_imply_from, $tiy); | |||||||||||||||||||||||||||
| 147 | } else { | ||||||||||||||||||||||||||||
| 148 | 0 | $rate = $quoted_currency->rate_for($tiy); | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | return $rate; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 2 | ▶ | no Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 155 | __PACKAGE__->meta->make_immutable; | ||||||||||||||||||||||||||||
| 156 | 1; | ||||||||||||||||||||||||||||