| 1 | package Finance::GeniusTrader::Graphics::Object::BuySellArrows; | ||
| 2 | |||
| 3 | # Copyright 2000-2002 Raphaƫl Hertzog, Fabien Fulhaber | ||
| 4 | # Copyright 2008 Robert A. Schmied | ||
| 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 | # ras hack based on version dated 24apr05 2865 bytes | ||
| 9 | # $Id$ | ||
| 10 | |||
| 11 | use strict; | ||
| 12 | use vars qw(@ISA); | ||
| 13 | @ISA = qw(Finance::GeniusTrader::Graphics::Object); | ||
| 14 | |||
| 15 | use Finance::GeniusTrader::Prices; | ||
| 16 | use Finance::GeniusTrader::Graphics::Object; | ||
| 17 | use Finance::GeniusTrader::Graphics::Driver; | ||
| 18 | use Finance::GeniusTrader::Graphics::Tools qw(:color); | ||
| 19 | use Finance::GeniusTrader::Conf; | ||
| 20 | |||
| 21 | Finance::GeniusTrader::Conf::default("Graphic::BuySellArrows::BuyColor", "green"); | ||
| 22 | Finance::GeniusTrader::Conf::default("Graphic::BuySellArrows::SellColor", "red"); | ||
| 23 | Finance::GeniusTrader::Conf::default("Graphic::BuySellArrows::Distance", 8); | ||
| 24 | Finance::GeniusTrader::Conf::default("Graphic::Candle::Height", 3); | ||
| 25 | |||
| 26 | Finance::GeniusTrader::Conf::default("Graphic::BuySellArrows::SizeFactor", 1); | ||
| 27 | # SizeFactor can be used to adjust the default size in both height and width | ||
| 28 | # without it the width cannot be altered | ||
| 29 | # | ||
| 30 | # i recommend altering the size via the SizeFactor and leave Graphic::Candle::Height at 3 | ||
| 31 | # | ||
| 32 | # i considered changing Graphic::Candle::Height to the more logical Graphic::BuySellArrows::Height | ||
| 33 | # but didn't know if there was a ulterior motive for this name or not so i left it alone | ||
| 34 | # | ||
| 35 | |||
| 36 | # we should also transparent these colors as well -- ah well you can do | ||
| 37 | # that in your gt config options file: | ||
| 38 | # Graphic::BuySellArrows::BuyColor "[0,135,0,64]" # partly transparent dark green | ||
| 39 | # Graphic::BuySellArrows::SellColor "[150,0,0,64]" # partly transparent dark red | ||
| 40 | # | ||
| 41 | |||
| 42 | =head1 Finance::GeniusTrader::Graphics::Object::BuySellArrows | ||
| 43 | |||
| 44 | This graphical object display buy and sell arrows. | ||
| 45 | |||
| 46 | =head2 Description and Usage | ||
| 47 | |||
| 48 | the object doesn't accept arguments when created, however it reads gt configure file | ||
| 49 | for and sets these values (default values are indicated): | ||
| 50 | |||
| 51 | Graphic::BuySellArrows::BuyColor "green" | ||
| 52 | Graphic::BuySellArrows::SellColor "red" | ||
| 53 | Graphic::BuySellArrows::Distance 8 | ||
| 54 | Graphic::Candle::Height 3 | ||
| 55 | Graphic::BuySellArrows::SizeFactor 1 | ||
| 56 | |||
| 57 | note: the default SizeFactor of 1 should make this modified version work | ||
| 58 | identically to the prior version | ||
| 59 | |||
| 60 | personally, i find a Distance of about 24 and a SizeFactor of 3 to 6 makes the arrow | ||
| 61 | plot better. in addition i prefer to darken the colors and make the partly transparent | ||
| 62 | |||
| 63 | Graphic::BuySellArrows::BuyColor "[0,135,0,64]" # very dark green | ||
| 64 | Graphic::BuySellArrows::SellColor "[150,0,0,64]" # dark red | ||
| 65 | |||
| 66 | |||
| 67 | =head2 used in graphic.pl graphic configuration file as shown below: | ||
| 68 | |||
| 69 | =head2 plotting on the primary price plot | ||
| 70 | |||
| 71 | --add=BuySellArrows(Systems::Generic \ | ||
| 72 | { S::Generic::CrossOverUp {I::MACD/1 26 52 20} {I::MACD/2 26 52 20} } \ | ||
| 73 | { S::Generic::CrossOverDown {I::MACD/1 26 52 20} {I::MACD/2 26 52 20} } \ | ||
| 74 | ) | ||
| 75 | |||
| 76 | =head2 plotting arrows on a secondary zone | ||
| 77 | |||
| 78 | --add=New-Zone(5) | ||
| 79 | |||
| 80 | --add=New-Zone(100) | ||
| 81 | --add=Curve(I:MACD/1 26 52 20, [120, 40, 0]) | ||
| 82 | --add=Curve(I:MACD/2 26 52 20, red) | ||
| 83 | --add=Text("macd: 26 52 20", 2, 95, left, center, small, [120, 40, 0], arial) | ||
| 84 | --add=Set-Scale(auto) | ||
| 85 | |||
| 86 | --add=BuySellArrows(Systems::Generic \ | ||
| 87 | { S::Generic::CrossOverUp {I::MACD/1 26 52 20} {I::MACD/2 26 52 20} } \ | ||
| 88 | { S::Generic::CrossOverDown {I::MACD/1 26 52 20} {I::MACD/2 26 52 20} } \ | ||
| 89 | ) | ||
| 90 | --add=Set-Special-Scale(auto,"log") | ||
| 91 | |||
| 92 | |||
| 93 | =cut | ||
| 94 | |||
| 95 | sub init { | ||
| 96 | my ($self, $prices_ds) = @_; | ||
| 97 | |||
| 98 | # Default values ... | ||
| 99 | $self->{'buy_color'} = get_color(Finance::GeniusTrader::Conf::get("Graphic::BuySellArrows::BuyColor")); | ||
| 100 | $self->{'sell_color'} = get_color(Finance::GeniusTrader::Conf::get("Graphic::BuySellArrows::SellColor")); | ||
| 101 | $self->{'distance'} = Finance::GeniusTrader::Conf::get("Graphic::BuySellArrows::Distance"); | ||
| 102 | $self->{'height'} = Finance::GeniusTrader::Conf::get("Graphic::Candle::Height"); | ||
| 103 | $self->{'sizefactor'} = Finance::GeniusTrader::Conf::get("Graphic::BuySellArrows::SizeFactor"); | ||
| 104 | $self->{'prices_ds'} = $prices_ds; | ||
| 105 | } | ||
| 106 | |||
| 107 | sub display { | ||
| 108 | my ($self, $driver, $picture) = @_; | ||
| 109 | my $scale = $self->get_scale(); | ||
| 110 | my $zone = $self->{'zone'}; | ||
| 111 | my ($start, $end) = $self->{'source'}->get_selected_range(); | ||
| 112 | my $space = $scale->convert_to_x_coordinate($start + 1) - | ||
| 113 | $scale->convert_to_x_coordinate($start); | ||
| 114 | |||
| 115 | my $xoff = int($space * $self->{'sizefactor'} / 2); | ||
| 116 | my $arrowht = int($self->{'height'} * $self->{'sizefactor'}); | ||
| 117 | |||
| 118 | for(my $i = $start; $i <= $end; $i++) | ||
| 119 | { | ||
| 120 | my $prices = $self->{'prices_ds'}->get($i); | ||
| 121 | my $low = $scale->convert_to_y_coordinate($prices->[$LOW]); | ||
| 122 | my $high = $scale->convert_to_y_coordinate($prices->[$HIGH]); | ||
| 123 | my $x = $scale->convert_to_x_coordinate($i); | ||
| 124 | |||
| 125 | # Get the datasource value : | ||
| 126 | # 1 => buy signal | ||
| 127 | # 0 => no signal | ||
| 128 | # -1 => sell signal | ||
| 129 | my $value = $self->{'source'}->get($i); | ||
| 130 | |||
| 131 | # Draw an Up and Green Arrow if value = 1 | ||
| 132 | if ($value eq 1) { | ||
| 133 | my $yb = $low - $self->{'distance'} - $arrowht; | ||
| 134 | my $xa = $x + int($space / 2) - 1; | ||
| 135 | ( $xa, my $ya ) = ( $xa, $low - $self->{'distance'} ); | ||
| 136 | my ( $x2, $y2 ) = ( $xa - int($xoff / 2), $yb ); | ||
| 137 | my ( $x3, $y3 ) = ( $xa + int($xoff / 2), $yb ); | ||
| 138 | my @points = ( | ||
| 139 | [$zone->absolute_coordinate( $xa, $ya )], | ||
| 140 | [$zone->absolute_coordinate( $x2, $y2 )], | ||
| 141 | [$zone->absolute_coordinate( $x3, $y3 )] | ||
| 142 | ); | ||
| 143 | $driver->filled_polygon($picture, $self->{'buy_color'}, @points); | ||
| 144 | } | ||
| 145 | |||
| 146 | # Draw a Down and Red Arrow if value = -1 | ||
| 147 | if ($value eq -1) { | ||
| 148 | my $yb = $high + $self->{'distance'} + $arrowht; | ||
| 149 | my $xa = $x + int($space / 2) - 1; | ||
| 150 | ( $xa, my $ya ) = ( $xa, $high + $self->{'distance'} ); | ||
| 151 | my ( $x2, $y2 ) = ( $xa - int($xoff / 2), $yb ); | ||
| 152 | my ( $x3, $y3 ) = ( $xa + int($xoff / 2), $yb ); | ||
| 153 | my @points = ( | ||
| 154 | [$zone->absolute_coordinate( $xa, $ya )], | ||
| 155 | [$zone->absolute_coordinate( $x2, $y2 )], | ||
| 156 | [$zone->absolute_coordinate( $x3, $y3 )] | ||
| 157 | ); | ||
| 158 | $driver->filled_polygon($picture, $self->{'sell_color'}, @points); | ||
| 159 | } | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | 1; |