StockAccount.pm

filter: Statementstmt 75.0% 334 / 445 Branchbran 64.5% 80 / 124 Conditioncond 60.4% 29 / 48 MC/DCmcdc 37.9% 11 / 29 Subroutinesub 78.8% 41 / 52 Podpod 62.2% 28 / 45 totaltotal 70.3% 523 / 743 CC 90 SCAR 23.4
CC
90
Cov
72%
periodicStatsString
42.0
stockTransaction
29.9
availableAcquisitionsString
20.0
CRAP 10.4
« StockAccount/Import.pm ↑ Summary StockAccount/Import/OptionsXpress.pm »
1 package Finance::StockAccount;
2  
3 5 our $VERSION = '0.01';
4  
5 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 ▶use Finance::StockAccount::Set;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Finance::StockAccount::AccountTransaction;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Finance::StockAccount::Stock;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16  
17 ### Class definition
18 16 ▶sub new {
Subroutine new: called CC=4 SCAR=14.0
Pod documented
19 16 my ($class, $options) = @_;
20 16 my $self = {
21 sets => {},
22 skipStocks => {},
23 stats => $class->getNewStatsHash(),
24 allowZeroPrice => 0,
25 verbose => 1,
26 };
27 16 ▶ if ($options and exists($options->{allowZeroPrice}) and $options->{allowZeroPrice}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options and exists $options->{"allowZeroPrice"} and $options->{"allowZeroPrice"}) 1 15 16
Condition66%4/6
$options and exists $options->{"allowZeroPrice"} and $options->{"allowZeroPrice"}
!ll&&!rl&&r
1501
$options and exists $options->{"allowZeroPrice"}
!ll&&!rl&&r
1501
MC/DC33%1/3
$options and exists $options->{"allowZeroPrice"} and $options->{"allowZeroPrice"}
$options exists $options->{"allowZeroPrice"} $options->{"allowZeroPrice"}
Truth table50%2/4
A and B and C
A= $optionsB= exists $options->{"allowZeroPrice"}C= $options->{"allowZeroPrice"}
ABCresult
0XX0
10X0
1100
1111
28 1 $self->{allowZeroPrice} = 1;
29 }
30 16 return bless($self, $class);
31 }
32  
33 35 ▶sub getNewStatsHash {
Subroutine getNewStatsHash: called CC=1 SCAR=0.0
Pod undocumented
34 return {
35 35 stale => 1,
36 startDate => undef,
37 endDate => undef,
38 maxCashInvested => 0,
39 totalOutlays => 0,
40 totalRevenues => 0,
41 profit => 0,
42 profitOverOutlays => 0,
43 profitOverMaxCashInvested => 0,
44 profitOverYears => 0,
45 pomciOverYears => 0,
46 commissions => 0,
47 regulatoryFees => 0,
48 otherFees => 0,
49 numberOfTrades => 0,
50 numberExcluded => 0,
51 annualRatio => 0,
52 annualStats => undef,
53 annualStatsStale => 1,
54 quarterlyStats => undef,
55 quarterlyStatsStale => 1,
56 monthlyStats => undef,
57 monthlyStatsStale => 1,
58 };
59 }
60  
61  
62 ### For convenience text output
63 5 my $statsKeyHeadings = [qw(
64 Outlays Revenues MaxInvested Profit OverOut
65 OverInvested Commiss RegFees OthFees NumTrades
66 )];
67 5 my $statsKeyHeadingsPattern = "%12s %12s %12s %12s %7s %12s %9s %7s %7s %9s";
68  
69 5 my $statsKeys = [qw(
70 totalOutlays totalRevenues maxCashInvested profit profitOverOutlays
71 profitOverMaxCashInvested commissions regulatoryFees otherFees numberOfTrades
72 )];
73 5 my $statsKeysPattern = "%12.2f %12.2f %12.2f %12.2f %7.2f %12.2f %9.2f %7.2f %7.2f %9d";
74  
75 5 my $statsLinesArray = [
76 'First Trade Date' => 'startDate' => '%35s',
77 'Last Trade Date' => 'endDate' => '%35s',
78 'Maximum Cash Invested at Once' => 'maxCashInvested' => '%35.2f',
79 'Sum Outlays' => 'totalOutlays' => '%35.2f',
80 'Sum Revenues' => 'totalRevenues' => '%35.2f',
81 'Total Profit' => 'profit' => '%35.2f',
82 'Profit Over Years' => 'profitOverYears' => '%35.2f',
83 'Profit Over Sum Outlays' => 'profitOverOutlays' => '%35.2f',
84 'Profit Over Max Cash Invested' => 'profitOverMaxCashInvested' => '%35.2f',
85 'The Above (^) Over Years' => 'pomciOverYears' => '%35.2f',
86 'Total Commissions' => 'commissions' => '%35.2f',
87 'Total Regulatory Fees' => 'regulatoryFees' => '%35.2f',
88 'Total Other Fees' => 'otherFees' => '%35.2f',
89 'Num Trades Included in Stats' => 'numberOfTrades' => '%35d',
90 'Num Trades Excluded from Stats' => 'numberExcluded' => '%35d',
91 ];
92  
93  
94 ### Methods
95 281 ▶sub stale {
Subroutine stale: called CC=5 SCAR=16.6
Pod undocumented
96 281 my ($self, $assertion) = @_;
97 281 my $stats = $self->{stats};
98 281 ▶ if (defined($assertion)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $assertion) { } 281 0 281
99 281 ▶ if ($assertion == 1 or $assertion == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($assertion == 1 or $assertion == 0) { } 281 0 281
Condition66%2/3
$assertion == 1 or $assertion == 0
l!l&&r!l&&!r
262190
MC/DC0%0/2
$assertion == 1 or $assertion == 0
$assertion == 1 $assertion == 0
Truth table66%2/3
A or B
A= $assertion == 1B= $assertion == 0
ABresult
1X1
011
000
100 281 ▶ if ($assertion) {
Branch: 1 branch
BranchTrueFalseTotal
if ($assertion) { } 262 19 281
101 262 my $value = 1;
102 262 $stats->{stale} = $value;
103 262 $stats->{annualStatsStale} = $value;
104 262 $stats->{quarterlyStatsStale} = $value;
105 262 $stats->{monthlyStatsStale} = $value;
106 }
107 else {
108 19 $stats->{stale} = 0;
109 }
110 281 return 1;
111 }
112 else {
113 0 croak "Method 'stale' only accepts assertions in the form of 1 or 0 -- $assertion is not valid.";
114 }
115 }
116 else {
117 0 return $stats->{stale};
118 }
119 }
120  
121 3 ▶sub allowZeroPrice {
Subroutine allowZeroPrice: called CC=3 SCAR=11.0
Pod documented
122 3 my ($self, $allowZeroPrice) = @_;
123 3 ▶ if (defined($allowZeroPrice)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $allowZeroPrice) { } 1 2 3
124 1 ▶ $self->{allowZeroPrice} = $allowZeroPrice ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$allowZeroPrice ? : 0 1 1
125 1 return 1;
126 }
127 else {
128 2 return $self->{allowZeroPrice};
129 }
130 }
131  
132 949 ▶sub getSet {
Subroutine getSet: called CC=2 SCAR=6.9
Pod documented
133 949 my ($self, $hashKey) = @_;
134 949 ▶ if (exists($self->{sets}{$hashKey})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'sets'}{$hashKey}) { } 907 42 949
135 907 return $self->{sets}{$hashKey};
136 }
137 else {
138 42 return undef;
139 }
140 }
141  
142 463 ▶sub getSetFiltered {
Subroutine getSetFiltered: called CC=5 SCAR=16.2
Pod documented
143 463 my ($self, $hashKey) = @_;
144 463 ▶ if ($self->{skipStocks}{$hashKey}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'skipStocks'}{$hashKey}) { } 3 460 463
elsif (exists $self->{'sets'}{$hashKey}) { } 460 0 460
145 3 return undef;
146 }
147 elsif (exists($self->{sets}{$hashKey})) {
148 460 my $set = $self->{sets}{$hashKey};
149 460 ▶ if ($set->stale()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($set->stale) 431 29 460
150 431 $set->accountSales();
151 }
152 460 ▶ if ($set->realizationCount() > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($set->realizationCount > 0) { } 189 271 460
153 189 return $set;
154 }
155 else {
156 271 return undef;
157 }
158 }
159 else {
160 0 return undef;
161 }
162 }
163  
164 1 ▶sub getFilteredSets {
Subroutine getFilteredSets: called CC=3 SCAR=11.0
Pod documented
165 1 my $self = shift;
166 1 my @sets;
167 1 foreach my $hashKey (sort keys %{$self->{sets}}) {
168 26 my $set = $self->getSetFiltered($hashKey);
169 26 ▶ $set and push(@sets, $set);
Branch: 1 branch
BranchTrueFalseTotal
$set and push @sets, $set 19 7 26
170 }
171 1 return \@sets;
172 }
173  
174 257 ▶sub addToSet {
Subroutine addToSet: called CC=5 SCAR=16.9
Pod undocumented
175 257 my ($self, $at) = @_;
176 257 ▶ if (ref($at) and ref($at) eq 'Finance::StockAccount::AccountTransaction') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $at and ref $at eq 'Finance::StockAccount::AccountTransaction') { } 257 0 257
Condition33%1/3
ref $at and ref $at eq "Finance::StockAccount::AccountTransaction"
!ll&&!rl&&r
00257
MC/DC0%0/2
ref $at and ref $at eq "Finance::StockAccount::AccountTransaction"
ref $at ref $at eq "Finance::StockAccount::AccountTransaction"
Truth table33%1/3
A and B
A= ref $atB= ref $at eq "Finance::StockAccount::AccountTransaction"
ABresult
0X0
100
111
177 257 my $hashKey = $at->hashKey();
178 257 my $set = $self->getSet($hashKey);
179 257 my $status;
180 257 ▶ if ($set) {
Branch: 1 branch
BranchTrueFalseTotal
if ($set) { } 215 42 257
181 215 $status = $set->add([$at]);
182 }
183 else {
184 42 $set = Finance::StockAccount::Set->new([$at]);
185 42 $self->{sets}{$hashKey} = $set;
186 42 $status = $set;
187 }
188 257 ▶ $status and $self->stale(1);
Branch: 1 branch
BranchTrueFalseTotal
$status and $self->stale(1) 257 0 257
189 257 return $status;
190 }
191 else {
192 0 warn "Method addToSet requires a valid AccountTransaction object.\n";
193 0 return 0;
194 }
195 }
196  
197 1 ▶sub addAccountTransactions {
Subroutine addAccountTransactions: called CC=6 SCAR=20.9
Pod undocumented
198 1 my ($self, $accountTransactions) = @_;
199 1 ▶ if (ref($accountTransactions) and ref($accountTransactions) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $accountTransactions and ref $accountTransactions eq 'ARRAY') { } 1 0 1
Condition33%1/3
ref $accountTransactions and ref $accountTransactions eq "ARRAY"
!ll&&!rl&&r
001
MC/DC0%0/2
ref $accountTransactions and ref $accountTransactions eq "ARRAY"
ref $accountTransactions ref $accountTransactions eq "ARRAY"
Truth table33%1/3
A and B
A= ref $accountTransactionsB= ref $accountTransactions eq "ARRAY"
ABresult
0X0
100
111
200 1 my $added = 0;
201 1 foreach my $at (@$accountTransactions) {
202 1 ▶ $self->addToSet($at) and $added++;
Branch: 1 branch
BranchTrueFalseTotal
$self->addToSet($at) and ++$added 1 0 1
203 }
204 1 ▶ $added and $self->stale(1);
Branch: 1 branch
BranchTrueFalseTotal
$added and $self->stale(1) 1 0 1
205 1 return $added;
206 }
207 else {
208 0 warn "Method addAccountTransactions requiers a reference to an array of AccountTransaction objects.\n";
209 0 return 0;
210 }
211 }
212  
213 25 ▶sub stockTransaction {
Subroutine stockTransaction: called CC=20 SCAR=34.0
Pod documented
214 25 my ($self, $init) = @_;
215 25 my $helpString = q{ Please see 'perldoc Finance::StockAccount'.};
216 25 ▶ if (!(exists($init->{tm}) or exists($init->{dateString}))) {
Branch: 6 branches
BranchTrueFalseTotal
if (not exists $init->{'tm'} || exists $init->{'dateString'}) { } 1 24 25
elsif (not exists $init->{'stock'} || exists $init->{'symbol'}) { } 1 23 24
elsif (not exists $init->{'action'} && grep({$_ eq $init->{'action'};} 'buy', 'sell', 'short', 'cover')) { } 1 22 23
elsif (not exists $init->{'quantity'} && $init->{'quantity'} =~ /[0-9]/ && $init->{'quantity'} > 0) { } 1 21 22
elsif (not exists $init->{'price'} && $init->{'price'} =~ /[0-9]/) { } 1 20 21
elsif ($init->{'price'} <= 0 and not $self->{'allowZeroPrice'}) { } 2 18 20
Condition100%3/3
exists $init->{'tm'} || exists $init->{'dateString'}
l!l&&r!l&&!r
6181
MC/DC100%2/2
exists $init->{'tm'} || exists $init->{'dateString'}
exists $init->{'tm'} exists $init->{'dateString'}
Truth table100%3/3
A || B
A= exists $init->{'tm'}B= exists $init->{'dateString'}
ABresult
1X1
011
000
217 1 carp "A date is required to add a stock transaction, using either the 'tm' key or the 'dateString' key.$helpString";
218 1 return 0;
219 }
220 ▶ elsif (!(exists($init->{stock}) or exists($init->{symbol}))) {
Condition66%2/3
exists $init->{'stock'} || exists $init->{'symbol'}
l!l&&r!l&&!r
0231
MC/DC50%1/2
exists $init->{'stock'} || exists $init->{'symbol'}
exists $init->{'stock'} exists $init->{'symbol'}
Truth table66%2/3
A || B
A= exists $init->{'stock'}B= exists $init->{'symbol'}
ABresult
1X1
011
000
221 1 carp "A stock is required to add a transaction, using either the 'stock' key or the 'symbol' key.$helpString";
222 1 return 0;
223 }
224 88 ▶ elsif (!(exists($init->{action}) and (grep { $_ eq $init->{action} } qw(buy sell short cover)))) {
Condition66%2/3
exists $init->{'action'} && grep({$_ eq $init->{'action'};} 'buy', 'sell', 'short', 'cover')
!ll&&!rl&&r
1022
MC/DC50%1/2
exists $init->{'action'} && grep({$_ eq $init->{'action'};} 'buy', 'sell', 'short', 'cover')
exists $init->{'action'} grep({$_ eq $init->{'action'};} 'buy', 'sell', 'short', 'cover')
Truth table66%2/3
A && B
A= exists $init->{'action'}B= grep({$_ eq $init->{'action'};} 'buy', 'sell', 'short', 'cover')
ABresult
0X0
100
111
225 1 carp "To add a transaction, you must specify an action, one of 'buy', 'sell', 'short', or 'cover'.$helpString";
226 1 return 0;
227 }
228 ▶ elsif (!(exists($init->{quantity}) and $init->{quantity} =~ /[0-9]/ and $init->{quantity} > 0)) {
Condition66%4/6
exists $init->{'quantity'} && $init->{'quantity'} =~ /[0-9]/ && $init->{'quantity'} > 0
!ll&&!rl&&r
1021
exists $init->{"quantity"} and $init->{"quantity"} =~ /[0-9]/
!ll&&!rl&&r
1021
MC/DC33%1/3
exists $init->{'quantity'} && $init->{'quantity'} =~ /[0-9]/ && $init->{'quantity'} > 0
exists $init->{"quantity"} $init->{"quantity"} =~ /[0-9]/ $init->{'quantity'} > 0
Truth table50%2/4
A and B && C
A= exists $init->{"quantity"}B= $init->{"quantity"} =~ /[0-9]/C= $init->{'quantity'} > 0
ABCresult
0XX0
10X0
1100
1111
229 1 carp "To add a transaction, you must specify a numeric quantity, and it must be greater than zero.$helpString";
230 1 return 0;
231 }
232 ▶ elsif (!(exists($init->{price}) and $init->{price} =~ /[0-9]/)) {
Condition66%2/3
exists $init->{'price'} && $init->{'price'} =~ /[0-9]/
!ll&&!rl&&r
1020
MC/DC50%1/2
exists $init->{'price'} && $init->{'price'} =~ /[0-9]/
exists $init->{'price'} $init->{'price'} =~ /[0-9]/
Truth table66%2/3
A && B
A= exists $init->{'price'}B= $init->{'price'} =~ /[0-9]/
ABresult
0X0
100
111
233 1 carp "To add a transaction, you must specify a price, and it must be numeric.$helpString";
234 1 return 0;
235 }
236 ▶ elsif ($init->{price} <= 0 and !$self->{allowZeroPrice}) {
Condition100%3/3
$init->{"price"} <= 0 and not $self->{"allowZeroPrice"}
!ll&&!rl&&r
1712
MC/DC100%2/2
$init->{"price"} <= 0 and not $self->{"allowZeroPrice"}
$init->{"price"} <= 0 not $self->{"allowZeroPrice"}
Truth table100%3/3
A and not(B)
A= $init->{"price"} <= 0B= not $self->{"allowZeroPrice"}
ABresult
0X0
100
111
237 2 carp "To add a transaction, price must be greater than zero. By default, prices zero or lower are treated as suspect and not included in calculations.\nYou may override this default behavior by setting 'allowZeroPrice' to true.$helpString";
238 2 return 0;
239 }
240 else {
241 18 ▶ if (exists($init->{stock}) and (exists($init->{symbol}) or exists($init->{exchange}))) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $init->{"stock"} and exists $init->{"symbol"} || exists $init->{"exchange"}) 0 18 18
Condition16%1/6
exists $init->{"stock"} and exists $init->{"symbol"} || exists $init->{"exchange"}
!ll&&!rl&&r
1800
exists $init->{"symbol"} or exists $init->{"exchange"}
l!l&&r!l&&!r
000
MC/DC0%0/3
exists $init->{"stock"} and exists $init->{"symbol"} || exists $init->{"exchange"}
exists $init->{"stock"} exists $init->{"symbol"} exists $init->{"exchange"}
Truth table25%1/4
A and B or C
A= exists $init->{"stock"}B= exists $init->{"symbol"}C= exists $init->{"exchange"}
ABCresult
0XX0
1000
11X1
1011
242 0 carp "To add a transaction, you may specify a Finance::StockAccount::Stock object, or a symbol (optionally with an exchange).\nYou need not specify both. Proceeding on the assumption that the stock object is intentional and the symbol/exchange are accidental...";
243 0 ▶ exists($init->{symbol}) and delete($init->{symbol});
Branch: 1 branch
BranchTrueFalseTotal
exists $init->{"symbol"} and delete $init->{"symbol"} 0 0 0
244 0 ▶ exists($init->{exchange}) and delete($init->{exchange});
Branch: 1 branch
BranchTrueFalseTotal
exists $init->{"exchange"} and delete $init->{"exchange"} 0 0 0
245 }
246 18 my $at = Finance::StockAccount::AccountTransaction->new($init);
247 18 ▶ if ($at) {
Branch: 1 branch
BranchTrueFalseTotal
if ($at) { } 18 0 18
248 18 return $self->addToSet($at);
249 }
250 else {
251 0 carp "Unable to create AccountTransaction object with those parameters.\n";
252 0 return 0;
253 }
254 }
255 }
256  
257 5 ▶sub skipStocks {
Subroutine skipStocks: called CC=3 SCAR=11.0
Pod documented
258 5 my ($self, @skipStocks) = @_;
259 5 my $count = 0;
260 5 ▶ if (scalar(@skipStocks)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @skipStocks) { } 3 2 5
261 3 foreach my $stock (@skipStocks) {
262 4 $self->{skipStocks}{$stock} = 1;
263 }
264 3 $self->stale(1);
265 3 return 1;
266 }
267 else {
268 2 return sort keys %{$self->{skipStocks}};
269 }
270 }
271  
272 1 ▶sub resetSkipStocks {
Subroutine resetSkipStocks: called CC=2 SCAR=7.0
Pod documented
273 1 my $self = shift;
274 1 ▶ if (scalar(keys %{$self->{skipStocks}})) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$$self{"skipStocks"};}) 1 0 1
275 1 $self->{skipStocks} = {};
276 1 $self->stale(1);
277 }
278 1 return 1;
279 }
280  
281 35 ▶sub staleSets {
Subroutine staleSets: called CC=3 SCAR=11.0
Pod undocumented
282 35 my $self = shift;
283 35 my $stale = 0;
284 35 foreach my $hashKey (keys %{$self->{sets}}) {
285 327 my $set = $self->getSet($hashKey);
286 327 ▶ if ($set) {
Branch: 1 branch
BranchTrueFalseTotal
if ($set) 327 0 327
287 327 $stale += $set->stale();
288 }
289 }
290 35 return $stale;
291 }
292  
293 28 ▶sub calculateMaxCashInvested {
Subroutine calculateMaxCashInvested: called CC=4 SCAR=13.9
Pod undocumented
294 28 my ($self, $realizations) = @_;
295 28 my @allTransactions = sort { $a->tm() <=> $b->tm() } map { @{$_->acquisitions()}, $_->divestment() } @$realizations;
296 28 my $numberOfTrades = scalar(@allTransactions);
297 28 @$realizations = ();
298 28 my ($total, $max) = (0, 0);
299 28 ▶ for (my $x=0; $x<scalar(@allTransactions); $x++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < scalar @allTransactions) 1023 28 1051
300 1023 my $transaction = $allTransactions[$x];
301 1023 $total += 0 - $transaction->cashEffect();
302 1023 ▶ $total > $max and $max = $total;
Branch: 1 branch
BranchTrueFalseTotal
$total > $max and $max = $total 291 732 1023
303 }
304 28 @allTransactions = ();
305 28 ▶ if (wantarray()) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 28 28
306 0 return $max, $numberOfTrades;
307 }
308 else {
309 28 return $max;
310 }
311 }
312  
313 19 ▶sub calculateStats {
Subroutine calculateStats: called CC=14 SCAR=26.5
Pod undocumented
314 19 my $self = shift;
315 19 my ($totalOutlays, $totalRevenues, $profit, $commissions, $regulatoryFees, $otherFees, $numberExcluded, $transactionCount) = (0, 0, 0, 0, 0, 0, 0, 0);
316 19 my ($startDate, $endDate);
317 19 my $setCount = 0;
318 19 my @allRealizations = ();
319 19 my $stats = $self->getNewStatsHash();
320 19 $self->{stats} = $stats;
321 19 $self->stale(0);
322 19 foreach my $hashKey (keys %{$self->{sets}}) {
323 99 my $set = $self->getSetFiltered($hashKey);
324 99 ▶ if ($set) {
Branch: 1 branch
BranchTrueFalseTotal
if ($set) { } 72 27 99
325 72 ▶ if ($set->stale()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($set->stale) 0 72 72
326 0 $set->accountSales();
327 }
328 72 $numberExcluded += $set->unrealizedTransactionCount();
329 72 ▶ next unless $set->success();
Branch: 1 branch
BranchTrueFalseTotal
unless $set->success 0 72 72
330  
331 ### Simple Totals
332 72 $totalOutlays += $set->totalOutlays();
333 72 $totalRevenues += $set->totalRevenues();
334 72 $profit += $set->profit();
335 72 $commissions += $set->commissions();
336 72 $regulatoryFees += $set->regulatoryFees();
337 72 $otherFees += $set->otherFees();
338 72 $transactionCount += $set->transactionCount();
339  
340 ### Date-Aware Totals
341 72 push(@allRealizations, @{$set->realizations()});
342  
343 ### End-Date, Start-Date
344 72 my $setStart = $set->startDate();
345 72 ▶ $setStart or die "Didn't get set startDate for hashkey $hashKey\n";
Branch: 1 branch
BranchTrueFalseTotal
$setStart or die "Didn't get set startDate for hashkey $hashKey\n" 72 0 72
346 72 ▶ if (!defined($startDate)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $startDate) { } 15 57 72
elsif ($setStart < $startDate) { } 3 54 57
347 15 $startDate = $setStart;
348 }
349 elsif ($setStart < $startDate) {
350 3 $startDate = $setStart;
351 }
352 72 my $setEnd = $set->endDate();
353 72 ▶ if (!$endDate) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $endDate) { } 15 57 72
elsif ($setEnd > $endDate) { } 17 40 57
354 15 $endDate = $setEnd;
355 }
356 elsif ($setEnd > $endDate) {
357 17 $endDate = $setEnd;
358 }
359 72 $setCount++;
360 }
361 else {
362 27 my $unfilteredSet = $self->getSet($hashKey);
363 27 $numberExcluded += $unfilteredSet->unrealizedTransactionCount();
364 27 ▶ if ($self->{skipStocks}{$hashKey}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"skipStocks"}{$hashKey}) 3 24 27
365 3 $numberExcluded += $unfilteredSet->transactionCount();
366 }
367 }
368 }
369 19 $stats->{numberExcluded} = $numberExcluded;
370 19 ▶ if ($setCount > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($setCount > 0) { } 15 4 19
371 15 ▶ if ($totalOutlays) {
Branch: 1 branch
BranchTrueFalseTotal
if ($totalOutlays) { } 15 0 15
372 15 my $profitOverOutlays = $profit / $totalOutlays;
373 15 $stats->{totalOutlays} = $totalOutlays;
374 15 $stats->{totalRevenues} = $totalRevenues;
375 15 $stats->{profit} = $profit;
376 15 $stats->{commissions} = $commissions;
377 15 $stats->{regulatoryFees} = $regulatoryFees;
378 15 $stats->{otherFees} = $otherFees;
379 15 $stats->{profitOverOutlays} = $profitOverOutlays;
380 15 $stats->{startDate} = $startDate;
381 15 $stats->{endDate} = $endDate;
382 15 my $secondsInYear = 60 * 60 * 24 * 365.25;
383 15 my $secondsInAccount = $endDate->epoch() - $startDate->epoch();
384 15 ▶ if (!$secondsInAccount) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($secondsInAccount) 0 15 15
385 0 croak "No time passed in account? Can't calculate time-related stats.";
386 }
387 15 my $annualRatio = $secondsInYear / $secondsInAccount;
388 15 $stats->{annualRatio} = $annualRatio;
389 15 $stats->{profitOverYears} = $profit * $annualRatio;
390 15 my $maxCashInvested = $self->calculateMaxCashInvested(\@allRealizations);
391 15 $stats->{maxCashInvested} = $maxCashInvested;
392 15 $stats->{profitOverOutlays} = $profit / $totalOutlays;
393 15 my $pomci = $profit / $maxCashInvested;
394 15 $stats->{profitOverMaxCashInvested} = $pomci;
395 15 $stats->{pomciOverYears} = $pomci * $annualRatio;
396 15 $stats->{numberOfTrades} = $transactionCount;
397 15 return 1;
398 }
399 else {
400 0 carp "No totalOutlays found on which to compute stats.\n";
401 0 return 0;
402 }
403 }
404 else {
405 4 carp "No realized gains in stock account.\n";
406 4 return 0;
407 }
408 }
409  
410 52 ▶sub getStats {
Subroutine getStats: called CC=3 SCAR=11.0
Pod undocumented
411 52 my $self = shift;
412 52 ▶ if ($self->{stats}{stale} or $self->staleSets()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'stats'}{'stale'} or $self->staleSets) { } 19 33 52
Condition100%3/3
$self->{"stats"}{"stale"} or $self->staleSets
l!l&&r!l&&!r
17233
MC/DC100%2/2
$self->{"stats"}{"stale"} or $self->staleSets
$self->{"stats"}{"stale"} $self->staleSets
Truth table100%3/3
A or B
A= $self->{"stats"}{"stale"}B= $self->staleSets
ABresult
1X1
011
000
413 19 return $self->calculateStats();
414 }
415 else {
416 33 return 1;
417 }
418 }
419  
420 0 ▶sub stats {
Subroutine stats: not called CC=2 SCAR=17.9
Pod documented
421 0 my $self = shift;
422 0 $self->getStats();
423 0 my $stats = $self->{stats};
424 0 my @stats;
425 0 ▶ for (my $x=0; $x<scalar(@$statsLinesArray); $x+=3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < scalar @$statsLinesArray) 0 0 0
426 0 my $key = $statsLinesArray->[$x+1];
427 0 push(@stats, $key, $stats->{$key});
428 }
429 0 return \@stats;
430 }
431  
432 1 ▶sub statsString {
Subroutine statsString: called CC=2 SCAR=6.9
Pod documented
433 1 my $self = shift;
434 1 $self->getStats();
435 1 my $stats = $self->{stats};
436 1 my $statsString;
437 1 ▶ for (my $x=0; $x<scalar(@$statsLinesArray); $x+=3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < scalar @$statsLinesArray) 15 1 16
438 15 my ($name, $key, $valPattern) = @$statsLinesArray[$x .. $x+2];
439 15 $statsString .= sprintf("%30s $valPattern\n", $name, $stats->{$key});
440 }
441 1 return $statsString;
442 }
443  
444 0 ▶sub availableAcquisitionsString {
Subroutine availableAcquisitionsString: not called CC=4 SCAR=30.0
Pod documented
445 0 my ($self, $inputKey) = @_;
446 0 my ($string, @hashKeys);
447 0 ▶ if ($inputKey) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inputKey) { } 0 0 0
448 0 @hashKeys = ($inputKey);
449 }
450 else {
451 0 @hashKeys = keys %{$self->{sets}};
452 }
453 0 foreach my $hashKey (@hashKeys) {
454 0 my $set = $self->getSet($hashKey);
455 0 ▶ if ($set) {
Branch: 1 branch
BranchTrueFalseTotal
if ($set) 0 0 0
456 0 $string .= $set->availableAcquisitionsString();
457 }
458 }
459 0 return $string;
460 }
461  
462 13 ▶sub statsForPeriod {
Subroutine statsForPeriod: called CC=5 SCAR=16.3
Pod documented
463 13 my ($self, $tm1, $tm2) = @_;
464 13 my ($totalOutlays, $totalRevenues, $profit, $commissions, $regulatoryFees, $otherFees, $transactionCount) = (0, 0, 0, 0, 0, 0, 0);
465 13 my @allRealizations = ();
466 13 my $setCount = 0;
467 13 foreach my $hashKey (keys %{$self->{sets}}) {
468 338 my $unfilteredSet = $self->getSet($hashKey);
469 338 $unfilteredSet->setDateLimit($tm1, $tm2);
470 338 my $set = $self->getSetFiltered($hashKey);
471 338 ▶ if ($set) {
Branch: 1 branch
BranchTrueFalseTotal
if ($set) 98 240 338
472 98 $setCount++;
473 98 $totalOutlays += $set->totalOutlays();
474 98 $totalRevenues += $set->totalRevenues();
475 98 $profit += $set->profit();
476 98 $commissions += $set->commissions();
477 98 $regulatoryFees += $set->regulatoryFees();
478 98 $otherFees += $set->otherFees();
479 98 $transactionCount += $set->transactionCount();
480 98 push(@allRealizations, @{$set->realizations()});
481 }
482 338 $unfilteredSet->clearDateLimit();
483 }
484 13 ▶ if ($setCount > 0 and $totalOutlays) {
Branch: 1 branch
BranchTrueFalseTotal
if ($setCount > 0 and $totalOutlays) { } 13 0 13
Condition33%1/3
$setCount > 0 and $totalOutlays
!ll&&!rl&&r
0013
MC/DC0%0/2
$setCount > 0 and $totalOutlays
$setCount > 0 $totalOutlays
Truth table33%1/3
A and B
A= $setCount > 0B= $totalOutlays
ABresult
0X0
100
111
485 13 my $maxCashInvested = $self->calculateMaxCashInvested(\@allRealizations);
486 return {
487 13 totalOutlays => $totalOutlays,
488 totalRevenues => $totalRevenues,
489 maxCashInvested => $maxCashInvested,
490 profit => $profit,
491 profitOverOutlays => $profit / $totalOutlays,
492 profitOverMaxCashInvested => $profit / $maxCashInvested,
493 commissions => $commissions,
494 regulatoryFees => $regulatoryFees,
495 otherFees => $otherFees,
496 numberOfTrades => $transactionCount,
497 };
498 }
499 else {
500 0 carp "I couldn't calculate stats for the period from $tm1 to $tm2 for this stock account.\n";
501 0 return undef;
502 }
503 }
504  
505 1 ▶sub calculateAnnualStats {
Subroutine calculateAnnualStats: called CC=2 SCAR=6.9
Pod undocumented
506 1 my $self = shift;
507 1 $self->getStats();
508 1 my $stats = $self->{stats};
509 1 my $startDate = $stats->{startDate};
510 1 my $endDate = $stats->{endDate};
511 1 my $offset = $startDate->offset();
512 1 my $annualStats = [];
513 1 foreach my $year ($startDate->year() .. $endDate->year()) {
514 3 my $yearStart = Time::Moment->new(
515 year => $year,
516 month => 1,
517 day => 1,
518 hour => 0,
519 minute => 0,
520 second => 01,
521 nanosecond => 0,
522 offset => $offset,
523 );
524 3 my $yearEnd = Time::Moment->new(
525 year => $year,
526 month => 12,
527 day => 31,
528 hour => 23,
529 minute => 59,
530 second => 59,
531 nanosecond => 0,
532 offset => $offset,
533 );
534 3 my $yearStats = $self->statsForPeriod($yearStart, $yearEnd);
535 3 $yearStats->{year} = $year;
536 3 push(@$annualStats, $yearStats);
537 }
538 1 $stats->{annualStats} = $annualStats;
539 1 $stats->{annualStatsStale} = 0;
540 1 return $annualStats;
541 }
542  
543 1 ▶sub calculateQuarterlyStats {
Subroutine calculateQuarterlyStats: called CC=2 SCAR=6.9
Pod undocumented
544 1 my $self = shift;
545 1 $self->getStats();
546 1 my $quarterlyStats = [];
547 1 my $stats = $self->{stats};
548 1 my $startDate = $stats->{startDate};
549 1 my $endDate = $stats->{endDate};
550 1 my $offset = $startDate->offset();
551 1 my $startYear = $startDate->year();
552 1 my $startQuarter = ceil($startDate->month()/3);
553 1 my $currDate = Time::Moment->new(
554 year => $startYear,
555 month => ($startQuarter - 1) * 3 + 1,
556 day => 1,
557 hour => 0,
558 minute => 0,
559 second => 01,
560 nanosecond => 0,
561 offset => $offset,
562 );
563 1 ▶ while ($currDate < $endDate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($currDate < $endDate) 10 1 11
564 10 my $quarterEnd = $currDate->plus_months(3);
565 10 my $quarterStats = $self->statsForPeriod($currDate, $quarterEnd);
566 10 $quarterStats->{year} = $currDate->year();
567 10 $quarterStats->{quarter} = ceil($currDate->month()/3);
568 10 push(@$quarterlyStats, $quarterStats);
569 10 $currDate = $quarterEnd;
570 }
571 1 $stats->{quarterlyStats} = $quarterlyStats;
572 1 $stats->{quarterlyStatsStale} = 0;
573 1 return $quarterlyStats;
574 }
575  
576 0 ▶sub calculateMonthlyStats {
Subroutine calculateMonthlyStats: not called CC=2 SCAR=17.9
Pod undocumented
577 0 my $self = shift;
578 0 $self->getStats();
579 0 my $monthlyStats = [];
580 0 my $stats = $self->{stats};
581 0 my $startDate = $stats->{startDate};
582 0 my $endDate = $stats->{endDate};
583 0 my $offset = $startDate->offset();
584 0 my $startYear = $startDate->year();
585 0 my $startMonth = $startDate->month();
586 0 my $currDate = Time::Moment->new(
587 year => $startYear,
588 month => $startMonth,
589 day => 1,
590 hour => 0,
591 minute => 0,
592 second => 01,
593 nanosecond => 0,
594 offset => $offset,
595 );
596 0 ▶ while ($currDate < $endDate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($currDate < $endDate) 0 0 0
597 0 my $monthEnd = $currDate->plus_months(1);
598 0 my $monthStats = $self->statsForPeriod($currDate, $monthEnd);
599 0 $monthStats->{year} = $currDate->year();
600 0 $monthStats->{month} = $currDate->month();
601 0 push(@$monthlyStats, $monthStats);
602 0 $currDate = $monthEnd;
603 }
604 0 $stats->{monthlyStats} = $monthlyStats;
605 0 $stats->{monthlyStatsStale} = 0;
606 0 return $monthlyStats;
607 }
608  
609 1 ▶sub annualStats {
Subroutine annualStats: called CC=2 SCAR=7.4
Pod documented
610 1 my $self = shift;
611 1 my $stats = $self->{stats};
612 1 ▶ if ($stats->{annualStatsStale}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stats->{'annualStatsStale'}) { } 1 0 1
613 1 return $self->calculateAnnualStats();
614 }
615 else {
616 0 return $stats->{annualStats};
617 }
618 }
619  
620 1 ▶sub quarterlyStats {
Subroutine quarterlyStats: called CC=2 SCAR=7.4
Pod documented
621 1 my $self = shift;
622 1 my $stats = $self->{stats};
623 1 ▶ if ($stats->{quarterlyStatsStale}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stats->{'quarterlyStatsStale'}) { } 1 0 1
624 1 return $self->calculateQuarterlyStats();
625 }
626 else {
627 0 return $stats->{quarterlyStats};
628 }
629 }
630  
631 0 ▶sub monthlyStats {
Subroutine monthlyStats: not called CC=2 SCAR=17.9
Pod documented
632 0 my $self = shift;
633 0 my $stats = $self->{stats};
634 0 ▶ if ($stats->{monthlyStatsStale}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stats->{'monthlyStatsStale'}) { } 0 0 0
635 0 return $self->calculateMonthlyStats();
636 }
637 else {
638 0 return $stats->{monthlyStats};
639 }
640 }
641  
642 0 ▶sub periodicStatsString {
Subroutine periodicStatsString: not called CC=6 SCAR=37.4
Pod undocumented
643 0 my ($self, $params) = @_;
644 0 my $periodHeadings = $params->{periodHeadings};
645 0 my $periodHeadingsPattern = $params->{periodHeadingsPattern};
646 0 my $periodKeys = $params->{periodKeys};
647 0 my $periodValuesPattern = $params->{periodValuesPattern};
648 0 my $periodHeadingsWidth = $params->{periodHeadingsWidth};
649 0 my $statsArray = $params->{statsArray};
650 0 my $statsString = sprintf("$periodHeadingsPattern $statsKeyHeadingsPattern\n", @$periodHeadings, @$statsKeyHeadings);
651 0 my $lineLength = length($statsString);
652 0 my $pattern = "$periodValuesPattern $statsKeysPattern\n";
653 0 my $totals;
654 0 my $verbose = $self->{verbose};
655 0 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 0 0
656 0 $totals = [map { 0 } (0 .. scalar(@$statsKeys) - 1)];
657 }
658 0 foreach my $period (@$statsArray) {
659 0 my @row = map { $period->{$_} } @$periodKeys;
660 0 ▶ for (my $x=0; $x<scalar(@$statsKeys); $x++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < scalar @$statsKeys) 0 0 0
661 0 my $key = $statsKeys->[$x];
662 0 my $value = $period->{$key};
663 0 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 0 0
664 0 $totals->[$x] += $value;
665 }
666 0 push(@row, $value);
667 }
668 0 $statsString .= sprintf($pattern, @row);
669 }
670 0 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 0 0
671 0 $statsString .= '-'x$lineLength . "\n"
672 . sprintf("%-${periodHeadingsWidth}s $statsKeysPattern\n", 'COL SUMS', @$totals);
673 }
674 $statsString .= '-'x$lineLength . "\n"
675 0 . sprintf("%-${periodHeadingsWidth}s $statsKeysPattern\n", 'ACCT TOTAL', map { $self->{stats}{$_} } @$statsKeys);
676 0 return $statsString;
677 }
678  
679 0 ▶sub annualStatsString {
Subroutine annualStatsString: not called CC=1 SCAR=6.9
Pod documented
680 0 my $self = shift;
681 0 return $self->periodicStatsString({
682 periodHeadings => [qw(Year)],
683 periodHeadingsPattern => "%10s",
684 periodKeys => [qw(year)],
685 periodValuesPattern => "%10d",
686 periodHeadingsWidth => 10,
687 statsArray => $self->annualStats(),
688 });
689 }
690  
691 0 ▶sub quarterlyStatsString {
Subroutine quarterlyStatsString: not called CC=1 SCAR=6.9
Pod documented
692 0 my $self = shift;
693 0 return $self->periodicStatsString({
694 periodHeadings => [qw(Year Quarter)],
695 periodHeadingsPattern => "%4s %7s",
696 periodKeys => [qw(year quarter)],
697 periodValuesPattern => "%4d %7d",
698 periodHeadingsWidth => 12,
699 statsArray => $self->quarterlyStats(),
700 });
701 }
702  
703 0 ▶sub monthlyStatsString {
Subroutine monthlyStatsString: not called CC=1 SCAR=6.9
Pod documented
704 0 my $self = shift;
705 0 return $self->periodicStatsString({
706 periodHeadings => [qw(Year Month)],
707 periodHeadingsPattern => "%4s %5s",
708 periodKeys => [qw(year month)],
709 periodValuesPattern => "%4d %5d",
710 periodHeadingsWidth => 10,
711 statsArray => $self->monthlyStats(),
712 });
713 }
714  
715 14 ▶sub profit {
Subroutine profit: called CC=1 SCAR=0.0
Pod documented
716 14 my $self = shift;
717 14 $self->getStats();
718 14 return $self->{stats}{profit};
719 }
720  
721 4 ▶sub maxCashInvested {
Subroutine maxCashInvested: called CC=1 SCAR=0.0
Pod documented
722 4 my $self = shift;
723 4 $self->getStats();
724 4 return $self->{stats}{maxCashInvested};
725 }
726  
727 3 ▶sub profitOverOutlays {
Subroutine profitOverOutlays: called CC=1 SCAR=0.0
Pod documented
728 3 my $self = shift;
729 3 $self->getStats();
730 3 return $self->{stats}{profitOverOutlays};
731 }
732  
733 3 ▶sub profitOverMaxCashInvested {
Subroutine profitOverMaxCashInvested: called CC=1 SCAR=0.0
Pod undocumented
734 3 my $self = shift;
735 3 $self->getStats();
736 3 return $self->{stats}{profitOverMaxCashInvested};
737 }
738  
739 3 ▶sub profitOverYears {
Subroutine profitOverYears: called CC=1 SCAR=0.0
Pod documented
740 3 my $self = shift;
741 3 $self->getStats();
742 3 return $self->{stats}{profitOverYears};
743 }
744  
745 2 ▶sub profitOverMaxCashInvestedOverYears {
Subroutine profitOverMaxCashInvestedOverYears: called CC=1 SCAR=0.0
Pod undocumented
746 2 my $self = shift;
747 2 $self->getStats();
748 2 return $self->{stats}{pomciOverYears};
749 }
750  
751 5 ▶sub commissions {
Subroutine commissions: called CC=1 SCAR=0.0
Pod documented
752 5 my $self = shift;
753 5 $self->getStats();
754 5 return $self->{stats}{commissions};
755 }
756  
757 0 ▶sub totalCommissions {
Subroutine totalCommissions: not called CC=1 SCAR=6.9
Pod documented
758 0 return shift->commissions();
759 }
760  
761 3 ▶sub regulatoryFees {
Subroutine regulatoryFees: called CC=1 SCAR=0.0
Pod documented
762 3 my $self = shift;
763 3 $self->getStats();
764 3 return $self->{stats}{regulatoryFees};
765 }
766  
767 1 ▶sub otherFees {
Subroutine otherFees: called CC=1 SCAR=0.0
Pod documented
768 1 my $self = shift;
769 1 $self->getStats();
770 1 return $self->{stats}{otherFees};
771 }
772  
773 5 ▶sub numberOfTrades {
Subroutine numberOfTrades: called CC=1 SCAR=0.0
Pod undocumented
774 5 my $self = shift;
775 5 $self->getStats();
776 5 return $self->{stats}{numberOfTrades};
777 }
778  
779 6 ▶sub numberExcluded {
Subroutine numberExcluded: called CC=1 SCAR=0.0
Pod undocumented
780 6 my $self = shift;
781 6 $self->getStats();
782 6 return $self->{stats}{numberExcluded};
783 }
784  
785 0 ▶sub totalFees {
Subroutine totalFees: not called CC=1 SCAR=6.9
Pod undocumented
786 0 my $self = shift;
787 0 return $self->regulatoryFees() + $self->otherFees();
788 }
789  
790 0 ▶sub summaryByStock {
Subroutine summaryByStock: not called CC=2 SCAR=17.9
Pod documented
791 0 my $self = shift;
792 0 my $spacer = Finance::StockAccount::Set->oneLinerSpacer();
793 0 my $string = $spacer . Finance::StockAccount::Set->oneLinerHeader() . $spacer;
794 0 my $sets = $self->getFilteredSets();
795 0 foreach my $set (@$sets) {
796 0 $string .= $set->oneLiner() . $spacer;
797 }
798 0 return $string;
799 }
800  
801  
802 1 ▶sub realizationsString {
Subroutine realizationsString: called CC=2 SCAR=6.9
Pod documented
803 1 my $self = shift;
804 1 my $string = Finance::StockAccount::Realization->headerString();
805 1 my $sets = $self->getFilteredSets();
806 1 foreach my $set (@$sets) {
807 19 $string .= $set->realizationsString();
808 }
809 1 return $string;
810 }
811  
812  
813 5 1;
814  
815 __END__
« StockAccount/Import.pm ↑ Summary StockAccount/Import/OptionsXpress.pm »