File Coverage

blib/lib/Finance/Quant/TA.pm
Criterion Covered Total %
statement 6 57 10.5
branch 0 8 0.0
condition 0 3 0.0
subroutine 2 7 28.5
pod 0 5 0.0
total 8 80 10.0


line stmt bran cond sub pod time code
1             #!/usr/bin/perl -W
2             package Finance::Quant::TA;
3 1     1   31200 use strict;
  1         2  
  1         75  
4 1     1   7 use warnings;
  1         1  
  1         671  
5              
6             require Exporter;
7              
8             our @ISA = qw(Exporter);
9              
10             # Items to export into callers namespace by default. Note: do not export
11             # names by default without a very good reason. Use EXPORT_OK instead.
12             # Do not simply export all your public functions/methods/constants.
13              
14             # This allows declaration use Finance::Quant::TA ':all';
15             # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
16             # will save memory.
17             our %EXPORT_TAGS = ( 'all' => [ qw(
18            
19             ) ] );
20              
21             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
22              
23             our @EXPORT = qw(
24            
25             );
26              
27             our $VERSION = '0.01';
28              
29              
30             sub logs {
31 0     0 0   my $s = shift;
32 0           return [ map {log} @{$s}[0..$#{$s}] ];
  0            
  0            
  0            
33             }
34            
35             sub diff {
36 0     0 0   my ($series, $lag) = @_;
37 0           my @diff = map {undef} 1..$lag;
  0            
38 0           push @diff, $series->[$_] - $series->[$_-$lag] for ( $lag..$#{$series} );
  0            
39 0           return \@diff;
40             }
41            
42             sub ma {
43 0     0 0   my ($series, $lag) = @_;
44 0           my @ma = map {undef} 1..$lag;
  0            
45 0 0         for(@{$series}){unless($_){push @ma,undef}else{last}}
  0            
  0            
  0            
  0            
46 0           my $sum = 0;
47 0           for my $i ($#ma..$#{$series}) {
  0            
48 0           $sum += $series->[$i-$_] for (0..($lag-1));
49 0           push @ma, $sum/($lag);
50 0           $sum = 0;
51             }
52 0           return \@ma;
53             }
54            
55             sub stdev {
56 0     0 0   my ($series, $lag) = @_;
57 0           my @stdev = map {undef} 1..$lag;
  0            
58 0 0         for(@{$series}){unless($_){push @stdev,undef}else{last}}
  0            
  0            
  0            
  0            
59 0           my ($sum, $sum2) = (0, 0);
60 0           for my $i ($#stdev..$#{$series}) {
  0            
61 0           for (0..($lag-1)) {
62 0           $sum2 += ($series->[$i-$_])**2;
63 0           $sum += $series->[$i-$_] ;
64             }
65 0           push @stdev, ($sum2/$lag - ($sum/$lag)**2)**0.5;
66 0           ($sum, $sum2) = (0, 0);
67             }
68 0           return \@stdev;
69             }
70              
71             sub nstdev_ma{
72 0     0 0   my ($sd, $ma, $n) = @_;
73 0           my $ans=[[],[]];
74 0           for (0..$#{$sd}) {
  0            
75 0   0       my $yn = defined $sd->[$_] && defined $ma->[$_];
76 0 0         $ans->[0][$_] = $yn ? $ma->[$_] + $n*($sd->[$_]) : undef;
77 0 0         $ans->[1][$_] = $yn ? $ma->[$_] - $n*($sd->[$_]) : undef;
78             }
79 0           return $ans;
80             }
81              
82              
83              
84              
85              
86              
87             1;
88             __END__