| 1 | package Finance::GeniusTrader::Graphics::DataSource::PricesColor; | ||
| 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::Prices; | ||
| 13 | use Finance::GeniusTrader::Graphics::DataSource; | ||
| 14 | use Finance::GeniusTrader::Graphics::Driver; | ||
| 15 | use Finance::GeniusTrader::Tools qw(:math); | ||
| 16 | use Finance::GeniusTrader::Graphics::Tools qw(:color); | ||
| 17 | use Finance::GeniusTrader::Conf; | ||
| 18 | |||
| 19 | Finance::GeniusTrader::Conf::default("Graphic::PricesColor::Up", "green"); | ||
| 20 | Finance::GeniusTrader::Conf::default("Graphic::PricesColor::Down", "red"); | ||
| 21 | Finance::GeniusTrader::Conf::default("Graphic::PricesColor::Equal", "dark grey"); | ||
| 22 | |||
| 23 | =head1 Finance::GeniusTrader::Graphics::DataSource::PricesColor | ||
| 24 | |||
| 25 | This datasource provides a color depending on the prices movement. | ||
| 26 | Green when up, red when down, black when equal. | ||
| 27 | |||
| 28 | It uses a Finance::GeniusTrader::Prices object as a basis. | ||
| 29 | |||
| 30 | =head2 Finance::GeniusTrader::Prices::DataSource::PricesColor->new($prices) | ||
| 31 | |||
| 32 | Create a new prices color data source. | ||
| 33 | |||
| 34 | =cut | ||
| 35 | sub new { | ||
| 36 | my $type = shift; | ||
| 37 | my $class = ref($type) || $type; | ||
| 38 | my $prices = shift; | ||
| 39 | |||
| 40 | my $self = { "prices" => $prices, | ||
| 41 | "color" => [ | ||
| 42 | get_color(Finance::GeniusTrader::Conf::get("Graphic::PricesColor::Equal")), | ||
| 43 | get_color(Finance::GeniusTrader::Conf::get("Graphic::PricesColor::Down")), | ||
| 44 | get_color(Finance::GeniusTrader::Conf::get("Graphic::PricesColor::Up")) | ||
| 45 | ] | ||
| 46 | }; | ||
| 47 | |||
| 48 | bless $self, $class; | ||
| 49 | |||
| 50 | $self->set_available_range(0, $prices->count() - 1); | ||
| 51 | $self->set_selected_range($self->get_available_range()); | ||
| 52 | |||
| 53 | return $self; | ||
| 54 | } | ||
| 55 | |||
| 56 | =head2 $pc->set_{up,down,unchanged}_color($color) | ||
| 57 | |||
| 58 | Change the color returned for the up/down/unchanged days. | ||
| 59 | |||
| 60 | =cut | ||
| 61 | sub set_unchanged_color { $_[0]->{'color'}[0] = get_color($_[1]) } | ||
| 62 | sub set_down_color { $_[0]->{'color'}[1] = get_color($_[1]) } | ||
| 63 | sub set_up_color { $_[0]->{'color'}[2] = get_color($_[1]) } | ||
| 64 | |||
| 65 | |||
| 66 | sub is_available { | ||
| 67 | |||
| 68 | my ($self, $index) = @_; | ||
| 69 | if (($index >= 0) && ($index < $self->{'prices'}->count())) | ||
| 70 | { | ||
| 71 | return 1; | ||
| 72 | } | ||
| 73 | return 0; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub get { | ||
| 77 | my ($self, $index) = @_; | ||
| 78 | my $type = 0; | ||
| 79 | if ($index >= 1) { | ||
| 80 | if ($self->{'prices'}->at($index)->[$LAST] eq $self->{'prices'}->at($index - 1)->[$LAST]) { | ||
| 81 | $type = 0; | ||
| 82 | } | ||
| 83 | if ($self->{'prices'}->at($index)->[$LAST] < $self->{'prices'}->at($index - 1)->[$LAST]) { | ||
| 84 | $type = 1; | ||
| 85 | } | ||
| 86 | if ($self->{'prices'}->at($index)->[$LAST] > $self->{'prices'}->at($index - 1)->[$LAST]) { | ||
| 87 | $type = 2; | ||
| 88 | } | ||
| 89 | } else { | ||
| 90 | if ($self->{'prices'}->at($index)->[$CLOSE] eq $self->{'prices'}->at($index)->[$OPEN]) { | ||
| 91 | $type = 0; | ||
| 92 | } | ||
| 93 | if ($self->{'prices'}->at($index)->[$CLOSE] < $self->{'prices'}->at($index)->[$OPEN]) { | ||
| 94 | $type = 1; | ||
| 95 | } | ||
| 96 | if ($self->{'prices'}->at($index)->[$CLOSE] > $self->{'prices'}->at($index)->[$OPEN]) { | ||
| 97 | $type = 2; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | return $self->{'color'}[$type]; | ||
| 102 | } | ||
| 103 | |||
| 104 | sub update_value_range { | ||
| 105 | my ($self) = @_; | ||
| 106 | } | ||
| 107 | |||
| 108 | 1; |