| 1 | package Finance::GeniusTrader::Signals::Trend::HilbertChannelBreakout; | ||
| 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 @NAMES); | ||
| 9 | |||
| 10 | use Finance::GeniusTrader::Signals; | ||
| 11 | use Finance::GeniusTrader::Prices; | ||
| 12 | use Finance::GeniusTrader::Indicators::HilbertPeriod; | ||
| 13 | |||
| 14 | @ISA = qw(Finance::GeniusTrader::Signals); | ||
| 15 | @NAMES = ("HCBUp[#1,#2,#3,#4]", "HCBDown[#1,#2,#3,#4]"); | ||
| 16 | |||
| 17 | =head1 Finance::GeniusTrader::Signals::Trend::HilbertChannelBreakout | ||
| 18 | |||
| 19 | =cut | ||
| 20 | |||
| 21 | sub new { | ||
| 22 | my $type = shift; | ||
| 23 | my $class = ref($type) || $type; | ||
| 24 | my $args = shift; | ||
| 25 | |||
| 26 | my $self = { "args" => defined($args) ? $args : [ 15, 15, 0, 0 ] }; | ||
| 27 | |||
| 28 | $args->[0] = 15 if (! defined($args->[0])); | ||
| 29 | $args->[1] = 15 if (! defined($args->[1])); | ||
| 30 | $args->[2] = 0 if (! defined($args->[2])); | ||
| 31 | $args->[3] = 0 if (! defined($args->[3])); | ||
| 32 | |||
| 33 | return manage_object(\@NAMES, $self, $class, $self->{'args'}, ""); | ||
| 34 | } | ||
| 35 | |||
| 36 | sub initialize { | ||
| 37 | my ($self) = @_; | ||
| 38 | |||
| 39 | $self->{'period'} = Finance::GeniusTrader::Indicators::HilbertPeriod->new(); | ||
| 40 | $self->add_indicator_dependency($self->{'period'}, 1); | ||
| 41 | } | ||
| 42 | |||
| 43 | sub detect { | ||
| 44 | my ($self, $calc, $i) = @_; | ||
| 45 | my $indic = $calc->indicators; | ||
| 46 | my $prices = $calc->prices; | ||
| 47 | my $name_up = $self->get_name(0); | ||
| 48 | my $name_down = $self->get_name(1); | ||
| 49 | my $entry_value = $self->{'args'}[0]; | ||
| 50 | my $exit_value = $self->{'args'}[1]; | ||
| 51 | my $entry_k = $self->{'args'}[2]; | ||
| 52 | my $exit_k = $self->{'args'}[3]; | ||
| 53 | my $entry_look_back = 0; | ||
| 54 | my $exit_look_back = 0; | ||
| 55 | |||
| 56 | return if ($calc->signals->is_available($name_up, $i)); | ||
| 57 | return if (! $self->check_dependencies($calc, $i)); | ||
| 58 | |||
| 59 | my $period = $indic->get($self->{'period'}->get_name, $i); | ||
| 60 | |||
| 61 | return if ($i < $period); | ||
| 62 | |||
| 63 | if (defined($entry_value) && $entry_value != 0) { | ||
| 64 | $entry_look_back = $entry_value; | ||
| 65 | } else { | ||
| 66 | $entry_look_back = $entry_k * $period; | ||
| 67 | } | ||
| 68 | |||
| 69 | if (defined($exit_value) && $exit_value != 0) { | ||
| 70 | $exit_look_back = $exit_value; | ||
| 71 | } else { | ||
| 72 | $exit_look_back = $exit_k * $period; | ||
| 73 | } | ||
| 74 | |||
| 75 | $entry_look_back = 1 if ($entry_look_back < 1); | ||
| 76 | $exit_look_back = 1 if ($exit_look_back < 1); | ||
| 77 | |||
| 78 | my $entry_channel = $prices->at($i - 1)->[$HIGH]; | ||
| 79 | for (my $n = 1; $n <= $entry_look_back; $n++) { | ||
| 80 | |||
| 81 | if ($entry_channel < $prices->at($i - $n)->[$HIGH]) { | ||
| 82 | $entry_channel = $prices->at($i - $n)->[$HIGH]; | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | my $exit_channel = $prices->at($i - 1)->[$LOW]; | ||
| 87 | for (my $n = 1; $n <= $exit_look_back; $n++) { | ||
| 88 | |||
| 89 | if ($exit_channel > $prices->at($i - $n)->[$LOW]) { | ||
| 90 | $exit_channel = $prices->at($i - $n)->[$LOW]; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | if ($prices->at($i)->[$HIGH] > $entry_channel) { | ||
| 95 | $calc->signals->set($name_up, $i, 1); | ||
| 96 | } else { | ||
| 97 | $calc->signals->set($name_up, $i, 0); | ||
| 98 | } | ||
| 99 | |||
| 100 | if ($prices->at($i)->[$LOW] < $exit_channel) { | ||
| 101 | $calc->signals->set($name_down, $i, 1); | ||
| 102 | } else { | ||
| 103 | $calc->signals->set($name_down, $i, 0); | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | sub detect_interval { | ||
| 108 | my ($self, $calc, $first, $last) = @_; | ||
| 109 | |||
| 110 | $self->{'period'}->calculate_interval($calc, $first, $last); | ||
| 111 | Finance::GeniusTrader::Signals::detect_interval(@_); | ||
| 112 | } | ||
| 113 | |||
| 114 | 1; |