line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Math::Business::EMA; |
2
|
|
|
|
|
|
|
|
3
|
5
|
|
|
5
|
|
7050
|
use strict; |
|
5
|
|
|
|
|
16
|
|
|
5
|
|
|
|
|
95
|
|
4
|
5
|
|
|
5
|
|
15
|
use warnings; |
|
5
|
|
|
|
|
8
|
|
|
5
|
|
|
|
|
93
|
|
5
|
5
|
|
|
5
|
|
14
|
use Carp; |
|
5
|
|
|
|
|
8
|
|
|
5
|
|
|
|
|
1535
|
|
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
|
17
|
|
|
17
|
0
|
326
|
my $class = shift; |
15
|
17
|
|
|
|
|
38
|
my $this = bless { |
16
|
|
|
|
|
|
|
EMA => undef, |
17
|
|
|
|
|
|
|
R => 0, |
18
|
|
|
|
|
|
|
R1 => 0, |
19
|
|
|
|
|
|
|
}, $class; |
20
|
|
|
|
|
|
|
|
21
|
17
|
|
|
|
|
22
|
my $days = shift; |
22
|
17
|
100
|
|
|
|
29
|
if( defined $days ) { |
23
|
2
|
|
|
|
|
6
|
$this->set_days( $days ); |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
17
|
|
|
|
|
64
|
return $this; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub set_days { |
30
|
17
|
|
|
17
|
0
|
20
|
my $this = shift; |
31
|
17
|
|
|
|
|
20
|
my $arg = int(shift); |
32
|
|
|
|
|
|
|
|
33
|
17
|
50
|
|
|
|
25
|
croak "days must be a positive non-zero integer" if $arg <= 0; |
34
|
|
|
|
|
|
|
|
35
|
17
|
|
|
|
|
38
|
$this->{R} = 2.0 / (1.0 + $arg); |
36
|
17
|
|
|
|
|
25
|
$this->{R1} = (1 - $this->{R}); |
37
|
17
|
|
|
|
|
22
|
$this->{days} = $arg; |
38
|
|
|
|
|
|
|
|
39
|
17
|
|
|
|
|
37
|
$this->{tag} = "EMA($this->{days})"; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub insert { |
43
|
6614
|
|
|
6614
|
0
|
5742
|
my $this = shift; |
44
|
|
|
|
|
|
|
|
45
|
6614
|
50
|
|
|
|
7629
|
croak "You must set the number of days before you try to insert" if not $this->{R}; |
46
|
|
|
|
|
|
|
|
47
|
6614
|
|
|
|
|
8031
|
while( defined(my $Yt = shift) ) { |
48
|
6619
|
100
|
|
|
|
7470
|
if( defined(my $e = $this->{EMA}) ) { |
49
|
6406
|
|
|
|
|
11637
|
$this->{EMA} = ( $this->{R} * $Yt ) + ( $this->{R1} * $e ); |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
} else { |
52
|
213
|
|
|
|
|
215
|
my ($p,$N); |
53
|
213
|
100
|
100
|
|
|
451
|
if( ref($p = $this->{_p}) and (($N = @$p) >= $this->{days}-1) ) { |
54
|
15
|
|
|
|
|
18
|
my $sum = 0; |
55
|
15
|
|
|
|
|
48
|
$sum += $_ for @$p; |
56
|
|
|
|
|
|
|
|
57
|
15
|
|
|
|
|
31
|
$this->{EMA} = ( $this->{R} * $Yt ) + ( $this->{R1} * ($sum/$N) ); |
58
|
15
|
|
|
|
|
84
|
delete $this->{_p}; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
} else { |
61
|
198
|
|
|
|
|
178
|
push @{$this->{_p}}, $Yt; |
|
198
|
|
|
|
|
458
|
|
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub query { |
68
|
6537
|
|
|
6537
|
0
|
5737
|
my $this = shift; |
69
|
|
|
|
|
|
|
|
70
|
6537
|
|
|
|
|
9011
|
return $this->{EMA}; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
__END__ |