| 1 | package Finance::GeniusTrader::Graphics::Object::Candle; | ||
| 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 | # ras hack based on trunk version dated 30may05 | ||
| 8 | # $Id$ | ||
| 9 | |||
| 10 | use strict; | ||
| 11 | use vars qw(@ISA); | ||
| 12 | @ISA = qw(Finance::GeniusTrader::Graphics::Object); | ||
| 13 | |||
| 14 | use Finance::GeniusTrader::Prices; | ||
| 15 | use Finance::GeniusTrader::Graphics::Object; | ||
| 16 | use Finance::GeniusTrader::Graphics::Driver; | ||
| 17 | use Finance::GeniusTrader::Graphics::Tools qw(:color); | ||
| 18 | use Finance::GeniusTrader::Conf; | ||
| 19 | |||
| 20 | Finance::GeniusTrader::Conf::default("Graphic::Candle::UpColor", "green"); | ||
| 21 | Finance::GeniusTrader::Conf::default("Graphic::Candle::DownColor", "red"); | ||
| 22 | Finance::GeniusTrader::Conf::default("Graphic::Candle::Width", 6); | ||
| 23 | |||
| 24 | =head1 Finance::GeniusTrader::Graphics::Object::Candle | ||
| 25 | |||
| 26 | This graphical object display a series of candlesticks. | ||
| 27 | |||
| 28 | =cut | ||
| 29 | |||
| 30 | sub init { | ||
| 31 | my ($self) = @_; | ||
| 32 | |||
| 33 | # Default values ... | ||
| 34 | $self->{'up_body_color'} = | ||
| 35 | get_color(Finance::GeniusTrader::Conf::get("Graphic::Candle::UpColor")); | ||
| 36 | $self->{'up_body_border_color'} = | ||
| 37 | get_color(Finance::GeniusTrader::Conf::get_first( | ||
| 38 | "Graphic::Candle::UpBorderColor", | ||
| 39 | "Graphic::Candle::BorderColor", | ||
| 40 | "Graphic::ForegroundColor" | ||
| 41 | )); | ||
| 42 | $self->{'up_shadows_color'} = | ||
| 43 | get_color(Finance::GeniusTrader::Conf::get_first( | ||
| 44 | "Graphic::Candle::UpShadowColor", | ||
| 45 | "Graphic::Candle::ShadowColor", | ||
| 46 | "Graphic::Candle::UpBorderColor", | ||
| 47 | "Graphic::Candle::BorderColor", | ||
| 48 | "Graphic::ForegroundColor" | ||
| 49 | )); | ||
| 50 | |||
| 51 | $self->{'down_body_color'} = | ||
| 52 | get_color(Finance::GeniusTrader::Conf::get("Graphic::Candle::DownColor")); | ||
| 53 | $self->{'down_body_border_color'} = | ||
| 54 | get_color(Finance::GeniusTrader::Conf::get_first( | ||
| 55 | "Graphic::Candle::DownBorderColor", | ||
| 56 | "Graphic::Candle::BorderColor", | ||
| 57 | "Graphic::ForegroundColor" | ||
| 58 | )); | ||
| 59 | $self->{'down_shadows_color'} = | ||
| 60 | get_color(Finance::GeniusTrader::Conf::get_first( | ||
| 61 | "Graphic::Candle::DowbShadowColor", | ||
| 62 | "Graphic::Candle::ShadowColor", | ||
| 63 | "Graphic::Candle::DownBorderColor", | ||
| 64 | "Graphic::Candle::BorderColor", | ||
| 65 | "Graphic::ForegroundColor" | ||
| 66 | )); | ||
| 67 | |||
| 68 | $self->{'width'} = Finance::GeniusTrader::Conf::get("Graphic::Candle::Width"); | ||
| 69 | } | ||
| 70 | |||
| 71 | sub display { | ||
| 72 | my ($self, $driver, $picture) = @_; | ||
| 73 | my $scale = $self->get_scale(); | ||
| 74 | my $zone = $self->{'zone'}; | ||
| 75 | my $width = $self->{'width'}; | ||
| 76 | my ($start, $end) = $self->{'source'}->get_selected_range(); | ||
| 77 | my $space = $scale->convert_to_x_coordinate($start + 1) - | ||
| 78 | $scale->convert_to_x_coordinate($start); | ||
| 79 | my $offset = int($space/2) - int($width/2); | ||
| 80 | $offset = 0 if $offset < 1; | ||
| 81 | |||
| 82 | # only $y_min and $y_max are significant | ||
| 83 | my ($x_min, $y_min) = $scale->get_value_from_coordinate($start, 0); | ||
| 84 | my ($x_max, $y_max) = $scale->get_value_from_coordinate($end, $zone->height-1); | ||
| 85 | |||
| 86 | # these are coordinate values of $y_min and $y_max | ||
| 87 | my $yc_min = $scale->convert_to_y_coordinate($y_min); | ||
| 88 | my $yc_max = $scale->convert_to_y_coordinate($y_max); | ||
| 89 | |||
| 90 | for(my $i = $start; $i <= $end; $i++) | ||
| 91 | { | ||
| 92 | my $data = $self->{'source'}->get($i); | ||
| 93 | my $low = $scale->convert_to_y_coordinate($data->[$LOW]); | ||
| 94 | my $open = $scale->convert_to_y_coordinate($data->[$OPEN]); | ||
| 95 | my $close = $scale->convert_to_y_coordinate($data->[$CLOSE]); | ||
| 96 | my $high = $scale->convert_to_y_coordinate($data->[$HIGH]); | ||
| 97 | my $x = $scale->convert_to_x_coordinate($i); | ||
| 98 | $x += $offset; | ||
| 99 | |||
| 100 | # clip $y at top of zone | ||
| 101 | $low = $yc_max if ( $low > $yc_max ); | ||
| 102 | $open = $yc_max if ( $open > $yc_max ); | ||
| 103 | $close = $yc_max if ( $close > $yc_max ); | ||
| 104 | $high = $yc_max if ( $high > $yc_max ); | ||
| 105 | # clip $y at bottom of zone | ||
| 106 | $low = $yc_min if ( $low < $yc_min ); | ||
| 107 | $open = $yc_min if ( $open < $yc_min ); | ||
| 108 | $close = $yc_min if ( $close < $yc_min ); | ||
| 109 | $high = $yc_min if ( $high < $yc_min ); | ||
| 110 | |||
| 111 | if ($data->[$OPEN] < $data->[$CLOSE]) { | ||
| 112 | |||
| 113 | $driver->filled_rectangle($picture, | ||
| 114 | $zone->absolute_coordinate($x + 1, $open + 1), | ||
| 115 | $zone->absolute_coordinate($x + $width - 2 - 1, $close - 1), | ||
| 116 | $self->{'up_body_color'}); | ||
| 117 | |||
| 118 | $driver->rectangle($picture, | ||
| 119 | $zone->absolute_coordinate($x, $open), | ||
| 120 | $zone->absolute_coordinate($x + $width - 2, $close), | ||
| 121 | $self->{'up_body_border_color'}); | ||
| 122 | |||
| 123 | $driver->line($picture, | ||
| 124 | $zone->absolute_coordinate($x + int($width / 2) - 1, $close + 1), | ||
| 125 | $zone->absolute_coordinate($x + int($width / 2) - 1, $high), $self->{'up_shadows_color'}); | ||
| 126 | |||
| 127 | $driver->line($picture, | ||
| 128 | $zone->absolute_coordinate($x + int($width / 2) - 1, $low), | ||
| 129 | $zone->absolute_coordinate($x + int($width / 2) - 1, $open - 1), $self->{'up_shadows_color'}); | ||
| 130 | |||
| 131 | } else { | ||
| 132 | |||
| 133 | $driver->filled_rectangle($picture, | ||
| 134 | $zone->absolute_coordinate($x + 1, $close + 1), | ||
| 135 | $zone->absolute_coordinate($x + $width - 2 - 1, $open - 1), $self->{'down_body_color'}); | ||
| 136 | |||
| 137 | $driver->rectangle($picture, | ||
| 138 | $zone->absolute_coordinate($x, $close), | ||
| 139 | $zone->absolute_coordinate($x + $width - 2, $open), $self->{'down_body_border_color'}); | ||
| 140 | |||
| 141 | $driver->line($picture, | ||
| 142 | $zone->absolute_coordinate($x + int($width / 2) - 1, $open + 1), | ||
| 143 | $zone->absolute_coordinate($x + int($width / 2) - 1, $high), $self->{'down_shadows_color'}); | ||
| 144 | |||
| 145 | $driver->line($picture, | ||
| 146 | $zone->absolute_coordinate($x + int($width / 2) - 1, $low), | ||
| 147 | $zone->absolute_coordinate($x + int($width / 2) - 1, $close - 1), $self->{'down_shadows_color'}); | ||
| 148 | } | ||
| 149 | } | ||
| 150 | } | ||
| 151 | |||
| 152 | 1; |