| 1 | package Quant::Framework::ImpliedRate; | ||
| 2 | |||
| 3 | use Data::Chronicle::Reader; | ||
| 4 | use Data::Chronicle::Writer; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Quant::Framework::ImpliedRate - A module to save/load implied interest rates for currencies | ||
| 9 | |||
| 10 | =head1 DESCRIPTION | ||
| 11 | |||
| 12 | This module saves/loads implied interest rate data to/from Chronicle. | ||
| 13 | |||
| 14 | my $ir_data = Quant::Framework::ImpliedRate->new(symbol => 'USD-EUR', | ||
| 15 | rates => { 7 => 0.5, 30 => 1.2, 90 => 2.4 }); | ||
| 16 | $ir_data->save; | ||
| 17 | |||
| 18 | To read implied interest rates for a currency: | ||
| 19 | |||
| 20 | my $ir_data = Quant::Framework::ImpliedRate->new(symbol => 'USD-EUR'); | ||
| 21 | |||
| 22 | my $rates = $ir_data->rates; | ||
| 23 | |||
| 24 | =cut | ||
| 25 | |||
| 26 | use Moose; | ||
| 27 | extends 'Quant::Framework::Utils::Rates'; | ||
| 28 | |||
| 29 | =head2 for_date | ||
| 30 | |||
| 31 | The date for which we wish data | ||
| 32 | |||
| 33 | =cut | ||
| 34 | |||
| 35 | has for_date => ( | ||
| 36 | is => 'ro', | ||
| 37 | isa => 'Maybe[Date::Utility]', | ||
| 38 | default => undef, | ||
| 39 | ); | ||
| 40 | |||
| 41 | has chronicle_reader => ( | ||
| 42 | is => 'ro', | ||
| 43 | isa => 'Data::Chronicle::Reader', | ||
| 44 | ); | ||
| 45 | |||
| 46 | has chronicle_writer => ( | ||
| 47 | is => 'ro', | ||
| 48 | isa => 'Data::Chronicle::Writer', | ||
| 49 | ); | ||
| 50 | |||
| 51 | around _document_content => sub { | ||
| 52 | my $orig = shift; | ||
| 53 | my $self = shift; | ||
| 54 | |||
| 55 | my @symbols = split '-', $self->symbol; | ||
| 56 | |||
| 57 | return { | ||
| 58 | %{$self->$orig}, | ||
| 59 | rates => $self->rates, | ||
| 60 | type => $self->type, | ||
| 61 | info => $symbols[0] . ' rates implied from ' . $symbols[1], | ||
| 62 | }; | ||
| 63 | }; | ||
| 64 | |||
| 65 | =head2 document | ||
| 66 | |||
| 67 | The document that this object is tied to. | ||
| 68 | |||
| 69 | =cut | ||
| 70 | |||
| 71 | has document => ( | ||
| 72 | is => 'rw', | ||
| 73 | lazy_build => 1, | ||
| 74 | ); | ||
| 75 | |||
| 76 | sub _build_document { | ||
| 77 | my $self = shift; | ||
| 78 | |||
| 79 | my $document = $self->chronicle_reader->get('interest_rates', $self->symbol); | ||
| 80 | |||
| 81 | if ($self->for_date and $self->for_date->epoch < Date::Utility->new($document->{date})->epoch) { | ||
| 82 | $document = $self->chronicle_reader->get_for('interest_rates', $self->symbol, $self->for_date->epoch); | ||
| 83 | |||
| 84 | #Assume empty data in case there is nothing in the database | ||
| 85 | $document //= {}; | ||
| 86 | } | ||
| 87 | |||
| 88 | return $document; | ||
| 89 | } | ||
| 90 | |||
| 91 | =head2 save | ||
| 92 | |||
| 93 | Save the document into Chronicle data-store | ||
| 94 | |||
| 95 | =cut | ||
| 96 | |||
| 97 | sub save { | ||
| 98 | my $self = shift; | ||
| 99 | |||
| 100 | if (not defined $self->chronicle_reader->get('interest_rates', $self->symbol)) { | ||
| 101 | $self->chronicle_writer->set('interest_rates', $self->symbol, {}); | ||
| 102 | } | ||
| 103 | |||
| 104 | return $self->chronicle_writer->set('interest_rates', $self->symbol, $self->_document_content); | ||
| 105 | } | ||
| 106 | |||
| 107 | has type => ( | ||
| 108 | is => 'ro', | ||
| 109 | isa => 'qf_interest_rate_type', | ||
| 110 | default => 'implied', | ||
| 111 | ); | ||
| 112 | |||
| 113 | no Moose; | ||
| 114 | __PACKAGE__->meta->make_immutable; | ||
| 115 | 1; |