| 1 | package Finance::GeniusTrader::Indicators::VHF; | ||
| 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 @DEFAULT_ARGS); | ||
| 9 | |||
| 10 | use Finance::GeniusTrader::Indicators; | ||
| 11 | use Finance::GeniusTrader::Indicators::Generic::MinInPeriod; | ||
| 12 | use Finance::GeniusTrader::Indicators::Generic::MaxInPeriod; | ||
| 13 | use Finance::GeniusTrader::Prices; | ||
| 14 | |||
| 15 | @ISA = qw(Finance::GeniusTrader::Indicators); | ||
| 16 | @NAMES = ("VHF[#*]"); | ||
| 17 | @DEFAULT_ARGS = (28, "{I:Prices CLOSE}"); | ||
| 18 | |||
| 19 | =head1 Finance::GeniusTrader::Indicators::VHF | ||
| 20 | |||
| 21 | =head2 Overview | ||
| 22 | |||
| 23 | The Vertical Horizontal Filter (VHF) can tell you whether a market is going through a trending or congestion phase, and whether you should use trend-following indicators if the markets are trending or congestion-phase indicators if markets are in a trading range. | ||
| 24 | |||
| 25 | =head2 Calculation | ||
| 26 | |||
| 27 | VHF = (Highest Close (n) - Lowest Close (n)) / (Sum of absolute value of the one-day price change for the range (n)) | ||
| 28 | |||
| 29 | =head2 Parameters | ||
| 30 | |||
| 31 | The standard VHF works with a 28-days parameter : n = 28 | ||
| 32 | |||
| 33 | =head2 Example | ||
| 34 | |||
| 35 | Finance::GeniusTrader::Indicators::VHF->new() | ||
| 36 | Finance::GeniusTrader::Indicators::VHF->new([50]) | ||
| 37 | |||
| 38 | =head2 Links | ||
| 39 | |||
| 40 | http://www.equis.com/free/taaz/verthorizfilter.html | ||
| 41 | http://www.finance-net.com/apprendre/techniques/vhf.phtml | ||
| 42 | |||
| 43 | =cut | ||
| 44 | sub initialize { | ||
| 45 | my $self = shift; | ||
| 46 | |||
| 47 | $self->{'min'} = Finance::GeniusTrader::Indicators::Generic::MinInPeriod->new([ $self->{'args'}->get_arg_names(1), $self->{'args'}->get_arg_names(2) ]); | ||
| 48 | $self->{'max'} = Finance::GeniusTrader::Indicators::Generic::MaxInPeriod->new([ $self->{'args'}->get_arg_names(1), $self->{'args'}->get_arg_names(2) ]); | ||
| 49 | |||
| 50 | $self->add_indicator_dependency($self->{'min'}, 1); | ||
| 51 | $self->add_indicator_dependency($self->{'max'}, 1); | ||
| 52 | $self->add_prices_dependency($self->{'args'}->get_arg_names(1)); | ||
| 53 | } | ||
| 54 | |||
| 55 | =head2 Finance::GeniusTrader::Indicators::VHF::calculate($calc, $day) | ||
| 56 | |||
| 57 | =cut | ||
| 58 | sub calculate { | ||
| 59 | my ($self, $calc, $i) = @_; | ||
| 60 | my $indic = $calc->indicators; | ||
| 61 | my $prices = $calc->prices; | ||
| 62 | my $period = $self->{'args'}->get_arg_names(1); | ||
| 63 | my $min = $self->{'min'}; | ||
| 64 | my $max = $self->{'max'}; | ||
| 65 | my $min_name = $min->get_name; | ||
| 66 | my $max_name = $max->get_name; | ||
| 67 | my $vhf_name = $self->get_name(0); | ||
| 68 | my $sum_of_one_day_price_change = 0; | ||
| 69 | |||
| 70 | return if ($indic->is_available($vhf_name, $i)); | ||
| 71 | return if (! $self->check_dependencies($calc, $i)); | ||
| 72 | |||
| 73 | # Get Highest Close and Lowest Close values | ||
| 74 | my $lowest_close = $indic->get($min_name, $i); | ||
| 75 | my $highest_close = $indic->get($max_name, $i); | ||
| 76 | |||
| 77 | for (my $n = $i - $period + 1; $n <= $i; $n++) { | ||
| 78 | # Calculate the sum of absolute value of the one-day price change over the period | ||
| 79 | $sum_of_one_day_price_change += abs(($prices->at($n)->[$LAST] - $prices->at($n - 1)->[$LAST])); | ||
| 80 | |||
| 81 | } | ||
| 82 | |||
| 83 | # Calculate the VHF | ||
| 84 | my $vhf = ($highest_close - $lowest_close) / $sum_of_one_day_price_change; | ||
| 85 | |||
| 86 | # Return the results | ||
| 87 | $indic->set($vhf_name, $i, $vhf); | ||
| 88 | } | ||
| 89 | |||
| 90 | 1; |