GeniusTrader/Graphics/Object/CandleVolume.pm untested

filter: Statementstmt 0.0% 0 / 80 Branchbran 0.0% 0 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 109 CC 10 SCAR 41.3
CC
10
Cov
0%
display
110.0
init
2.0
BEGIN
2.0
CRAP 62.0
1 package Finance::GeniusTrader::Graphics::Object::CandleVolume;
2  
3 # Copyright 2003 Oliver Bossert
4 # Copyright 2000-2002 Raphaƫl Hertzog, Fabien Fulhaber
5 # This file is distributed under the terms of the General Public License
6 # version 2 or (at your option) any later version.
7  
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::CandleVolume
25  
26 This graphical object display a series of candlesticks.
27 The width of each candle is determined by the volume.
28  
29 =cut
30  
31 sub init {
32 my ($self) = @_;
33
34 # Default values ...
35 $self->{'up_body_color'} =
36 get_color(Finance::GeniusTrader::Conf::get("Graphic::Candle::UpColor"));
37 $self->{'up_body_border_color'} =
38 get_color(Finance::GeniusTrader::Conf::get_first(
39 "Graphic::Candle::UpBorderColor",
40 "Graphic::Candle::BorderColor",
41 "Graphic::ForegroundColor"
42 ));
43 $self->{'up_shadows_color'} =
44 get_color(Finance::GeniusTrader::Conf::get_first(
45 "Graphic::Candle::UpShadowColor",
46 "Graphic::Candle::ShadowColor",
47 "Graphic::Candle::UpBorderColor",
48 "Graphic::Candle::BorderColor",
49 "Graphic::ForegroundColor"
50 ));
51
52 $self->{'down_body_color'} =
53 get_color(Finance::GeniusTrader::Conf::get("Graphic::Candle::DownColor"));
54 $self->{'down_body_border_color'} =
55 get_color(Finance::GeniusTrader::Conf::get_first(
56 "Graphic::Candle::DownBorderColor",
57 "Graphic::Candle::BorderColor",
58 "Graphic::ForegroundColor"
59 ));
60 $self->{'down_shadows_color'} =
61 get_color(Finance::GeniusTrader::Conf::get_first(
62 "Graphic::Candle::DowbShadowColor",
63 "Graphic::Candle::ShadowColor",
64 "Graphic::Candle::DownBorderColor",
65 "Graphic::Candle::BorderColor",
66 "Graphic::ForegroundColor"
67 ));
68
69 $self->{'width'} = Finance::GeniusTrader::Conf::get("Graphic::Candle::Width");
70 }
71  
72 sub display {
73 my ($self, $driver, $picture) = @_;
74 my $scale = $self->get_scale();
75 my $zone = $self->{'zone'};
76 my $width = $self->{'width'};
77 my ($start, $end) = $self->{'source'}->get_selected_range();
78  
79 # only $y_min and $y_max are significant
80 my ($x_min, $y_min) = $scale->get_value_from_coordinate($start, 0);
81 my ($x_max, $y_max) = $scale->get_value_from_coordinate($end, $zone->height-1);
82  
83 # these are coordinate values of $y_min and $y_max
84 my $yc_min = $scale->convert_to_y_coordinate($y_min);
85 my $yc_max = $scale->convert_to_y_coordinate($y_max);
86  
87 my $holevolume = 0;
88 for(my $i = $start; $i <= $end; $i++)
89 {
90 my $data = $self->{'source'}->get($i);
91 $holevolume += $data->[$VOLUME];
92 }
93 my $holewidth = $scale->convert_to_x_coordinate($end)-
94 $scale->convert_to_x_coordinate($start) + $self->{'width'};
95  
96 my $lastx = $scale->convert_to_x_coordinate($start);
97 for(my $i = $start; $i <= $end; $i++)
98 {
99 my $data = $self->{'source'}->get($i);
100 my $low = $scale->convert_to_y_coordinate($data->[$LOW]);
101 my $open = $scale->convert_to_y_coordinate($data->[$OPEN]);
102 my $close = $scale->convert_to_y_coordinate($data->[$CLOSE]);
103 my $high = $scale->convert_to_y_coordinate($data->[$HIGH]);
104 my $x = $lastx;
105 $self->{'width'} = $holewidth * $data->[$VOLUME] / $holevolume;
106  
107 # clip $y at top of zone
108 $low = $yc_max if ( $low > $yc_max );
109 $open = $yc_max if ( $open > $yc_max );
110 $close = $yc_max if ( $close > $yc_max );
111 $high = $yc_max if ( $high > $yc_max );
112 # clip $y at bottom of zone
113 $low = $yc_min if ( $low < $yc_min );
114 $open = $yc_min if ( $open < $yc_min );
115 $close = $yc_min if ( $close < $yc_min );
116 $high = $yc_min if ( $high < $yc_min );
117  
118 if ($data->[$OPEN] < $data->[$CLOSE]) {
119
120 $driver->filled_rectangle($picture,
121 $zone->absolute_coordinate($x + 1, $open + 1),
122 $zone->absolute_coordinate($x + $self->{'width'} - 2 - 1, $close - 1),
123 $self->{'up_body_color'});
124  
125 $driver->rectangle($picture,
126 $zone->absolute_coordinate($x, $open),
127 $zone->absolute_coordinate($x + $self->{'width'} - 2, $close),
128 $self->{'up_body_border_color'});
129
130 $driver->line($picture,
131 $zone->absolute_coordinate($x + int($self->{'width'} / 2) - 1, $close + 1),
132 $zone->absolute_coordinate($x + int($self->{'width'} / 2) - 1, $high), $self->{'up_shadows_color'});
133
134 $driver->line($picture,
135 $zone->absolute_coordinate($x + int($self->{'width'} / 2) - 1, $low),
136 $zone->absolute_coordinate($x + int($self->{'width'} / 2) - 1, $open - 1), $self->{'up_shadows_color'});
137  
138 } else {
139
140 $driver->filled_rectangle($picture,
141 $zone->absolute_coordinate($x + 1, $close + 1),
142 $zone->absolute_coordinate($x + $self->{'width'} - 2 - 1, $open - 1), $self->{'down_body_color'});
143  
144 $driver->rectangle($picture,
145 $zone->absolute_coordinate($x, $close),
146 $zone->absolute_coordinate($x + $self->{'width'} - 2, $open), $self->{'down_body_border_color'});
147  
148 $driver->line($picture,
149 $zone->absolute_coordinate($x + int($self->{'width'} / 2) - 1, $open + 1),
150 $zone->absolute_coordinate($x + int($self->{'width'} / 2) - 1, $high), $self->{'down_shadows_color'});
151
152 $driver->line($picture,
153 $zone->absolute_coordinate($x + int($self->{'width'} / 2) - 1, $low),
154 $zone->absolute_coordinate($x + int($self->{'width'} / 2) - 1, $close - 1), $self->{'down_shadows_color'});
155 }
156 $lastx += ($self->{'width'} = $holewidth * $data->[$VOLUME] / $holevolume);
157 }
158 }
159  
160 1;