GeniusTrader/Graphics/DataSource/GenericIndicatorResults.pm untested

filter: Statementstmt 0.0% 0 / 98 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 136 CC 10 SCAR 24.1
CC
10
Cov
0%
get
20.0
update_value_range
20.0
new
12.0
CRAP 11.1
1 package Finance::GeniusTrader::Graphics::DataSource::GenericIndicatorResults;
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);
9  
10 @ISA = qw(Finance::GeniusTrader::Graphics::DataSource);
11  
12 use Finance::GeniusTrader::Eval;
13 use Finance::GeniusTrader::CacheValues;
14 use Finance::GeniusTrader::Graphics::DataSource;
15 use Finance::GeniusTrader::Tools qw(:math);
16 use Finance::GeniusTrader::Tools qw(extract_object_number);
17  
18 =head1 Finance::GeniusTrader::Graphics::DataSource::GenericIndicatorResults
19  
20 This datasource is a generic module to handle any information provided by
21 an indicator.
22  
23 =head2 Details
24  
25 This module will return either a serie of single data or a serie of array.
26  
27 If the input arguments contains a string like "Indicators::BOL/99", we
28 will assume that the user wants to have all data available form the
29 Bollinger indicator and we will return an array with all the data.
30  
31 If the input arguments contains a string like "Indicators::BOL" (It's the
32 same than "Indicators::BOL/0"), "Indicators::BOL/1" (or / any number), we
33 will assume that the user only wants a serie of single data, which is in
34 our example the third serie (keep in mind that the first serie start at
35 zero).
36  
37 We will either use only a single data serie or a all data available for
38 the calculation of the value range.
39  
40 =head2 Finance::GeniusTrader::Graphics::DataSource::GenericIndicatorResults->new($calc, $indicator_desc)
41  
42 Create a new indicator data source.
43  
44 =cut
45  
46 sub new {
47 my $type = shift;
48 my $class = ref($type) || $type;
49 my ($calc, $desc) = @_;
50
51 my @function = split(/\s+/, $desc);
52 my $indicator = create_standard_object(@function);
53 my $number = extract_object_number(@function);
54 if ($function[0] !~ m#/#) {
55 $number = 0;
56 }
57 if ($function[0] =~ m#/99#) {
58 $number = -1;
59 }
60
61 my $self = { 'calc' => $calc, 'indicator' => $indicator,
62 'number' => $number };
63 bless $self, $class;
64  
65 my $first = $indicator->days_required - 1;
66 my $last = $calc->prices->count - 1;
67
68 $self->set_available_range($first, $last);
69 $self->set_selected_range($self->get_available_range());
70
71 $indicator->calculate_interval($calc, $first, $last);
72  
73 return $self;
74 }
75  
76 sub is_available {
77 my ($self, $index) = @_;
78 my $calc = $self->{'calc'};
79 my $indicator = $self->{'indicator'};
80 my $number = $self->{'number'};
81 my $name = ($number < 0) ? $indicator->get_name : $indicator->get_name($number);
82
83 return $calc->indicators->is_available($name, $index);
84 }
85  
86 sub get {
87 my ($self, $index) = @_;
88 my $calc = $self->{'calc'};
89 my $indicator = $self->{'indicator'};
90 my $number = $self->{'number'};
91 my @results;
92
93 if ($number < 0) {
94 for(my $n = 0; $n < $indicator->get_nb_values; $n++) {
95 my $name = $indicator->get_name($n);
96 if ($calc->indicators->is_available($name, $index)) {
97 $results[$n] = $calc->indicators->get($name, $index);
98 }
99 }
100 return @results;
101 } else {
102 my $name = $indicator->get_name($number);
103 if ($calc->indicators->is_available($name, $index)) {
104 return $calc->indicators->get($name, $index);
105 }
106 }
107 }
108  
109 sub update_value_range {
110 my ($self) = @_;
111 my $calc = $self->{'calc'};
112 my $indicator = $self->{'indicator'};
113 my $number = $self->{'number'};
114 my ($start, $end) = $self->get_selected_range();
115 my ($min, $max);
116
117 if ($number < 0) {
118  
119 my $name = $indicator->get_name;
120 $min = $calc->indicators->get($name, $start);
121 $max = $calc->indicators->get($name, $start);
122  
123 for(my $i = $start; $i <= $end; $i++) {
124 for(my $n = 0; $n < $indicator->get_nb_values; $n++) {
125 $name = $indicator->get_name($n);
126 if ($calc->indicators->is_available($name, $i)) {
127 $min = min($calc->indicators->get($name, $i), $min);
128 $max = max($calc->indicators->get($name, $i), $max);
129 }
130 }
131 }
132 } else {
133
134 my $name = $indicator->get_name($number);
135 $min = $calc->indicators->get($name, $start);
136 $max = $calc->indicators->get($name, $start);
137  
138 for(my $i = $start; $i <= $end; $i++) {
139 if ($calc->indicators->is_available($name, $i)) {
140 $min = min($calc->indicators->get($name, $i), $min);
141 $max = max($calc->indicators->get($name, $i), $max);
142 }
143 }
144 }
145 $self->set_min_value($min);
146 $self->set_max_value($max);
147 }
148  
149 1;