| 1 | package Finance::GeniusTrader::Indicators::HilbertSine; | ||
| 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::Indicators; | ||
| 11 | use Finance::GeniusTrader::Indicators::WTCL; | ||
| 12 | use Finance::GeniusTrader::Indicators::HilbertPeriod; | ||
| 13 | use Finance::GeniusTrader::Prices; | ||
| 14 | use Finance::GeniusTrader::Tools qw(:math); | ||
| 15 | use POSIX; | ||
| 16 | |||
| 17 | @ISA = qw(Finance::GeniusTrader::Indicators); | ||
| 18 | @NAMES = ("HilbertSine", "HilbertLeadSine"); | ||
| 19 | |||
| 20 | =head1 Finance::GeniusTrader::Indicators::HilbertSine | ||
| 21 | |||
| 22 | =head2 Overview | ||
| 23 | |||
| 24 | =head2 Calculation | ||
| 25 | |||
| 26 | =head2 Examples | ||
| 27 | |||
| 28 | =head2 Links | ||
| 29 | |||
| 30 | TASC May 2000 - page 27 | ||
| 31 | |||
| 32 | =cut | ||
| 33 | sub initialize { | ||
| 34 | my $self = shift; | ||
| 35 | |||
| 36 | $self->{'median'} = Finance::GeniusTrader::Indicators::WTCL->new([0]); | ||
| 37 | $self->{'period'} = Finance::GeniusTrader::Indicators::HilbertPeriod->new; | ||
| 38 | |||
| 39 | $self->add_indicator_dependency($self->{'period'}, 1); | ||
| 40 | } | ||
| 41 | |||
| 42 | sub calculate { | ||
| 43 | my ($self, $calc, $i) = @_; | ||
| 44 | my $indic = $calc->indicators; | ||
| 45 | my $prices = $calc->prices; | ||
| 46 | |||
| 47 | return if ($indic->is_available($self->get_name, $i)); | ||
| 48 | return if (! $self->check_dependencies($calc, $i)); | ||
| 49 | |||
| 50 | my $period = $indic->get($self->{'period'}->get_name, $i); | ||
| 51 | |||
| 52 | return if ($period > $i + 1); | ||
| 53 | |||
| 54 | $self->{'median'}->calculate_interval($calc, $i - $period + 1, $i); | ||
| 55 | |||
| 56 | my ($real, $imag) = (0, 0); | ||
| 57 | for (my $n = 0; $n < $period; $n++) | ||
| 58 | { | ||
| 59 | my $price = $indic->get($self->{'median'}->get_name, $i - $n); | ||
| 60 | $real += sin(2 * PI * $n / int($period)) * $price; | ||
| 61 | $imag += cos(2 * PI * $n / int($period)) * $price; | ||
| 62 | } | ||
| 63 | |||
| 64 | my $phase = 0; | ||
| 65 | if (abs($imag) > 0.001) { | ||
| 66 | $phase = POSIX::atan($real / $imag); | ||
| 67 | } else { | ||
| 68 | $phase = PI / 2 * sign($real); | ||
| 69 | } | ||
| 70 | |||
| 71 | if (($period < 30) && ($period > 0)) { | ||
| 72 | $phase += (6.818 / $period - 0.227) * 2 * PI; | ||
| 73 | } | ||
| 74 | |||
| 75 | $phase += PI / 2; | ||
| 76 | if ($imag < 0) { $phase += PI } | ||
| 77 | if ($phase > 7 / 8 * 2 * PI) { $phase -= 2 * PI } | ||
| 78 | |||
| 79 | $indic->set($self->get_name(0), $i, sin($phase)); | ||
| 80 | $indic->set($self->get_name(1), $i, sin($phase + PI / 4)); | ||
| 81 | } | ||
| 82 | |||
| 83 | sub calculate_interval { | ||
| 84 | my ($self, $calc, $first, $last) = @_; | ||
| 85 | |||
| 86 | $self->{'period'}->calculate_interval($calc, $first, $last); | ||
| 87 | $self->{'median'}->calculate_interval($calc, $first, $last); | ||
| 88 | for (my $i = $first; $i <= $last; $i++) | ||
| 89 | { | ||
| 90 | $self->calculate($calc, $i); | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | 1; |