Framework/VolSurface/Cutoff.pm untested

filter: Statementstmt 0.0% 0 / 71 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 104 CC 4 SCAR 14.8
CC
4
Cov
0%
cutoff_date_for_effective_day
12.0
_build_code_gmt
6.0
_build__mytz
2.0
CRAP 4.4
1 package Quant::Framework::VolSurface::Cutoff;
2  
3 use Moose;
4 use DateTime;
5 use DateTime::TimeZone;
6 use Date::Utility;
7 use Quant::Framework::VolSurface::Utils;
8 use Quant::Framework::Utils::Types;
9  
10 =head1 Quant::Framework::VolSurface::Cutoff coercion
11  
12 If you'd like to coerce a Quant::Framework::VolSurface::Cutoff from a String,
13 the coercion rule is here.
14  
15 package MyClass;
16 use Quant::Framework::VolSurface::Cutoff;
17 has cutoff => (
18 isa => 'Quant::Framework::VolSurface::Cutoff',
19 coerce => 1,
20 );
21  
22 package main;
23 my $instance = MyClass->new(cutoff => 'New York 10:00');
24  
25 =cut
26  
27 =head1 ATTRIBUTES
28  
29 =head2 code
30  
31 Cutoff code denoting timezone of cutoff (e.g. New York 10:00)
32  
33 =cut
34  
35 has code => (
36 is => 'ro',
37 isa => 'qf_cutoff_code',
38 required => 1,
39 );
40  
41 =head2 code_gmt
42  
43 =cut
44  
45 has code_gmt => (
46 is => 'rw',
47 isa => 'qf_cutoff_code',
48 lazy_build => 1,
49 );
50  
51 has _mytz => (
52 is => 'ro',
53 lazy_build => 1,
54 );
55  
56 sub _build__mytz {
57 return DateTime::TimeZone->new(name => shift->timezone);
58 }
59  
60 sub _build_code_gmt {
61 my $self = shift;
62 my $offset = $self->_mytz->offset_for_datetime(DateTime->now());
63  
64 $self->code =~ /^(.+) (\d{1,2}):(\d{2})/;
65 my $hour = $2;
66 my $minute = $3;
67 if ($offset != 0) {
68 $hour -= ($offset / 3600);
69 }
70 return 'UTC ' . $hour . ':' . $minute;
71 }
72  
73 =head2 timezone
74  
75 =cut
76  
77 has timezone => (
78 is => 'ro',
79 isa => 'Str',
80 init_arg => undef,
81 lazy_build => 1,
82 );
83  
84 sub _build_timezone {
85 my $self = shift;
86  
87 my ($city) = ($self->code =~ /^(.+) \d{1,2}:\d{2}/);
88  
89 my %timezone_for = (
90 'Bangkok' => 'Asia/Bangkok',
91 'Beijing' => 'Asia/Shanghai',
92 'Bucharest' => 'Europe/Bucharest',
93 'Budapest' => 'Europe/Budapest',
94 'Colombia' => 'America/Bogota',
95 'Frankfurt' => 'Europe/Berlin',
96 'Hanoi' => 'Asia/Ho_Chi_Minh',
97 'Istanbul' => 'Europe/Istanbul',
98 'Jakarta' => 'Asia/Jakarta',
99 'Kuala Lumpur' => 'Asia/Kuala_Lumpur',
100 'London' => 'Europe/London',
101 'Manila' => 'Asia/Manila',
102 'Mexico' => 'America/Mexico_City',
103 'Moscow' => 'Europe/Moscow',
104 'Mumbai' => 'Asia/Kolkata',
105 'New York' => 'America/New_York',
106 'Santiago' => 'America/Santiago',
107 'Sao Paulo' => 'America/Sao_Paulo',
108 'Seoul' => 'Asia/Seoul',
109 'Singapore' => 'Asia/Singapore',
110 'Taipei' => 'Asia/Taipei',
111 'Taiwan' => 'Asia/Taipei',
112 'Tel Aviv' => 'Asia/Jerusalem',
113 'Tokyo' => 'Asia/Tokyo',
114 'Warsaw' => 'Europe/Warsaw',
115 'Wellington' => 'Pacific/Auckland',
116 'UTC' => 'UTC',
117 );
118  
119 # No need to default: anything that's a bom_cutoff_code will have a match.
120 return $timezone_for{$city};
121 }
122  
123 around BUILDARGS => sub {
124 my $orig = shift;
125 my $class = shift;
126 my @args = @_;
127  
128 if (scalar @args == 1) {
129 @args = (code => $args[0]);
130 }
131  
132 return $class->$orig(@args);
133 };
134  
135 =head1 METHODS
136  
137 =head2 seconds_to_cutoff_time
138  
139 Gives the number of BOM trading seconds from a given date
140 to the next cutoff, given a maturity and a calendar.
141  
142 =cut
143  
144 sub seconds_to_cutoff_time {
145 my ($self, $args) = @_;
146  
147 my $from = $args->{from} || die 'No "from" date given to seconds_to_cutoff_time.';
148 my $calendar = $args->{calendar} || die 'No Calendar given to seconds_to_cutoff_time.';
149 my $maturity = $args->{maturity} || die 'No maturity given to seconds_to_cutoff_time.';
150  
151 # From the given $from date and $maturity, we get the "effective day"
152 # on which the cutoff we are looking for falls on.
153 # (See effective_date attr of Quant::Framework::VolSurface for more details
154 # on what this is in practice.)
155 #
156 # To do so, we first move directly ahead to the effective day,
157 # then adjust into the period between subsequent NY1700 (the definition
158 # of effective day.
159 my $effective_day = $self->_vol_utils->effective_date_for($from->plus_time_interval($maturity . 'd'));
160  
161 my $cutoff_date = $self->cutoff_date_for_effective_day($effective_day, $calendar);
162  
163 my $seconds = $calendar->seconds_of_trading_between_epochs($from->epoch, $cutoff_date->epoch);
164  
165 return $seconds;
166 }
167  
168 has _vol_utils => (
169 is => 'ro',
170 isa => 'Quant::Framework::VolSurface::Utils',
171 init_arg => undef,
172 lazy => 1,
173 default => sub { Quant::Framework::VolSurface::Utils->new },
174 );
175  
176 =head2 cutoff_date_for_effective_day
177  
178 Will give you the cutoff date for a given effective day. The cutoff date
179 will always land within the same effective day as given, but may be in
180 the previous GMT day, as the effective day spans between NY1700s.
181  
182 =cut
183  
184 sub cutoff_date_for_effective_day {
185 my ($self, $effective_day, $calendar) = @_;
186  
187 # We start by truncating the given effective_day back to GMT midnight,
188 # then move forward to the cutoff time (in GMT). This is our cutoff date,
189 # unless it happens to fall on the next effective day, in which case we move
190 # back a day.
191  
192 $effective_day = $effective_day->truncate_to_day;
193  
194 my $cutoff_offset_from_GMT = $self->_mytz->offset_for_datetime(DateTime->from_epoch(epoch => $effective_day->epoch)); # % 86400;
195 my ($hours, $minutes) = ($self->code =~ /(\d{1,2}):(\d{2})$/);
196 my $cutoff_date = Date::Utility->new($effective_day->epoch - $cutoff_offset_from_GMT + $hours * 3600 + $minutes * 60);
197  
198 my $rollover_date = $self->_vol_utils->NY1700_rollover_date_on($effective_day);
199  
200 if ($cutoff_date->epoch > $rollover_date->epoch) {
201 $cutoff_date = Date::Utility->new($cutoff_date->epoch - 86400);
202 }
203  
204 # I put the $attempts login in place as I'm uncomfortable about adding
205 # a loop that could theoretically never break.
206 my $attempts;
207 while (not _valid_cutoff_date($self->_vol_utils->effective_date_for($cutoff_date), $calendar)) {
208 die('Could not find valid cutoff date after 10 attempts, so bailing out!') if ++$attempts > 10;
209 $cutoff_date = Date::Utility->new($cutoff_date->epoch + 86400);
210 }
211  
212 return $cutoff_date;
213 }
214  
215 sub _valid_cutoff_date {
216 my ($cutoff_date, $calendar) = @_;
217  
218 return $calendar->trades_on($cutoff_date);
219 }
220  
221 no Moose;
222 __PACKAGE__->meta->make_immutable;
223 1;