Framework/Currency.pm untested

filter: Statementstmt 0.0% 0 / 90 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 9 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 150 CC 12 SCAR 25.0
CC
12
Cov
0%
new
42.0
_build_daycount
12.0
weight_on
12.0
CRAP 12.2
1 package Quant::Framework::Currency;
2  
3 =head1 NAME
4  
5 Quant::Framework::Currency
6  
7 =head1 DESCRIPTION
8  
9 The representation of currency within our system
10  
11 my $currency = Quant::Framework::Currency->new({ symbol => 'AUD'});
12  
13 =cut
14  
15 use Carp;
16 use Moose;
17 use Scalar::Util qw(looks_like_number);
18 use List::Util qw(first);
19  
20 use Date::Utility;
21 use Data::Chronicle::Reader;
22 use Data::Chronicle::Writer;
23 use Quant::Framework::Holiday;
24 use Quant::Framework::InterestRate;
25 use Quant::Framework::ImpliedRate;
26  
27 =head2 symbol
28 Represents currency symbol.
29 =cut
30  
31 has symbol => (
32 is => 'ro',
33 required => 1,
34 );
35  
36 =head2 for_date
37 The Date::Utility wherein this underlying is fixed.
38 =cut
39  
40 has 'for_date' => (
41 is => 'ro',
42 isa => 'Maybe[Date::Utility]',
43 default => undef,
44 );
45  
46 =head2 chronicle_reader and chronicle_writer
47  
48 Used to work with Chronicle storage data.
49  
50 =cut
51  
52 has chronicle_reader => (
53 is => 'ro',
54 isa => 'Data::Chronicle::Reader',
55 );
56  
57 has chronicle_writer => (
58 is => 'ro',
59 isa => 'Data::Chronicle::Writer',
60 );
61  
62 has daycount => (
63 is => 'rw',
64 lazy_build => 1,
65 );
66  
67 sub _build_daycount {
68 my $self = shift;
69  
70 return 360 if first { $self->symbol eq $_ } qw(AED CHF CZK EGP EUR IDR JPY MXN NOK SAR SEK USD XAG XAU TRY);
71 return 365 if first { $self->symbol eq $_ } qw(AUD BRL CAD CNY GBP HKD INR KRW NZD PLN RUB SGD ZAR KWD);
72 return;
73 }
74  
75 has holidays => (
76 is => 'rw',
77 lazy_build => 1,
78 );
79  
80 sub _build_holidays {
81 my $self = shift;
82  
83 my $holidays_ref = Quant::Framework::Holiday::get_holidays_for($self->chronicle_reader, $self->symbol, $self->for_date);
84 my %holidays = map { Date::Utility->new($_)->days_since_epoch => $holidays_ref->{$_} } keys %$holidays_ref;
85 my $year = $self->for_date ? $self->for_date->year : Date::Utility->new->year;
86 # pseudo-holiday for country is on 24-Dec and 31-Dec annually
87 $holidays{Date::Utility->new($_ . '-' . $year)->days_since_epoch} = 'pseudo-holiday' for qw(24-Dec 31-Dec);
88  
89 return \%holidays;
90 }
91  
92 =head2 weight_on
93 Returns the weight assigned to the day of a given Date::Utility object. Return 0.0
94 for holidays and 1.0 if there is no weight set.
95 =cut
96  
97 sub weight_on {
98 my ($self, $when) = @_;
99 my $weight = $self->holidays->{$when->days_since_epoch};
100 return
101 (!defined $weight) ? 1.0
102 : ($weight eq 'pseudo-holiday') ? 0.5
103 : 0.0;
104 }
105  
106 =head2 has_holiday_on
107 Returns true if the currency has a holiday on the day of a given Date::Utility
108 object. Holidays with weights are not considered real holidays, this sub will
109 return 0 for them.
110 =cut
111  
112 sub has_holiday_on {
113 my ($self, $when) = @_;
114 my $weight = $self->holidays->{$when->days_since_epoch};
115 return defined $weight && $weight ne 'pseudo-holiday';
116 }
117  
118 =head2 interest
119 Returns an interest rates object
120 =cut
121  
122 has interest => (
123 is => 'ro',
124 isa => 'Quant::Framework::InterestRate',
125 lazy_build => 1,
126 );
127  
128 sub _build_interest {
129 my $self = shift;
130  
131 return Quant::Framework::InterestRate->new({
132 symbol => $self->symbol,
133 for_date => $self->for_date,
134 chronicle_reader => $self->chronicle_reader,
135 chronicle_writer => $self->chronicle_writer,
136 });
137 }
138  
139 =head2 rate_for
140 Returns market rate
141 =cut
142  
143 sub rate_for {
144 my ($self, $tiy) = @_;
145  
146 return $self->interest->rate_for($tiy);
147 }
148  
149 # cache of implied rates object(s)
150 has _cached => (
151 is => 'rw',
152 default => sub { return {} },
153 );
154  
155 =head2 rate_implied_from
156 Returns rates implied from another currency
157 $usd->rate_implied_from('JPY',$tiy) # returns rate of usd implied from jpy for a specific term
158 =cut
159  
160 sub rate_implied_from {
161 my ($self, $implied_from, $tiy) = @_;
162  
163 my $implied_symbol = $self->symbol . '-' . $implied_from;
164 return $self->_cached->{$implied_symbol}->rate_for($tiy)
165 if $self->_cached->{$implied_symbol};
166  
167 $self->_cached->{$implied_symbol} = Quant::Framework::ImpliedRate->new({
168 symbol => $implied_symbol,
169 for_date => $self->for_date,
170 chronicle_reader => $self->chronicle_reader,
171 chronicle_writer => $self->chronicle_writer,
172 });
173  
174 return $self->_cached->{$implied_symbol}->rate_for($tiy);
175 }
176  
177 has _build_time => (
178 is => 'ro',
179 default => sub { time },
180 );
181  
182 # we cache objects, when we're getting object from cache we should check if it isn't too old
183 # currently we allow age to be up to 30 seconds
184 sub _object_expired {
185 return shift->_build_time + 30 < time;
186 }
187  
188 =head1 METHODS
189  
190 =head2 new($symbol)
191  
192 Constructor. Should be used in the following ways:
193  
194 Saving new interest rates:
195 ->new(symbol => 'SYM', rates => {...}, date => Date::Utility->new);
196 ->save;
197  
198 Fetching the latest rates in DB:
199 ->new('SYM');
200 ->new(symbol => 'SYM');
201  
202 Fetching historical rates:
203 ->new(symbol => 'SYM', for_date => Date::Utility->new('some time in the past'));
204  
205 =cut
206  
207 my %_cached_objects;
208  
209 sub new {
210 my ($class, @given) = @_;
211 my %args =
212 (ref $given[0] eq 'HASH') ? %{$given[0]}
213 : (scalar @given == 1) ? (symbol => $given[0])
214 : @given;
215  
216 my $currency;
217  
218 # Shall we cache?
219 if (exists $args{symbol} and scalar keys %args == 1) {
220 my $symbol = $args{symbol};
221 $currency = $_cached_objects{$symbol};
222 if (not $currency or $currency->_object_expired) {
223 $currency = $class->_new(%args);
224 $_cached_objects{$symbol} = $currency
225 if (scalar keys %{$currency->interest->document});
226 }
227  
228 } else {
229 $currency = $class->_new(%args);
230 }
231  
232 return $currency;
233 }
234  
235 no Moose;
236 __PACKAGE__->meta->make_immutable(
237 constructor_name => '_new',
238 replace_constructor => 1
239 );
240  
241 1;