| 1 | package Finance::GeniusTrader::CacheValues; | ||
| 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 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Finance::GeniusTrader::CacheValues - Cache the computed values (of indic/signals) for a single share | ||
| 12 | |||
| 13 | =head1 DESCRIPTION | ||
| 14 | |||
| 15 | This object is designed to be associated with a Finance::GeniusTrader::Prices object. | ||
| 16 | It may contain the computed value of some indicators corresponding | ||
| 17 | to the Finance::GeniusTrader::Prices object. | ||
| 18 | |||
| 19 | =over | ||
| 20 | |||
| 21 | =item C<< my $cache = Finance::GeniusTrader::CacheValues->new; >> | ||
| 22 | |||
| 23 | Create a new Finance::GeniusTrader::CacheValues that will contain computed values of | ||
| 24 | some indicators or signals. | ||
| 25 | |||
| 26 | =cut | ||
| 27 | sub new { | ||
| 28 | my $type = shift; | ||
| 29 | my $class = ref($type) || $type; | ||
| 30 | my $self = { 'values' => {} }; | ||
| 31 | |||
| 32 | return bless $self, $class; | ||
| 33 | } | ||
| 34 | |||
| 35 | =item C<< $cache->get($name, $i) >> | ||
| 36 | |||
| 37 | Return the value of the indicator $name for the day $i. | ||
| 38 | |||
| 39 | =cut | ||
| 40 | sub get { | ||
| 41 | my ($self, $name, $i) = @_; | ||
| 42 | |||
| 43 | if (defined($i)) { | ||
| 44 | return $self->{'values'}{$name}[$i]; | ||
| 45 | } else { | ||
| 46 | return $self->{'values'}{$name}; | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | =item C<< $cache->set($name, $i, $value) >> | ||
| 51 | |||
| 52 | Store the computed value $value of indicator $name for the day $i. | ||
| 53 | |||
| 54 | =cut | ||
| 55 | sub set { | ||
| 56 | my ($self, $name, $i, $val) = @_; | ||
| 57 | |||
| 58 | $self->{'values'}{$name}[$i] = $val; | ||
| 59 | } | ||
| 60 | |||
| 61 | =item C<< $cache->is_available($name, $i) >> | ||
| 62 | |||
| 63 | =item C<< $cache->is_available_interval($name, $first, $last) >> | ||
| 64 | |||
| 65 | Check if the value of indicator $name is available for day $i. | ||
| 66 | |||
| 67 | =cut | ||
| 68 | sub is_available { | ||
| 69 | my ($self, $name, $i) = @_; | ||
| 70 | |||
| 71 | return defined($self->{'values'}{$name}[$i]) ? 1 : 0; | ||
| 72 | } | ||
| 73 | sub is_available_interval { | ||
| 74 | my ($self, $name, $first, $last) = @_; | ||
| 75 | |||
| 76 | for (my $i = $first; $i <= $last; $i++) | ||
| 77 | { | ||
| 78 | if (! defined($self->{'values'}{$name}[$i])) | ||
| 79 | { | ||
| 80 | return 0; | ||
| 81 | } | ||
| 82 | } | ||
| 83 | return 1; | ||
| 84 | } | ||
| 85 | |||
| 86 | =pod | ||
| 87 | |||
| 88 | =back | ||
| 89 | |||
| 90 | =cut | ||
| 91 | 1; |