Framework/CorporateAction.pm

filter: Statementstmt 37.5% 18 / 48 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 50.0% 5 / 10 Podpod 100% 5 / 5 totaltotal 32.9% 28 / 85 CC 10 SCAR 37.8
CC
10
Cov
28%
update
90.0
load
6.0
create
2.0
CRAP 43.8
« Framework/VolSurface/Validator.pm ↑ Summary Framework/TradingCalendar.pm »
1 package Quant::Framework::CorporateAction;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Date::Utility;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Quant::Framework::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Quant::Framework::CorporateAction
13  
14 =head1 DESCRIPTION
15  
16 Represents the corporate actions data of an underlying from database. Create
17 new unpersistent CorporateAction can be done via:
18  
19 my $ca = Quant::Framework::CorporateAction::create($storage_accessor, 'QWER', Date::Utility->new);
20  
21 Obviously, it will have empty actions hash
22  
23 $ca->actions; # empty hashref by default
24  
25 Corporate actions can be persisted (stored in Chronicle) via C<save> method
26  
27 $ca->save;
28  
29 Load persisted actions can be done via:
30  
31 my $ca = Quant::Framework::CorporateAction::load($storage_accessor, 'QWER', Date::Utility->new);
32  
33 It will return C<undef> if there are no presisted actions in Chronicle. The date can be
34 ommitted, than, it will try to load the most recent (last) corporate actions, i.e.
35  
36 my $ca = Quant::Framework::CorporateAction::load($storage_accessor, 'QWER');
37  
38  
39 To update actions, the C<update> method should be invoked, with appropriate structure, i.e.
40  
41 my $ca = $ca->update({
42 "62799500" => {
43 "monitor_date" => "2014-02-07T06:00:07Z",
44 "type" => "ACQUIS",
45 "monitor" => 1,
46 "description" => "Acquisition",
47 "effective_date" => "15-Jul-14",
48 "flag" => "N",
49 }, Date::Utility->new);
50  
51 The C<update> in scalar context will return B<new unpersisted> Corporate object. You have
52 to presist it via C<save> method.
53  
54 In the list context it will return new unpersisted Corporate object, and two hasref for
55 new and cancelled action.
56  
57  
58 =cut
59  
60 =head1 ATTRIBUTES
61  
62 =head2 document
63  
64 The document, which actually incorporates all data. Can be used to get the date of corporate
65 actions, e.g.:
66  
67 $corporate_actions->document->recorded_date
68  
69 =cut
70  
71 2 has document => (
72 is => 'ro',
73 required => 1,
74 );
75  
76 2 my $_NAMESPACE = 'corporate_actions';
77  
78 =head1 SUBROUTINES
79  
80 =head2 create ($storage_accessor, $symbol, $for_date)
81  
82 my $corp_actions = Quant::Framework::CorporateAction::crate($storage_accessor, "USAAPL", Date::Utility->new)
83  
84 Creates a new unsaved corporate actions.
85  
86 =cut
87  
88 sub create {
89 0 ▶ my ($storage_accessor, $symbol, $for_date) = @_;
Subroutine create: not called CC=1 SCAR=6.9
Pod documented
90 0 my $document = Quant::Framework::Document->new(
91 storage_accessor => $storage_accessor,
92 recorded_date => $for_date,
93 symbol => $symbol,
94 data => {actions => {}},
95 namespace => $_NAMESPACE,
96 );
97  
98 0 return __PACKAGE__->new(
99 document => $document,
100 );
101 }
102  
103 =head2 load ($storage_accessor, $symbol, $for_date)
104  
105 my $corp_actions = Quant::Framework::CorporateAction::load($storage_accessor, "USAAPL", Date::Utility->new)
106  
107 Loads the corporate actions for the specified symbol at the specified date. The date can
108 be omitted, then it loads the most recent corporate actions.
109  
110 Might return undef, if no actions exists in Chronicle
111  
112 =cut
113  
114 sub load {
115 0 ▶ my ($storage_accessor, $symbol, $for_date) = @_;
Subroutine load: not called CC=2 SCAR=17.9
Pod documented
116  
117 0 ▶ my $document = Quant::Framework::Document::load($storage_accessor, $_NAMESPACE, $symbol, $for_date)
Branch: 1 branch
BranchTrueFalseTotal
my $document = Quant::Framework::Document::load($storage_accessor, $_NAMESPACE, $symbol, $for_date) or return 0 0 0
118 or return;
119  
120 0 return __PACKAGE__->new(
121 document => $document,
122 );
123 }
124  
125 =head2 save
126  
127 $corp_actions->save;
128  
129 Stores corporate actions in chronicle.
130  
131 =cut
132  
133 sub save {
134 0 ▶ my $self = shift;
Subroutine save: not called CC=1 SCAR=6.9
Pod documented
135 0 $self->document->save;
136 0 return;
137 }
138  
139 =head2 update($actions, $date);
140  
141 my $new_corp_actions = $corp_actions->update({
142 "32799500" => {
143 "monitor_date" => "2015-02-07T06:00:07Z",
144 "type" => "DIV",
145 "monitor" => 1,
146 "description" => "Divided Stocks",
147 "effective_date" => "15-Jul-15",
148 "flag" => "N"
149 },
150 }, Date::Utility->new)
151 $new_corp_actions->save;
152  
153 Takes the existing actions, applies "diff" of actions in Bloomberg
154 format (i.e. adds new actions, or cancels exising ones), and
155 returns new unpersisted (non-saved) CorporateAction object.
156  
157 You have to invoke C<save> method to persist new corporate actions
158 in Chronicle.
159  
160 The C<$date> argument in mandatory.
161  
162 =cut
163  
164 sub update {
165 0 ▶ my ($self, $actions, $new_date) = @_;
Subroutine update: not called CC=9 SCAR=45.0
Pod documented
166  
167 # clone original data
168 0 my $original = $self->document->data;
169 0 my $data = {%$original};
170  
171 0 my %new;
172 0 foreach my $action_id (keys %$actions) {
173 # flag 'N' = New & 'U' = Update
174 0 my $action = $actions->{$action_id};
175 my $is_new = ($action->{flag} eq 'N' and not $original->{actions}->{$action_id})
176 0 ▶ || $action->{flag} eq 'U';
Condition0%0/6
$action->{'flag'} eq 'N' && !$original->{'actions'}{$action_id} || $action->{'flag'} eq 'U'
l!l&&r!l&&!r
000
$action->{"flag"} eq "N" and not $original->{"actions"}{$action_id}
!ll&&!rl&&r
000
MC/DC0%0/3
$action->{'flag'} eq 'N' && !$original->{'actions'}{$action_id} || $action->{'flag'} eq 'U'
$action->{"flag"} eq "N" not $original->{"actions"}{$action_id} $action->{'flag'} eq 'U'
Truth table0%0/5
A and not(B) || C
A= $action->{"flag"} eq "N"B= not $original->{"actions"}{$action_id}C= $action->{'flag'} eq 'U'
ABCresult
11X1
0X11
1011
0X00
1000
177  
178 0 ▶ $new{$action_id} = $action if ($is_new);
Branch: 1 branch
BranchTrueFalseTotal
if $is_new 0 0 0
179 }
180  
181 0 my %merged_actions = (%{$data->{actions}}, %new);
182  
183 0 my %cancelled;
184 0 foreach my $action_id (keys %$actions) {
185 0 my $action = $actions->{$action_id};
186 # flag 'D' = Delete
187 0 ▶ if ($action->{flag} eq 'D' and $original->{actions}->{$action_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action->{"flag"} eq "D" and $original->{"actions"}{$action_id}) 0 0 0
Condition0%0/3
$action->{"flag"} eq "D" and $original->{"actions"}{$action_id}
!ll&&!rl&&r
000
MC/DC0%0/2
$action->{"flag"} eq "D" and $original->{"actions"}{$action_id}
$action->{"flag"} eq "D" $original->{"actions"}{$action_id}
Truth table0%0/3
A and B
A= $action->{"flag"} eq "D"B= $original->{"actions"}{$action_id}
ABresult
0X0
100
111
188 0 $cancelled{$action_id} = $action;
189 0 delete $merged_actions{$action_id};
190 }
191 }
192  
193 0 $data->{actions} = \%merged_actions;
194  
195 0 my $new_document = Quant::Framework::Document->new(
196 data => $data,
197 storage_accessor => $self->document->storage_accessor,
198 recorded_date => $new_date,
199 symbol => $self->document->symbol,
200 namespace => $_NAMESPACE,
201 );
202 0 my $new_ca = __PACKAGE__->new(document => $new_document);
203  
204 0 ▶ return wantarray ? ($new_ca, \%new, \%cancelled) : $new_ca;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
205 }
206  
207 =head2 actions
208  
209 my $actions = $corp_actions->actions;
210  
211 Returns hashref of actions in Bloomberg format. If there are no actions, the empty hashref
212 is returned.
213  
214 =cut
215  
216 sub actions {
217 0 ▶ return shift->document->data->{actions};
Subroutine actions: not called CC=1 SCAR=6.9
Pod documented
218 }
219  
220 2 1;
« Framework/VolSurface/Validator.pm ↑ Summary Framework/TradingCalendar.pm »