Framework/Dividend.pm untested

filter: Statementstmt 0.0% 0 / 108 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 15 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 167 CC 13 SCAR 24.6
CC
13
Cov
0%
dividend_rate_for
30.0
rate_for
12.0
get_discrete_dividend_for_period
12.0
CRAP 11.7
1 package Quant::Framework::Dividend;
2  
3 =head1 NAME
4  
5 Quant::Framework::Dividend - A module to save/load dividends
6  
7 =head1 DESCRIPTION
8  
9 This module saves/loads dividends data to/from Chronicle.
10 To save dividends for a company:
11  
12 my $corp_dividends = Quant::Framework::Dividends->new(symbol => $symbol,
13 rates => { 1 => 0, 2 => 1, 3=> 0.04 }
14 discrete_points => { '2015-04-24' => 0, '2015-09-09' => 0.134 });
15 $corp_dividends->save;
16  
17 To read dividends information for a company:
18  
19 my $corp_dividends = Quant::Framework::Dividends->new(symbol => $symbol);
20  
21 my $rates = $corp_dividends->rates;
22 my $disc_points = $corp_dividends->discrete_points;
23  
24 =cut
25  
26 use Moose;
27 extends 'Quant::Framework::Utils::Rates';
28  
29 use Data::Chronicle::Reader;
30 use Data::Chronicle::Writer;
31  
32 =head2 underlying_config
33  
34 Used for more advanced query of Dividen. For simple `rate_for` queries,
35 this is not required.
36  
37 =cut
38  
39 has underlying_config => (
40 is => 'ro',
41 );
42  
43 =head2 for_date
44  
45 The date for which we wish data
46  
47 =cut
48  
49 has for_date => (
50 is => 'ro',
51 isa => 'Maybe[Date::Utility]',
52 default => undef,
53 );
54  
55 has chronicle_reader => (
56 is => 'ro',
57 isa => 'Data::Chronicle::Reader',
58 );
59  
60 has chronicle_writer => (
61 is => 'ro',
62 isa => 'Data::Chronicle::Writer',
63 );
64  
65 has document => (
66 is => 'rw',
67 lazy_build => 1,
68 );
69  
70 sub _build_document {
71 my $self = shift;
72  
73 my $document = $self->chronicle_reader->get('dividends', $self->symbol);
74  
75 if ($self->for_date and $self->for_date->epoch < Date::Utility->new($document->{date})->epoch) {
76 $document = $self->chronicle_reader->get_for('dividends', $self->symbol, $self->for_date->epoch);
77  
78 # This works around a problem with Volatility surfaces and negative dates to expiry.
79 # We have to use the oldest available surface.. and we don't really know when it
80 # was relative to where we are now.. so just say it's from the requested day.
81 # We do not allow saving of historical surfaces, so this should be fine.
82 $document //= {};
83 $document->{date} = $self->for_date->datetime_iso8601;
84 }
85  
86 return $document;
87 }
88 around _document_content => sub {
89 my $orig = shift;
90 my $self = shift;
91  
92 return {
93 %{$self->$orig},
94 rates => $self->rates,
95 discrete_points => $self->discrete_points,
96 date => $self->recorded_date->datetime_iso8601,
97 };
98 };
99  
100 =head2 save
101  
102 Saves dividend data to the provided Chronicle storage
103  
104 =cut
105  
106 sub save {
107 my $self = shift;
108  
109 #if chronicle does not have this document, first create it because in document_content we will need it
110 if (not defined $self->chronicle_reader->get('dividends', $self->symbol)) {
111 $self->chronicle_writer->set('dividends', $self->symbol, {});
112 }
113  
114 return $self->chronicle_writer->set('dividends', $self->symbol, $self->_document_content, $self->recorded_date);
115 }
116  
117 =head2 recorded_date
118  
119 The date (and time) that the dividend was recorded, as a Date::Utility.
120  
121 =cut
122  
123 has recorded_date => (
124 is => 'ro',
125 isa => 'Date::Utility',
126 lazy_build => 1,
127 );
128  
129 sub _build_recorded_date {
130 my $self = shift;
131 return Date::Utility->new($self->document->{date});
132 }
133  
134 =head2 discrete_points
135  
136 The discrete dividend points received from provider.
137  
138 =cut
139  
140 has discrete_points => (
141 is => 'ro',
142 lazy_build => 1,
143 );
144  
145 sub _build_discrete_points {
146 my $self = shift;
147  
148 return if not defined $self->document;
149 return $self->document->{discrete_points} || undef;
150 }
151  
152 =head2 rate_for
153  
154 Returns the rate for a particular timeinyears for symbol.
155 ->rate_for(7/365)
156  
157 =cut
158  
159 sub rate_for {
160 my ($self, $tiy) = @_;
161  
162 # Handle discrete dividend
163 my ($nearest_yield_days_before, $nearest_yield_before) = (0, 0);
164 my $days_to_expiry = $tiy * 365.0;
165 my @sorted_expiries = sort { $a <=> $b } keys(%{$self->rates});
166 foreach my $day (@sorted_expiries) {
167 if ($day <= $days_to_expiry) {
168 $nearest_yield_days_before = $day;
169 $nearest_yield_before = $self->rates->{$day};
170 next;
171 }
172 last;
173 }
174  
175 # Re-annualize
176 my $discrete_points = $nearest_yield_before * $nearest_yield_days_before / 365;
177  
178 if ($days_to_expiry) {
179 return $discrete_points * 365 / ($days_to_expiry * 100);
180 }
181 return 0;
182 }
183  
184 =head2 dividend_rate_for
185  
186 Get the dividend rate for this underlying over a given time period (expressed in timeinyears.)
187  
188 =cut
189  
190 sub dividend_rate_for {
191 my ($self, $tiy) = @_;
192  
193 die 'Attempting to get dividend rate on an undefined asset symbol for ' . $self->underlying_config->symbol
194 unless (defined $self->underlying_config->asset_symbol);
195  
196 return $self->underlying_config->default_dividend_rate if defined $self->underlying_config->default_dividend_rate;
197  
198 my $rate;
199  
200 # timeinyears cannot be undef
201 $tiy ||= 0;
202 my $type = $self->underlying_config->asset_class;
203  
204 my $which = $type eq 'currency' ? 'Quant::Framework::Currency' : 'Quant::Framework::Asset';
205  
206 my $asset = $which->new({
207 symbol => $self->underlying_config->asset_symbol,
208 for_date => $self->for_date,
209 chronicle_reader => $self->chronicle_reader,
210 chronicle_writer => $self->chronicle_writer,
211 });
212  
213 if ($self->underlying_config->uses_implied_rate_for_asset) {
214 $rate = $asset->rate_implied_from($self->underlying_config->rate_to_imply_from, $tiy);
215 } else {
216 $rate = $asset->rate_for($tiy);
217 }
218  
219 return $rate;
220 }
221  
222 =head2 get_discrete_dividend_for_period
223  
224 Returns discrete dividend for the given (start,end) dates and dividend recorded date for the underlying specified using `underlying_config`
225  
226 =cut
227  
228 sub get_discrete_dividend_for_period {
229 my ($self, $args) = @_;
230  
231 my ($start, $end) =
232 map { Date::Utility->new($_) } @{$args}{'start', 'end'};
233  
234 my %valid_dividends;
235 my $discrete_points = $self->discrete_points;
236 my $dividend_recorded_date = $self->recorded_date;
237  
238 if ($discrete_points and %$discrete_points) {
239 my @sorted_dates =
240 sort { $a->epoch <=> $b->epoch }
241 map { Date::Utility->new($_) } keys %$discrete_points;
242  
243 foreach my $dividend_date (@sorted_dates) {
244 if ( not $dividend_date->is_before($start)
245 and not $dividend_date->is_after($end))
246 {
247 my $date = $dividend_date->date_yyyymmdd;
248 $valid_dividends{$date} = $discrete_points->{$date};
249 }
250 }
251 }
252  
253 return ($dividend_recorded_date, \%valid_dividends);
254 }
255  
256 =head2 dividend_adjustments_for_period
257  
258 Returns dividend adjustments for given start/end period
259  
260 =cut
261  
262 sub dividend_adjustments_for_period {
263 my ($self, $args) = @_;
264  
265 my ($dividend_recorded_date, $applicable_dividends) =
266 ($self->underlying_config->market_prefer_discrete_dividend)
267 ? $self->get_discrete_dividend_for_period($args)
268 : {};
269  
270 my ($start, $end) = @{$args}{'start', 'end'};
271 my $duration_in_sec = $end->epoch - $start->epoch;
272  
273 my ($dS, $dK) = (0, 0);
274 foreach my $date (keys %$applicable_dividends) {
275 my $adjustment = $applicable_dividends->{$date};
276 my $effective_date = Date::Utility->new($date);
277 my $sec_away_from_action = ($effective_date->epoch - $start->epoch);
278 my $duration_in_year = $sec_away_from_action / (86400 * 365);
279  
280 my $ir = Quant::Framework::InterestRate->new({
281 symbol => $self->symbol,
282 underlying_config => $self->underlying_config,
283 chronicle_reader => $self->chronicle_reader,
284 chronicle_writer => $self->chronicle_writer
285 });
286  
287 #TODO: rewrite this using an instance of InterestRate
288 my $r_rate = $ir->interest_rate_for($duration_in_year);
289  
290 my $adj_present_value = $adjustment * exp(-$r_rate * $duration_in_year);
291 my $s_adj = ($duration_in_sec - $sec_away_from_action) / ($duration_in_sec) * $adj_present_value;
292 $dS -= $s_adj;
293 my $k_adj = $sec_away_from_action / ($duration_in_sec) * $adj_present_value;
294 $dK += $k_adj;
295 }
296  
297 return {
298 barrier => $dK,
299 spot => $dS,
300 recorded_date => $dividend_recorded_date,
301 };
302 }
303  
304 no Moose;
305 __PACKAGE__->meta->make_immutable;
306 1;