| 1 | package Finance::GeniusTrader::Indicators::HilbertPeriod; | ||
| 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::WMA; | ||
| 12 | use Finance::GeniusTrader::Indicators::WTCL; | ||
| 13 | use Finance::GeniusTrader::Indicators::Generic::ByName; | ||
| 14 | use Finance::GeniusTrader::Prices; | ||
| 15 | use POSIX; | ||
| 16 | |||
| 17 | @ISA = qw(Finance::GeniusTrader::Indicators); | ||
| 18 | @NAMES = ("HilbertPeriod", "HP:Detrender", "HP:Q1", "HP:I1", "HP:jI", | ||
| 19 | "HP:jQ", "HP:I2", "HP:Q2", "HP:Re", "HP:Im"); | ||
| 20 | |||
| 21 | =head1 Finance::GeniusTrader::Indicators::HilbertPeriod | ||
| 22 | |||
| 23 | =head2 Overview | ||
| 24 | |||
| 25 | =head2 Calculation | ||
| 26 | |||
| 27 | =head2 Examples | ||
| 28 | |||
| 29 | =head2 Links | ||
| 30 | |||
| 31 | TASC November 2000 - page 108 | ||
| 32 | |||
| 33 | =cut | ||
| 34 | sub initialize { | ||
| 35 | my $self = shift; | ||
| 36 | |||
| 37 | # Initilize TR (True Range) | ||
| 38 | $self->{'median'} = Finance::GeniusTrader::Indicators::WTCL->new([0]); | ||
| 39 | $self->{'smoother'} = Finance::GeniusTrader::Indicators::WMA->new([4, | ||
| 40 | "{I:Generic:ByName " . $self->{'median'}->get_name . "}" ]); | ||
| 41 | |||
| 42 | $self->add_indicator_dependency($self->{'median'}, 1); | ||
| 43 | |||
| 44 | } | ||
| 45 | |||
| 46 | =head2 Finance::GeniusTrader::Indicators::HilbertPeriod::calculate($calc, $day) | ||
| 47 | |||
| 48 | =cut | ||
| 49 | sub calculate { | ||
| 50 | my ($self, $calc, $i) = @_; | ||
| 51 | my $indic = $calc->indicators; | ||
| 52 | my $prices = $calc->prices; | ||
| 53 | my $smoother_name = $self->{'smoother'}->get_name; | ||
| 54 | |||
| 55 | return if ($indic->is_available($self->get_name, $i)); | ||
| 56 | |||
| 57 | $self->{'median'}->calculate_interval($calc, 0, $i); | ||
| 58 | $self->{'smoother'}->calculate_interval($calc, 0, $i); | ||
| 59 | |||
| 60 | for(my $n = 0; $n <= 24; $n++) | ||
| 61 | { | ||
| 62 | $indic->set($self->get_name, $n, 0); | ||
| 63 | } | ||
| 64 | for(my $n = 0; $n <= $i; $n++) | ||
| 65 | { | ||
| 66 | # Calcul de detrender | ||
| 67 | if ($indic->is_available_interval($smoother_name, $n - 6, $n) && | ||
| 68 | $indic->is_available($self->get_name, $n - 1)) | ||
| 69 | { | ||
| 70 | $indic->set("HP:Detrender", $n, | ||
| 71 | ( | ||
| 72 | 0.25 * $indic->get($smoother_name, $n) + | ||
| 73 | 0.75 * $indic->get($smoother_name, $n - 2) - | ||
| 74 | 0.75 * $indic->get($smoother_name, $n - 4) - | ||
| 75 | 0.25 * $indic->get($smoother_name, $n - 6) | ||
| 76 | ) * (0.046 * $indic->get($self->get_name, $n - 1) + 0.332) | ||
| 77 | ); | ||
| 78 | } | ||
| 79 | |||
| 80 | # Calcul de Q1 | ||
| 81 | if ($indic->is_available_interval("HP:Detrender", $n - 6, $n) && | ||
| 82 | $indic->is_available($self->get_name, $n - 1)) | ||
| 83 | { | ||
| 84 | $indic->set("HP:Q1", $n, | ||
| 85 | ( | ||
| 86 | 0.25 * $indic->get("HP:Detrender", $n) + | ||
| 87 | 0.75 * $indic->get("HP:Detrender", $n - 2) - | ||
| 88 | 0.75 * $indic->get("HP:Detrender", $n - 4) - | ||
| 89 | 0.25 * $indic->get("HP:Detrender", $n - 6) | ||
| 90 | ) * (0.046 * $indic->get($self->get_name, $n - 1) + 0.332) | ||
| 91 | ); | ||
| 92 | } | ||
| 93 | |||
| 94 | # Calcul de I1 | ||
| 95 | if ($indic->is_available("HP:Detrender", $n - 3)) | ||
| 96 | { | ||
| 97 | $indic->set("HP:I1", $n, | ||
| 98 | $indic->get("HP:Detrender", $n - 3) | ||
| 99 | ); | ||
| 100 | } | ||
| 101 | |||
| 102 | # Calcul de jI | ||
| 103 | if ($indic->is_available_interval("HP:I1", $n - 6, $n)) | ||
| 104 | { | ||
| 105 | $indic->set("HP:jI", $n, | ||
| 106 | 0.25 * $indic->get("HP:I1", $n) + | ||
| 107 | 0.75 * $indic->get("HP:I1", $n - 2) - | ||
| 108 | 0.75 * $indic->get("HP:I1", $n - 4) - | ||
| 109 | 0.25 * $indic->get("HP:I1", $n - 6) | ||
| 110 | ); | ||
| 111 | } | ||
| 112 | |||
| 113 | # Calcul de jQ | ||
| 114 | if ($indic->is_available_interval("HP:Q1", $n - 6, $n)) | ||
| 115 | { | ||
| 116 | $indic->set("HP:jQ", $n, | ||
| 117 | 0.25 * $indic->get("HP:Q1", $n) + | ||
| 118 | 0.75 * $indic->get("HP:Q1", $n - 2) - | ||
| 119 | 0.75 * $indic->get("HP:Q1", $n - 4) - | ||
| 120 | 0.25 * $indic->get("HP:Q1", $n - 6) | ||
| 121 | ); | ||
| 122 | } | ||
| 123 | |||
| 124 | # Calcul de I2 | ||
| 125 | if ($indic->is_available("HP:I1", $n) && | ||
| 126 | $indic->is_available("HP:jQ", $n)) | ||
| 127 | { | ||
| 128 | $indic->set("HP:I2", $n, | ||
| 129 | $indic->get("HP:I1", $n) - $indic->get("HP:jQ", $n) | ||
| 130 | ); | ||
| 131 | } | ||
| 132 | |||
| 133 | # Calcul de Q2 | ||
| 134 | if ($indic->is_available("HP:Q1", $n) && | ||
| 135 | $indic->is_available("HP:jI", $n)) | ||
| 136 | { | ||
| 137 | $indic->set("HP:Q2", $n, | ||
| 138 | $indic->get("HP:Q1", $n) + $indic->get("HP:jI", $n) | ||
| 139 | ); | ||
| 140 | } | ||
| 141 | |||
| 142 | # Calcul de I2 bis | ||
| 143 | if ($indic->is_available_interval("HP:I2", $n - 1, $n)) | ||
| 144 | { | ||
| 145 | $indic->set("HP:I2", $n, | ||
| 146 | 0.15 * $indic->get("HP:I2", $n) + | ||
| 147 | 0.85 * $indic->get("HP:I2", $n - 1) | ||
| 148 | ); | ||
| 149 | } | ||
| 150 | |||
| 151 | # Calcul de Q2 bis | ||
| 152 | if ($indic->is_available_interval("HP:Q2", $n - 1, $n)) | ||
| 153 | { | ||
| 154 | $indic->set("HP:Q2", $n, | ||
| 155 | 0.15 * $indic->get("HP:Q2", $n) + | ||
| 156 | 0.85 * $indic->get("HP:Q2", $n - 1) | ||
| 157 | ); | ||
| 158 | } | ||
| 159 | |||
| 160 | # Calcul de Re et Im | ||
| 161 | if ($indic->is_available_interval("HP:I2", $n - 1, $n) && | ||
| 162 | $indic->is_available_interval("HP:Q2", $n - 1, $n)) | ||
| 163 | { | ||
| 164 | my $X1 = $indic->get("HP:I2", $n) * $indic->get("HP:I2", $n - 1); | ||
| 165 | my $X2 = $indic->get("HP:I2", $n) * $indic->get("HP:Q2", $n - 1); | ||
| 166 | my $Y1 = $indic->get("HP:Q2", $n) * $indic->get("HP:Q2", $n - 1); | ||
| 167 | my $Y2 = $indic->get("HP:Q2", $n) * $indic->get("HP:I2", $n - 1); | ||
| 168 | $indic->set("HP:Re", $n, $X1 + $Y1); | ||
| 169 | $indic->set("HP:Im", $n, $X2 - $Y2); | ||
| 170 | } | ||
| 171 | |||
| 172 | # Calcul de Re bis | ||
| 173 | if ($indic->is_available_interval("HP:Re", $n - 1, $n)) | ||
| 174 | { | ||
| 175 | $indic->set("HP:Re", $n, | ||
| 176 | 0.2 * $indic->get("HP:Re", $n) + | ||
| 177 | 0.8 * $indic->get("HP:Re", $n - 1) | ||
| 178 | ); | ||
| 179 | } | ||
| 180 | |||
| 181 | # Calcul de Im bis | ||
| 182 | if ($indic->is_available_interval("HP:Im", $n - 1, $n)) | ||
| 183 | { | ||
| 184 | $indic->set("HP:Im", $n, | ||
| 185 | 0.2 * $indic->get("HP:Im", $n) + | ||
| 186 | 0.8 * $indic->get("HP:Im", $n - 1) | ||
| 187 | ); | ||
| 188 | } | ||
| 189 | |||
| 190 | # Calcul de HilbertPeriod | ||
| 191 | if ($indic->is_available("HP:Im", $n) && | ||
| 192 | $indic->is_available("HP:Re", $n) && | ||
| 193 | $indic->is_available("HilbertPeriod", $n - 1)) | ||
| 194 | { | ||
| 195 | my $im = $indic->get("HP:Im", $n); | ||
| 196 | my $re = $indic->get("HP:Re", $n); | ||
| 197 | my $prev_period = $indic->get("HilbertPeriod", $n - 1); | ||
| 198 | my $period = 0; | ||
| 199 | |||
| 200 | if (($re != 0) and ($im != 0)) | ||
| 201 | { | ||
| 202 | $period = 1 / (POSIX::atan($im / $re) / (2 * 3.1415)); | ||
| 203 | } | ||
| 204 | |||
| 205 | if ($period > 1.5 * $prev_period) | ||
| 206 | { | ||
| 207 | $period = 1.5 * $prev_period; | ||
| 208 | } | ||
| 209 | |||
| 210 | if ($period < 0.67 * $prev_period) | ||
| 211 | { | ||
| 212 | $period = 0.67 * $prev_period; | ||
| 213 | } | ||
| 214 | |||
| 215 | $period = 6 if ($period < 6); | ||
| 216 | $period = 50 if ($period > 50); | ||
| 217 | |||
| 218 | $period = 0.2 * $period + 0.8 * $prev_period; | ||
| 219 | |||
| 220 | $indic->set("HilbertPeriod", $n, $period); | ||
| 221 | } | ||
| 222 | } | ||
| 223 | } | ||
| 224 | |||
| 225 | sub calculate_interval { | ||
| 226 | my ($self, $calc, $first, $last) = @_; | ||
| 227 | |||
| 228 | # Calculate ADX for the last record | ||
| 229 | # so that all intermediate values will be stored | ||
| 230 | $self->calculate($calc, $last); | ||
| 231 | |||
| 232 | } | ||
| 233 | |||
| 234 | 1; |