line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Math::Business::SMA; |
2
|
|
|
|
|
|
|
|
3
|
6
|
|
|
6
|
|
5668
|
use strict; |
|
6
|
|
|
|
|
13
|
|
|
6
|
|
|
|
|
189
|
|
4
|
6
|
|
|
6
|
|
112
|
use warnings; |
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
146
|
|
5
|
6
|
|
|
6
|
|
27
|
use Carp; |
|
6
|
|
|
|
|
28
|
|
|
6
|
|
|
|
|
3604
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
1; |
8
|
|
|
|
|
|
|
|
9
|
0
|
|
|
0
|
0
|
0
|
sub tag { (shift)->{tag} } |
10
|
|
|
|
|
|
|
|
11
|
0
|
|
|
0
|
0
|
0
|
sub recommended { croak "no recommendation" } |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub new { |
14
|
14
|
|
|
14
|
0
|
485
|
my $class = shift; |
15
|
14
|
|
|
|
|
79
|
my $this = bless { |
16
|
|
|
|
|
|
|
val => [], |
17
|
|
|
|
|
|
|
cur => undef, |
18
|
|
|
|
|
|
|
rec => 1, |
19
|
|
|
|
|
|
|
}, $class; |
20
|
|
|
|
|
|
|
|
21
|
14
|
|
|
|
|
25
|
my $days = shift; |
22
|
14
|
100
|
|
|
|
41
|
if( defined $days ) { |
23
|
11
|
|
|
|
|
27
|
$this->set_days( $days ); |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
14
|
|
|
|
|
50
|
return $this; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub set_days { |
30
|
15
|
|
|
15
|
0
|
24
|
my $this = shift; |
31
|
15
|
|
|
|
|
22
|
my $arg = int(shift); |
32
|
|
|
|
|
|
|
|
33
|
15
|
50
|
|
|
|
36
|
croak "days must be a positive non-zero integer" if $arg <= 0; |
34
|
|
|
|
|
|
|
|
35
|
15
|
|
|
|
|
46
|
$this->{val} = []; |
36
|
15
|
|
|
|
|
29
|
delete $this->{SMA}; |
37
|
15
|
|
|
|
|
24
|
$this->{days} = $arg; |
38
|
15
|
|
|
|
|
61
|
$this->{tag} = "SMA($this->{days})"; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub insert { |
42
|
226
|
|
|
226
|
0
|
442
|
my $this = shift; |
43
|
226
|
|
|
|
|
313
|
my $val = $this->{val}; |
44
|
226
|
|
|
|
|
330
|
my $N = $this->{days}; |
45
|
|
|
|
|
|
|
|
46
|
226
|
50
|
|
|
|
418
|
croak "You must set the number of days before you try to insert" if not defined $N; |
47
|
|
|
|
|
|
|
|
48
|
226
|
|
|
|
|
806
|
while( defined(my $PM = shift) ) { |
49
|
227
|
|
|
|
|
4915
|
push @$val, $PM; |
50
|
|
|
|
|
|
|
|
51
|
227
|
100
|
|
|
|
782
|
if( @$val >= $N ) { |
52
|
147
|
100
|
|
|
|
715
|
if( defined( my $s = $this->{SMA} ) ) { |
53
|
133
|
|
|
|
|
170
|
my $old = shift @$val; |
54
|
133
|
|
|
|
|
691
|
$this->{SMA} = $s - $old/$N + $PM/$N; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
} else { |
57
|
14
|
|
|
|
|
23
|
my $sum = 0; |
58
|
14
|
|
|
|
|
77
|
$sum += $_ for @$val; |
59
|
|
|
|
|
|
|
|
60
|
14
|
|
|
|
|
177
|
$this->{SMA} = $sum/$N; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub query { |
67
|
272
|
|
|
272
|
0
|
630
|
my $this = shift; |
68
|
|
|
|
|
|
|
|
69
|
272
|
|
|
|
|
915
|
return $this->{SMA}; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
__END__ |