| 1 | package Finance::GeniusTrader::Graphics::DataSource::SingleIndicator; | ||
| 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::SingleIndicator | ||
| 19 | |||
| 20 | This datasource is a generic module to handle any information provided by an | ||
| 21 | indicator. | ||
| 22 | |||
| 23 | We will return a serie of data based on a single generic indicator name, like | ||
| 24 | "Indicators::BOL/2". | ||
| 25 | |||
| 26 | =cut | ||
| 27 | |||
| 28 | sub new { | ||
| 29 | my $type = shift; | ||
| 30 | my $class = ref($type) || $type; | ||
| 31 | my ($calc, $desc) = @_; | ||
| 32 | |||
| 33 | my @function = split(/\s+/, $desc); | ||
| 34 | my $indicator = create_standard_object(@function); | ||
| 35 | my $number = extract_object_number(@function); | ||
| 36 | |||
| 37 | my $self = { 'calc' => $calc, 'indicator' => $indicator, | ||
| 38 | 'number' => $number, 'calculated' => 1 }; | ||
| 39 | bless $self, $class; | ||
| 40 | |||
| 41 | my $first = $indicator->days_required - 1; | ||
| 42 | my $last = $calc->prices->count - 1; | ||
| 43 | |||
| 44 | $self->set_available_range($first, $last); | ||
| 45 | $self->set_selected_range($self->get_available_range()); | ||
| 46 | $self->{'calculated'} = 0; | ||
| 47 | |||
| 48 | return $self; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub calculate { | ||
| 52 | my ($self, $force) = @_; | ||
| 53 | $force = 0 if (! defined($force)); | ||
| 54 | if ($force && !$self->{'calculated'}) { | ||
| 55 | $self->{'indicator'}->calculate_interval($self->{'calc'}, $self->get_selected_range()); | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | sub set_selected_range { | ||
| 60 | my ($self, $start, $end) = @_; | ||
| 61 | |||
| 62 | $self->{'selected_start'} = $start; | ||
| 63 | $self->{'selected_end'} = $end; | ||
| 64 | |||
| 65 | $self->calculate(1); | ||
| 66 | $self->update_value_range(); | ||
| 67 | } | ||
| 68 | |||
| 69 | sub is_available { | ||
| 70 | my ($self, $index) = @_; | ||
| 71 | my $calc = $self->{'calc'}; | ||
| 72 | my $indicator = $self->{'indicator'}; | ||
| 73 | my $number = $self->{'number'}; | ||
| 74 | my $name = $indicator->get_name($number); | ||
| 75 | |||
| 76 | $self->calculate(); | ||
| 77 | |||
| 78 | return $calc->indicators->is_available($name, $index); | ||
| 79 | } | ||
| 80 | |||
| 81 | sub get { | ||
| 82 | my ($self, $index) = @_; | ||
| 83 | my $calc = $self->{'calc'}; | ||
| 84 | my $indicator = $self->{'indicator'}; | ||
| 85 | my $number = $self->{'number'}; | ||
| 86 | |||
| 87 | my $name = $indicator->get_name($number); | ||
| 88 | if ($calc->indicators->is_available($name, $index)) { | ||
| 89 | return $calc->indicators->get($name, $index); | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | sub update_value_range { | ||
| 94 | my ($self) = @_; | ||
| 95 | my $calc = $self->{'calc'}; | ||
| 96 | my $indicator = $self->{'indicator'}; | ||
| 97 | my $number = $self->{'number'}; | ||
| 98 | my ($start, $end) = $self->get_selected_range(); | ||
| 99 | my ($min, $max); | ||
| 100 | |||
| 101 | my $name = $indicator->get_name($number); | ||
| 102 | |||
| 103 | for(my $i = $start; $i <= $end; $i++) { | ||
| 104 | if ($calc->indicators->is_available($name, $i)) { | ||
| 105 | $min = $calc->indicators->get($name, $i) if (! defined($min)); | ||
| 106 | $max = $calc->indicators->get($name, $i) if (! defined($max)); | ||
| 107 | $min = min($calc->indicators->get($name, $i), $min); | ||
| 108 | $max = max($calc->indicators->get($name, $i), $max); | ||
| 109 | } | ||
| 110 | } | ||
| 111 | $self->set_min_value($min); | ||
| 112 | $self->set_max_value($max); | ||
| 113 | } | ||
| 114 | |||
| 115 | 1; |