Framework/ExpiryConventions.pm untested

filter: Statementstmt 0.0% 0 / 177 Branchbran 0.0% 0 / 66 Conditioncond 0.0% 0 / 33 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 307 CC 34 SCAR 33.1
CC
34
Cov
0%
vol_expiry_date
56.0
forward_expiry_date
56.0
_FX_forward_delivery_date
42.0
CRAP 27.4
1 package Quant::Framework::ExpiryConventions;
2  
3 use strict;
4 use warnings;
5  
6 use Moose;
7 use DateTime;
8 use Date::Utility;
9 use Quant::Framework::Currency;
10 use List::Util qw(min);
11  
12 has calendar => (
13 is => 'ro',
14 isa => 'Quant::Framework::TradingCalendar',
15 );
16  
17 has for_date => (
18 is => 'ro',
19 isa => 'Maybe[Date::Utility]',
20 default => undef,
21 );
22  
23 =head2 chronicle_reader
24  
25 Used to work with Chronicle storage data (Holidays and Partial trading data)
26  
27 =cut
28  
29 has chronicle_reader => (
30 is => 'ro',
31 isa => 'Data::Chronicle::Reader',
32 );
33  
34 has is_forex_market => (
35 is => 'ro',
36 );
37  
38 =head2 symbol
39  
40 What is the proper-cased symbol for our underlying?
41  
42 =cut
43  
44 has 'symbol' => (
45 is => 'ro',
46 isa => 'Str',
47 required => 1,
48 );
49  
50 has quoted_currency => (
51 is => 'ro',
52 isa => 'Maybe[Quant::Framework::Currency]',
53 );
54  
55 has asset_symbol => (
56 is => 'ro',
57 lazy_build => 1,
58 );
59  
60 sub _build_asset_symbol {
61 my $self = shift;
62 my $symbol = '';
63  
64 if ($self->symbol =~ /^FUT(\w+)_/) {
65 $symbol = $1;
66 }
67  
68 return $symbol;
69 }
70  
71 has asset => (
72 is => 'ro',
73 );
74  
75 # This returns number of days after the trade date which determine the delivery
76 # and spot date. Except USDCAD which is 1 day, other are all 2 days.
77 has _days_for_settlement => (
78 is => 'ro',
79 isa => 'Int',
80 lazy => 1,
81 builder => '_build__days_for_settlement',
82 );
83  
84 sub _build__days_for_settlement {
85 my $self = shift;
86 my $days = 2;
87 if ($self->symbol and $self->symbol eq 'frxUSDCAD') {
88 $days = 1;
89 }
90  
91 return $days;
92 }
93  
94 sub _is_good_business_day {
95 my ($self, $date) = @_;
96  
97 my $holiday_haver = (ref $self->asset =~ /Currency$/) ? $self->asset : $self->calendar;
98  
99 if ( $date->is_a_weekend
100 or $holiday_haver->has_holiday_on($date)
101 or $self->quoted_currency->has_holiday_on($date))
102 {
103 return;
104 }
105  
106 return 1;
107 }
108  
109 sub _is_good_expiry_day {
110 my ($self, $date) = @_;
111  
112 if ($date->is_a_weekend or $date->date_ddmmmyy =~ /^(?:1\-Jan|25\-Dec)/) {
113 return;
114 }
115  
116 return 1;
117 }
118  
119 sub _is_good_settlement_day {
120 my ($self, $date) = @_;
121  
122 if (not $self->_is_good_business_day($date)) {
123 return;
124 }
125  
126 if (
127 Quant::Framework::Currency->new({
128 symbol => 'USD',
129 chronicle_reader => $self->chronicle_reader,
130 }
131 )->has_holiday_on($date))
132 {
133 return;
134 }
135  
136 return 1;
137 }
138  
139 sub _get_interim_date {
140 my ($self, $date) = @_;
141  
142 my $interim_date = Date::Utility->new($date->epoch + 86400);
143 if ($self->symbol !~ /USD/) {
144 if (not $self->_is_good_business_day($interim_date)) {
145 $interim_date = $self->_get_interim_date($interim_date);
146 }
147 } else {
148 my $non_USD_currency =
149 ($self->asset_symbol =~ /USD/)
150 ? $self->quoted_currency
151 : $self->asset;
152 if ( $non_USD_currency->has_holiday_on($interim_date)
153 or $interim_date->is_a_weekend)
154 {
155 $interim_date = $self->_get_interim_date($interim_date);
156 }
157 }
158  
159 return $interim_date;
160 }
161  
162 # See Clark, Foreign exchange option pricing, p5 s1.4 for an explanation.
163 sub _spot_date {
164 my ($self, $date) = @_;
165 my $spot_date;
166 my $interim_date;
167  
168 if ($self->_days_for_settlement == 1) {
169 $spot_date = Date::Utility->new($date->epoch + 86400);
170 } elsif ($self->_days_for_settlement == 2) {
171 $interim_date = $self->_get_interim_date($date);
172 $spot_date = Date::Utility->new($interim_date->epoch + 86400);
173 }
174  
175 while (not $self->_is_good_settlement_day($spot_date)) {
176 $spot_date = Date::Utility->new($spot_date->epoch + 86400);
177 }
178  
179 return $spot_date;
180 }
181  
182 =head2 vol_expiry_date
183  
184 $underlying->vol_expiry_date({from => $date, term => '1W'});
185  
186 =cut
187  
188 sub vol_expiry_date {
189 my ($self, $args) = @_;
190 my ($from, $term) = @{$args}{qw(from term)};
191  
192 my $expiry_date;
193  
194 $term = '1D' if ($term =~ /^o\/?n$/i);
195  
196 if ($term =~ /(\d+)([DW])/) {
197 my ($days, $unit) = ($1, $2);
198 $days *= 7 if ($unit eq 'W');
199  
200 $expiry_date = Date::Utility->new($from->epoch + (86400 * $days));
201  
202 if (not $self->_is_good_expiry_day($expiry_date)) {
203 $expiry_date = $self->vol_expiry_date({
204 from => $expiry_date,
205 term => '1D'
206 });
207 }
208 } else {
209 my $months = ($term =~ /^\s?(\d+)([MY])$/ and $2 eq 'M') ? $1 : $1 * 12;
210  
211 if ($self->is_forex_market) {
212 $expiry_date = $self->_FX_month_and_year_term_vol_expiry_date({
213 from => $from,
214 months => $months
215 });
216 } else {
217 $expiry_date = $self->_EQ_month_and_year_term_vol_expiry_date({
218 from => $from,
219 months => $months
220 });
221 }
222 }
223  
224 return $expiry_date;
225 }
226  
227 # When the term is expressed in months or years, the
228 # logic for FX and EQ differs. Here is the FX logic.
229 sub _FX_month_and_year_term_vol_expiry_date {
230 my ($self, $args) = @_;
231 my ($from, $months) = @{$args}{qw(from months)};
232  
233 # Step 1: To obtain the valid forward delivery date
234 my $forward_delivery_date = $self->_FX_forward_delivery_date({
235 from => $from,
236 months => $months
237 });
238 my $delivery_date = $forward_delivery_date;
239  
240 # Step 2: To obtain the expiry date by shift the forward_delivery_date backward
241 while (not $self->_is_good_expiry_day($forward_delivery_date)
242 or $self->_spot_date($forward_delivery_date)->epoch > $delivery_date->epoch)
243 {
244 $forward_delivery_date = Date::Utility->new($forward_delivery_date->epoch - 86400);
245 }
246  
247 my $expiry_date = $forward_delivery_date;
248  
249 return $expiry_date;
250 }
251  
252 sub _FX_forward_delivery_date {
253 my ($self, $args) = @_;
254 my ($from, $months) = @{$args}{qw(from months)};
255  
256 my $expiry_date;
257 my $last_day_of_month_rule;
258  
259 # Step 1: Get the spot date
260 my $spot_date = $self->_spot_date($from);
261  
262 # Step 2: Shift forward by the maturity days
263 my $spot_month = $spot_date->month;
264 my $spot_year = $spot_date->year;
265 my $spot_day = $spot_date->day_of_month;
266  
267 my $forward_month = $spot_month + $months;
268 my $forward_year = $spot_year;
269 my $forward_day = $spot_day;
270  
271 while ($forward_month > 12) {
272 $forward_month = $forward_month - 12;
273 $forward_year = $forward_year + 1;
274 }
275  
276 # Step 3: To obtain the valid day_of_month for forward month
277  
278 # 3.1: First check if the spot date itself is drop on last business day of the spot month, then the delivery date should set as last day of forward month
279 # Example: If the spot date is 31Jan2011, the 1M delivery date must set as 28-Feb-11 and not 31-Feb-11
280 my $first_day_of_next_mth_after_spot_mth = Date::Utility->new('1-' . $spot_date->months_ahead(1));
281 my $last_trading_day_of_spot_mth = $self->calendar->trade_date_before($first_day_of_next_mth_after_spot_mth, {lookback => 1});
282  
283 # This is just to build the date for forward month so that we can obtain the last day of forward month
284 my $forward_date = Date::Utility->new(
285 DateTime->new(
286 year => $forward_year,
287 month => $forward_month,
288 day => 1,
289 ));
290  
291 my $last_day_of_forward_month = $forward_date->days_in_month;
292  
293 if ($spot_date->epoch == $last_trading_day_of_spot_mth->epoch) {
294 $forward_day = $last_day_of_forward_month;
295 $last_day_of_month_rule = 1;
296 }
297  
298 # 3.2: Check if the days_of_month of spot month is greater than the last_days_of_month on forward month, then the forward deliver date should set on the last_days_of_month on forward month
299 # Example: If the spot date is 30Jan2011, the 1M delivery date must set as 28-Feb-11 and not 30-Feb-11
300  
301 if ($forward_day > $last_day_of_forward_month) {
302 $forward_day = $last_day_of_forward_month;
303 $last_day_of_month_rule = 1;
304  
305 } elsif ($forward_day < $last_day_of_forward_month) {
306 $last_day_of_month_rule = 0;
307 }
308  
309 #Step 4: convert the forward date to Date::Utility date format
310 $forward_date = Date::Utility->new(
311 DateTime->new(
312 year => $forward_year,
313 month => $forward_month,
314 day => $forward_day,
315 )->epoch
316 );
317 my $forward_delivery_date =
318 Date::Utility->new($forward_date->day_of_month . '-' . $forward_date->month_as_string . '-' . $forward_date->year_in_two_digit);
319  
320 # Step 5: To obtain valid delivery date
321 if ($last_day_of_month_rule) {
322 while (not $self->_is_good_settlement_day($forward_delivery_date)) {
323 $forward_delivery_date = Date::Utility->new($forward_delivery_date->epoch - 86400);
324 }
325 } else {
326 while (not $self->_is_good_settlement_day($forward_delivery_date)) {
327 $forward_delivery_date = Date::Utility->new($forward_delivery_date->epoch + 86400);
328 }
329  
330 # This is to make sure that the delivery date would be forward to next month
331 if ($forward_delivery_date->month > $forward_month) {
332 $forward_delivery_date = Date::Utility->new($forward_delivery_date->epoch - 86400);
333  
334 while (not $self->_is_good_settlement_day($forward_delivery_date)) {
335 $forward_delivery_date = Date::Utility->new($forward_delivery_date->epoch - 86400);
336 }
337 }
338 }
339  
340 return $forward_delivery_date;
341 }
342  
343 sub _EQ_month_and_year_term_vol_expiry_date {
344 my ($self, $args) = @_;
345 my ($from, $months) = @{$args}{qw(from months)};
346  
347 my $last_day;
348 my $expiry_month = $from->months_ahead($months);
349 my $days_in_month = Date::Utility->new('1-' . $expiry_month)->days_in_month;
350 my $day_str = min($days_in_month, $from->day_of_month);
351 my $expiry_date = Date::Utility->new($day_str . '-' . $from->months_ahead($months));
352  
353 # Blatant abuse of holes in Date::Utility integrity.
354 if ($expiry_date->days_in_month <= $expiry_date->day_of_month) {
355 $expiry_date = Date::Utility->new($expiry_date->days_in_month . '-' . $expiry_date->months_ahead(0));
356 $last_day++;
357 }
358  
359 # No weekends.
360 if ($expiry_date->is_a_weekend) {
361 $expiry_date =
362 $last_day
363 ? $self->calendar->trade_date_before($expiry_date)
364 : $self->calendar->trade_date_after($expiry_date);
365 }
366  
367 return $expiry_date;
368 }
369  
370 =head2 forward_expiry_date
371  
372 $underlying->forward_expiry_date({from => $date, term => '1W'});
373  
374 For Forward, the expiry date is calculated forward from the spot date.
375 Example: On 4-Jul-12, the expiry date for 1W is 13-Jul-12.
376 - Spot date is 6-Jul-12, 7 days forward from this 6-Jul-12 is 13-Jul-12
377  
378 =cut
379  
380 sub forward_expiry_date {
381 my ($self, $args) = @_;
382 my ($from, $term) = @{$args}{qw(from term)};
383  
384 my $expiry_date;
385 my $days;
386 if ($term =~ /^o\/?n$/i or $term eq '1D') {
387 $days = 1;
388 $expiry_date = Date::Utility->new($from->epoch + (86400 * $days));
389 if (not $self->_is_good_settlement_day($expiry_date)) {
390 $expiry_date = $self->forward_expiry_date({
391 from => $expiry_date,
392 term => '1D'
393 });
394 }
395 } elsif ($term =~ /(\d+)([DW])/ and $term ne '1D') {
396  
397 my $days = ($term =~ /^(\d)([DW])$/ and $2 eq 'W') ? $1 * 7 : $1;
398  
399 # Step 1: Get the spot date
400 my $spot_date = $self->_spot_date($from);
401  
402 # Step 2: Shift forward by the maturity days
403 my $forward_date = Date::Utility->new($spot_date->epoch + $days * 86400);
404  
405 while (not $self->_is_good_settlement_day($forward_date)) {
406 $forward_date = Date::Utility->new($forward_date->epoch + 86400);
407 }
408  
409 $expiry_date = $forward_date;
410  
411 } elsif ($term =~ /(\d+)([MY])/) {
412  
413 my $months = ($term =~ /^(\d+)([MY])$/ and $2 eq 'M') ? $1 : $1 * 12;
414  
415 $expiry_date = $self->_FX_forward_delivery_date({
416 from => $from,
417 months => $months
418 });
419  
420 }
421  
422 return $expiry_date;
423 }
424  
425 1;