| 1 | package Finance::GeniusTrader::Calculator; | ||
| 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 Finance::GeniusTrader::CacheValues; | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Finance::GeniusTrader::Calculator - All data (of a single share) used for calculations | ||
| 12 | |||
| 13 | =head1 DESCRIPTION | ||
| 14 | |||
| 15 | This is a facility object to ease the collaboration between Finance::GeniusTrader::Prices | ||
| 16 | and Finance::GeniusTrader::CacheValues. It contains the prices (Finance::GeniusTrader::Prices), | ||
| 17 | and the result of various indicators and signals within two Finance::GeniusTrader::CacheValues | ||
| 18 | object. This object is manipulated by all the indicators, signals and | ||
| 19 | systems. | ||
| 20 | |||
| 21 | A calculator can contain the same serie of prices but indexed on different | ||
| 22 | time frames. | ||
| 23 | |||
| 24 | =over | ||
| 25 | |||
| 26 | =item C<< my $c = Finance::GeniusTrader::Calculator->new($prices [, $code]) >> | ||
| 27 | |||
| 28 | Create a new Finance::GeniusTrader::Calculator object with $prices used for | ||
| 29 | calculations. The calculator is associated to share named $code. | ||
| 30 | |||
| 31 | =item C<< $c->prices() >> | ||
| 32 | |||
| 33 | =item C<< $c->indicators() >> | ||
| 34 | |||
| 35 | =item C<< $c->signals() >> | ||
| 36 | |||
| 37 | Return the corresponding object of the current timeframe. | ||
| 38 | |||
| 39 | =item C<< $c->prices_on_timeframe($timeframe) >> | ||
| 40 | |||
| 41 | =item C<< $c->indicators_on_timeframe($timeframe) >> | ||
| 42 | |||
| 43 | =item C<< $c->signals_on_timeframe($timeframe) >> | ||
| 44 | |||
| 45 | Return the corresponding object of the indicated timeframe. Learn | ||
| 46 | more about the timeframes in Finance::GeniusTrader::DateTime. | ||
| 47 | |||
| 48 | =item C<< $c->set_code($code) >> | ||
| 49 | |||
| 50 | Sets the code of the share which datas are stored in this object. | ||
| 51 | |||
| 52 | =item C<< $c->code() >> | ||
| 53 | |||
| 54 | Returns the code of the share associated with this object. | ||
| 55 | |||
| 56 | =cut | ||
| 57 | sub new { | ||
| 58 | my $type = shift; | ||
| 59 | my $class = ref($type) || $type; | ||
| 60 | my $prices = shift; | ||
| 61 | my $code = shift || ""; | ||
| 62 | |||
| 63 | my $self = { 'code' => $code, 'tf' => {} }; | ||
| 64 | |||
| 65 | $self->{'tf'}{$prices->timeframe}{'prices'} = $prices; | ||
| 66 | $self->{'tf'}{$prices->timeframe}{'indics'} = Finance::GeniusTrader::CacheValues->new; | ||
| 67 | $self->{'tf'}{$prices->timeframe}{'signals'} = Finance::GeniusTrader::CacheValues->new; | ||
| 68 | |||
| 69 | bless $self, $class; | ||
| 70 | |||
| 71 | $self->set_current_timeframe($prices->timeframe); | ||
| 72 | |||
| 73 | return $self; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub set_code { | ||
| 77 | my ($self, $code) = @_; | ||
| 78 | $self->{'code'} = $code; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub code { $_[0]->{'code'} } | ||
| 82 | |||
| 83 | sub prices { $_[0]->{'_prices'} } | ||
| 84 | sub indicators { $_[0]->{'_indics'} } | ||
| 85 | sub signals { $_[0]->{'_signals'} } | ||
| 86 | |||
| 87 | sub prices_on_timeframe { $_[0]->{'tf'}{$_[1]}{'prices'} } | ||
| 88 | sub indicators_on_timeframe { $_[0]->{'tf'}{$_[1]}{'indics'} } | ||
| 89 | sub signals_on_timeframe { $_[0]->{'tf'}{$_[1]}{'signals'} } | ||
| 90 | |||
| 91 | =item C<< $calc->set_current_timeframe($timeframe) >> | ||
| 92 | |||
| 93 | Changes the current timeframe to the indicated one. If the timeframe | ||
| 94 | doesn't exist, it tries to create it. Returns 1 on success and 0 on | ||
| 95 | failure. | ||
| 96 | |||
| 97 | =item C<< $calc->current_timeframe() >> | ||
| 98 | |||
| 99 | Returns the current timeframe. | ||
| 100 | |||
| 101 | =cut | ||
| 102 | sub set_current_timeframe { | ||
| 103 | my ($self, $tf) = @_; | ||
| 104 | if (exists $self->{'tf'}{$tf}) | ||
| 105 | { | ||
| 106 | $self->{'_prices'} = $self->{'tf'}{$tf}{'prices'}; | ||
| 107 | $self->{'_indics'} = $self->{'tf'}{$tf}{'indics'}; | ||
| 108 | $self->{'_signals'} = $self->{'tf'}{$tf}{'signals'}; | ||
| 109 | return 1; | ||
| 110 | } else { | ||
| 111 | if ($self->create_timeframe($tf)) | ||
| 112 | { | ||
| 113 | $self->{'_prices'} = $self->{'tf'}{$tf}{'prices'}; | ||
| 114 | $self->{'_indics'} = $self->{'tf'}{$tf}{'indics'}; | ||
| 115 | $self->{'_signals'} = $self->{'tf'}{$tf}{'signals'}; | ||
| 116 | return 1; | ||
| 117 | } | ||
| 118 | return 0; | ||
| 119 | } | ||
| 120 | } | ||
| 121 | sub current_timeframe { $_[0]->prices->timeframe } | ||
| 122 | |||
| 123 | =item C<< $calc->create_timeframe($timeframe) >> | ||
| 124 | |||
| 125 | Create the given timeframe with an other serie of prices available in the | ||
| 126 | calculator. Returns 1 on success and 0 on failure. | ||
| 127 | |||
| 128 | =cut | ||
| 129 | sub create_timeframe { | ||
| 130 | my ($self, $tf) = @_; | ||
| 131 | # Sort the timeframe from the lowest to the biggest | ||
| 132 | # So the first one is the lowest that can build all the bigger | ||
| 133 | my @tfs = $self->available_timeframe; | ||
| 134 | # Only if possible | ||
| 135 | if ($self->{'tf'}{$tfs[0]}{'prices'}->timeframe < $tf) | ||
| 136 | { | ||
| 137 | my $new = $self->{'tf'}{$tfs[0]}{'prices'}->convert_to_timeframe($tf); | ||
| 138 | $self->{'tf'}{$tf}{'prices'} = $new; | ||
| 139 | $self->{'tf'}{$tf}{'indics'} = Finance::GeniusTrader::CacheValues->new; | ||
| 140 | $self->{'tf'}{$tf}{'signals'} = Finance::GeniusTrader::CacheValues->new; | ||
| 141 | |||
| 142 | # If we udpated the current timeframe, then let's sync it | ||
| 143 | if ($self->current_timeframe == $tf) | ||
| 144 | { | ||
| 145 | $self->set_current_timeframe($tf); | ||
| 146 | } | ||
| 147 | return 1; | ||
| 148 | } | ||
| 149 | return 0; | ||
| 150 | } | ||
| 151 | |||
| 152 | =item C<< $calc->available_timeframe() >> | ||
| 153 | |||
| 154 | Returns the sorted list of available timeframes. | ||
| 155 | |||
| 156 | =cut | ||
| 157 | sub available_timeframe { | ||
| 158 | my ($self) = @_; | ||
| 159 | return sort { $a <=> $b } keys %{$self->{'tf'}}; | ||
| 160 | } | ||
| 161 | |||
| 162 | =item C<< $calc->timeframe_is_available() >> | ||
| 163 | |||
| 164 | Returns true if the given timeframe is available in the calculator. | ||
| 165 | Otherwise returns false. | ||
| 166 | |||
| 167 | =cut | ||
| 168 | sub timeframe_is_available { | ||
| 169 | my ($self, $tf) = @_; | ||
| 170 | if (exists $self->{'tf'}{$tf}) | ||
| 171 | { | ||
| 172 | return 1; | ||
| 173 | } | ||
| 174 | return 0; | ||
| 175 | } | ||
| 176 | |||
| 177 | =pod | ||
| 178 | |||
| 179 | =back | ||
| 180 | |||
| 181 | =cut | ||
| 182 | 1; |