| 1 | package Finance::GeniusTrader::Indicators::ZigZag; | ||
| 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 | |||
| 12 | @ISA = qw(Finance::GeniusTrader::Indicators); | ||
| 13 | @NAMES = ("ZigZag[#*]"); | ||
| 14 | @DEFAULT_ARGS = (10, "{I:Prices CLOSE}"); | ||
| 15 | |||
| 16 | =head1 NAME | ||
| 17 | |||
| 18 | Finance::GeniusTrader::Indicators::ZigZag | ||
| 19 | |||
| 20 | =head1 DESCRIPTION | ||
| 21 | |||
| 22 | The Zig Zag indicator filters out changes in an underlying plot (e.g., a security's price or another indicator) that are less than a specified amount. The Zig Zag indicator only shows significant changes. | ||
| 23 | |||
| 24 | =head2 Parameters | ||
| 25 | |||
| 26 | =over | ||
| 27 | |||
| 28 | =item Percentage change (10%) | ||
| 29 | |||
| 30 | The first argument is the percentage change required to yield a line that only reverses after a change from high to low of 10% or greater. | ||
| 31 | |||
| 32 | =back | ||
| 33 | |||
| 34 | =head2 Creation | ||
| 35 | |||
| 36 | Finance::GeniusTrader::Indicators::ZigZag->new() | ||
| 37 | Finance::GeniusTrader::Indicators::ZigZag->new([5]) | ||
| 38 | |||
| 39 | If you need an 8 % ZigZag indicator of the opening prices you can write | ||
| 40 | one of those lines : | ||
| 41 | |||
| 42 | Finance::GeniusTrader::Indicators::SMA->new([8], "OPEN", $GET_OPEN) | ||
| 43 | Finance::GeniusTrader::Indicators::SMA->new([8, "OPEN"]) | ||
| 44 | |||
| 45 | A ZigZag indicator with a 20 % threshold of the Volume could be created with : | ||
| 46 | |||
| 47 | Finance::GeniusTrader::Indicators::ZigZag->new([20, "{I:Volume}"]) | ||
| 48 | |||
| 49 | =cut | ||
| 50 | |||
| 51 | sub initialize { | ||
| 52 | my ($self) = @_; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub calculate { | ||
| 56 | my ($self, $calc, $i) = @_; | ||
| 57 | my $percentage = $self->{'args'}->get_arg_values($calc, $i, 1); | ||
| 58 | my $name = $self->get_name; | ||
| 59 | |||
| 60 | return if (! defined($percentage)); | ||
| 61 | |||
| 62 | $self->remove_volatile_dependencies(); | ||
| 63 | $self->add_volatile_arg_dependency(2, $percentage); | ||
| 64 | return if (! $self->check_dependencies($calc, $i)); | ||
| 65 | |||
| 66 | my $previous_peak = 0; | ||
| 67 | my $zigzag = 0; | ||
| 68 | my $date = 0; | ||
| 69 | my $sign = 0; | ||
| 70 | |||
| 71 | for (my $n = 0; $n <= $i; $n++) { | ||
| 72 | |||
| 73 | if ($self->{'args'}->get_arg_values($calc, $n, 2) >= $previous_peak * (1 + $percentage / 100)) { | ||
| 74 | if ($sign eq -1) { | ||
| 75 | $calc->indicators()->set($name, $date, $previous_peak); | ||
| 76 | } | ||
| 77 | $date = $n; | ||
| 78 | $sign = 1; | ||
| 79 | } | ||
| 80 | if ($self->{'args'}->get_arg_values($calc, $n, 2) <= $previous_peak * (1 - $percentage / 100)) { | ||
| 81 | if ($sign eq 1) { | ||
| 82 | $calc->indicators()->set($name, $date, $previous_peak); | ||
| 83 | } | ||
| 84 | $date = $n; | ||
| 85 | $sign = -1; | ||
| 86 | } | ||
| 87 | if (($sign eq 1) and ($self->{'args'}->get_arg_values($calc, $n, 2) > $previous_peak)) { | ||
| 88 | $previous_peak = $self->{'args'}->get_arg_values($calc, $n, 2); | ||
| 89 | $date = $n; | ||
| 90 | } | ||
| 91 | if (($sign eq -1) and ($self->{'args'}->get_arg_values($calc, $n, 2) < $previous_peak)) { | ||
| 92 | $previous_peak = $self->{'args'}->get_arg_values($calc, $n, 2); | ||
| 93 | $date = $n; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | $calc->indicators()->set($name, $i, $previous_peak); | ||
| 97 | } | ||
| 98 | |||
| 99 | sub calculate_interval { | ||
| 100 | my ($self, $calc, $first, $last) = @_; | ||
| 101 | |||
| 102 | $self->calculate($calc, $last); | ||
| 103 | } | ||
| 104 | |||
| 105 | 1; |