GeniusTrader/Indicators.pm untested

filter: Statementstmt 0.0% 0 / 86 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 126 CC 6 SCAR 21.7
CC
6
Cov
0%
new
30.0
calculate_interval
6.0
get_registered_object
2.0
CRAP 8.7
1 package Finance::GeniusTrader::Indicators;
2  
3 # Copyright 2000-2002 Raphaƫl Hertzog, Fabien Fulhaber
4 # This file is distributed under the terms of the General Public License
5 # version 2 or (at your option) any later version.
6  
7 use strict;
8 use vars qw(@ISA @EXPORT $GET_FIRST $GET_OPEN $GET_HIGH $GET_LOW $GET_LAST
9 $GET_CLOSE $GET_VOLUME %OBJECT_REPOSITORY);
10  
11 require Exporter;
12 @ISA = qw(Exporter Finance::GeniusTrader::Dependency);
13 @EXPORT = qw($GET_FIRST $GET_OPEN $GET_HIGH $GET_LOW $GET_LAST $GET_CLOSE
14 $GET_VOLUME &build_object_name &manage_object);
15  
16 use Finance::GeniusTrader::Prices;
17 use Finance::GeniusTrader::Calculator;
18 use Finance::GeniusTrader::Registry;
19 use Finance::GeniusTrader::Dependency;
20 use Finance::GeniusTrader::ArgsTree;
21 #ALL# use Log::Log4perl qw(:easy);
22  
23 =head1 NAME
24  
25 Finance::GeniusTrader::Indicators - Provides some functions that will be used by all indicators.
26  
27 =head1 DESCRIPTION
28  
29 =head2 GENERIC EXPORTED FUNCTIONS
30  
31 =over
32  
33 =item C<< build_object_name($encoded, [ @args ], $key) >>
34  
35 Generate the name of an indicator based on its "encoded" name.
36  
37 =back
38  
39 =head2 FUNCTIONS TO RETRIEVE USUAL PRICES
40  
41 $GET_OPEN, $GET_FIRST, $GET_HIGH, $GET_LOW, $GET_LAST, $GET_CLOSE
42 and $GET_VOLUME are functions references that can be passed as arguments
43 to some indicators where input functions are expected. They are
44 automatically exported when doing "use Finance::GeniusTrader::Indicators;".
45  
46 For example, the indicator AMA (Arithmetic Moving Average) can be used
47 to calculate the average of anything (other indicators or prices). You
48 can provide those functions if you want to calculate an average of
49 some prices (or quotations or volumes).
50  
51 =cut
52 $GET_FIRST = $GET_OPEN = sub { $_[0]->prices()->at($_[1])->[$FIRST] };
53 $GET_HIGH = sub { $_[0]->prices()->at($_[1])->[$HIGH] };
54 $GET_LOW = sub { $_[0]->prices()->at($_[1])->[$LOW] };
55 $GET_LAST = $GET_CLOSE = sub { $_[0]->prices()->at($_[1])->[$LAST] };
56 $GET_VOLUME = sub { $_[0]->prices()->at($_[1])->[$VOLUME] };
57  
58 =head2 MANAGE A REPOSITORY OF INDICATORS
59  
60 Finance::GeniusTrader::Indicators::get_registered_object($name);
61 Finance::GeniusTrader::Indicators::register_object($name, $object);
62 Finance::GeniusTrader::Indicators::get_or_register_object($name, $object);
63 Finance::GeniusTrader::Indicators::manage_object(\@NAMES, $object, $class, $args, $key);
64  
65 =cut
66 sub get_registered_object {
67 Finance::GeniusTrader::Registry::get_registered_object(\%OBJECT_REPOSITORY, @_);
68 }
69 sub register_object {
70 Finance::GeniusTrader::Registry::register_object(\%OBJECT_REPOSITORY, @_);
71 }
72 sub get_or_register_object {
73 Finance::GeniusTrader::Registry::get_or_register_object(\%OBJECT_REPOSITORY, @_);
74 }
75 sub manage_object {
76 Finance::GeniusTrader::Registry::manage_object(\%OBJECT_REPOSITORY, @_);
77 }
78  
79 =head2 DEFAULT FUNCTIONS FOR INDICATORS
80  
81 =over
82  
83 =item C<< Finance::GeniusTrader::Indicators::Module->new($args, $key, $func) >>
84  
85 Create a new indicator with the given arguments. $key and $func are optional,
86 they are useful for indicators which can use non-usual input streams.
87  
88 =cut
89 sub new {
90 my ($type, $args, $key, $func) = @_;
91 my $class = ref($type) || $type;
92  
93 no strict "refs";
94
95 my $self = { };
96 if (defined($args)) {
97 if ( $#{$args} < $#{"$class\::DEFAULT_ARGS"} ) {
98 for (my $n=($#{$args}+1); $n<=$#{"$class\::DEFAULT_ARGS"}; $n++) {
99 push @{$args}, ${"$class\::DEFAULT_ARGS"}[$n];
100 }
101 }
102 $self->{'args'} = Finance::GeniusTrader::ArgsTree->new(@{$args});
103 } elsif (defined (@{"$class\::DEFAULT_ARGS"})) {
104 $self->{'args'} = Finance::GeniusTrader::ArgsTree->new(@{"$class\::DEFAULT_ARGS"});
105 } else {
106 $self->{'args'} = Finance::GeniusTrader::ArgsTree->new(); # no args
107 }
108  
109 if (defined($func)) {
110 # User supplied input
111 #$self->{'func'} = $func;
112 die "We tried to pass a 'func' parameter to an indicator, please convert the module...";
113 } #else {
114 # We make the supposition that the last parameter is going to be
115 # an input parameter ...
116 #$self->{'func'} = sub { $self->{'args'}->get_arg_values($_[0], $_[1],
117 # $self->{'args'}->get_nb_args() ); };
118 #}
119 $self->{'func'} = sub { die "Please convert this module to NOT use \$self->{'func'} ..."; };
120
121 return manage_object(\@{"$class\::NAMES"}, $self, $class, $self->{'args'}, $key);
122 }
123  
124 =item C<< $indic->calculate_all($calc) >>
125  
126 calculate_all will calculate all the values of the indicators for all
127 possibles days.
128  
129 =cut
130 sub calculate_all {
131 my ($self, $calc) = @_;
132 my $c = $calc->prices->count;
133 my $indic = $calc->indicators;
134  
135 $self->calculate_interval($calc, 0, $c - 1);
136 return;
137 }
138  
139 =item C<< $indic->calculate_interval($calc, $first, $last) >>
140  
141 Provide a default non-optimized version of calculate_interval that
142 calls calculate once for each day.
143  
144 Real indicators are encouraged to override this function to provide an
145 optimized version of the calculation algorithm by possibly reusing
146 the result of previous days.
147  
148 =cut
149 sub calculate_interval {
150 my ($self, $calc, $first, $last) = @_;
151  
152 if (ref($self->{'args'}) =~ /Finance::GeniusTrader::ArgsTree/) {
153 $self->{'args'}->prepare_interval($calc, $first, $last);
154 }
155 for (my $i = $first; $i <= $last; $i++)
156 {
157 $self->calculate($calc, $i);
158 }
159 return;
160 }
161  
162 =item C<< $indic->initialize() >>
163  
164 Default method that does nothing.
165  
166 =cut
167 sub initialize { 1; }
168  
169 =item C<< $indic->get_name >>
170  
171 =item C<< $indic->get_name($n) >>
172  
173 Get the name of the indicator. If the indicator returns several values,
174 you can get the name corresponding to any value, you just have to
175 precise in the parameters the index of the value that you're interested
176 in.
177  
178 =item C<< $indic->get_nb_values >>
179  
180 Return the number of different values produced by this indicator that are
181 available for use.
182  
183 =back
184  
185 =cut
186 # Those functions are exported by Finance::GeniusTrader::Registry
187  
188 1;