| 1 | package Finance::GeniusTrader::Indicators::Wilders; | ||
| 2 | |||
| 3 | # Copyright 2005 Thomas Weigert | ||
| 4 | # Based on and for GeniusTrader (C) 2000-2002 Raphaƫl Hertzog, Fabien Fulhaber | ||
| 5 | # This file is distributed under the terms of the General Public License | ||
| 6 | # version 2 or (at your option) any later version. | ||
| 7 | |||
| 8 | # $Id: Wilders.pm,v 1.4 2009/07/09 16:28:46 ras Exp ras $ | ||
| 9 | |||
| 10 | # Standards-Version: 1.0 | ||
| 11 | |||
| 12 | use strict; | ||
| 13 | use vars qw(@ISA @NAMES @DEFAULT_ARGS); | ||
| 14 | |||
| 15 | use Finance::GeniusTrader::Indicators; | ||
| 16 | use Finance::GeniusTrader::Indicators::SMA; | ||
| 17 | |||
| 18 | @ISA = qw(Finance::GeniusTrader::Indicators); | ||
| 19 | @NAMES = ("Wilders[#*]"); | ||
| 20 | @DEFAULT_ARGS = (14, "{I:Prices CLOSE}"); | ||
| 21 | |||
| 22 | =head1 NAME | ||
| 23 | |||
| 24 | Finance::GeniusTrader::Indicators::Wilders - Wilder's smoothing (aka wells wilders moving average) | ||
| 25 | |||
| 26 | =head1 DESCRIPTION | ||
| 27 | |||
| 28 | Wilder's smoothing is using simple averages for the initial calculation. | ||
| 29 | For the subsequent average calculations, he drops 1/14th of the previous | ||
| 30 | average value and adds 1/nth of the new value. This is the "classic" | ||
| 31 | exponential average, in which the smoothing factor is 1/n, instead of the | ||
| 32 | "modern" exponential average, in which the smoothing factor is 2/(n+1). | ||
| 33 | |||
| 34 | Wilders(i) = (1/n) * Close(i) + (1 - 1/n) * Wilders(i-1) | ||
| 35 | |||
| 36 | |||
| 37 | =head2 Parameters | ||
| 38 | |||
| 39 | =over | ||
| 40 | |||
| 41 | =item Period (default 14) | ||
| 42 | |||
| 43 | The first argument is the period used to calculed the average. | ||
| 44 | |||
| 45 | =item Other data input | ||
| 46 | |||
| 47 | The second argument is optional. It can be used to specify an other | ||
| 48 | stream of input data for the average instead of the close prices. | ||
| 49 | This is usually an indicator, including I:Prices. | ||
| 50 | |||
| 51 | =back | ||
| 52 | |||
| 53 | =head2 Creation | ||
| 54 | |||
| 55 | Finance::GeniusTrader::Indicators::Wilders->new() | ||
| 56 | Finance::GeniusTrader::Indicators::Wilders->new([20]) | ||
| 57 | |||
| 58 | If you need a 30 days Wilders of the opening prices you can write | ||
| 59 | the following line: | ||
| 60 | |||
| 61 | Finance::GeniusTrader::Indicators::Wilders->new([30, "{I:Prices OPEN}"]) | ||
| 62 | |||
| 63 | A 10 days Wilders of the RSI could be created with : | ||
| 64 | |||
| 65 | Finance::GeniusTrader::Indicators::Wilders->new([10, "{I:RSI}"]) | ||
| 66 | |||
| 67 | =cut | ||
| 68 | |||
| 69 | sub initialize { | ||
| 70 | my ($self) = @_; | ||
| 71 | |||
| 72 | unless ( $self->{'args'}->is_constant(1) ) { | ||
| 73 | print STDERR __PACKAGE__ . ": error: Argument 1 \"" | ||
| 74 | . $self->{'args'}->get_arg_names(1) | ||
| 75 | . "\" must be a constant value."; | ||
| 76 | die "\n"; | ||
| 77 | } | ||
| 78 | |||
| 79 | $self->{'sma'} = Finance::GeniusTrader::Indicators::SMA->new([ $self->{'args'}->get_arg_names() ]); | ||
| 80 | $self->add_indicator_dependency($self->{'sma'}, 1); | ||
| 81 | $self->add_arg_dependency(2, $self->{'args'}->get_arg_names(1)); | ||
| 82 | } | ||
| 83 | |||
| 84 | sub calculate { | ||
| 85 | my ($self, $calc, $i) = @_; | ||
| 86 | my $indic = $calc->indicators; | ||
| 87 | my $name = $self->get_name; | ||
| 88 | my $before = 1; | ||
| 89 | |||
| 90 | my $nb = $self->{'args'}->get_arg_values($calc, $i, 1); | ||
| 91 | return if (! defined($nb) || $nb==0); | ||
| 92 | |||
| 93 | return if ($indic->is_available($name, $i)); | ||
| 94 | return if (! $self->check_dependencies($calc, $i)); | ||
| 95 | |||
| 96 | my $prev = $indic->get($name, $i - 1); | ||
| 97 | my $k = 1 / $nb; | ||
| 98 | my $wilders; | ||
| 99 | if (defined $prev) { | ||
| 100 | $wilders = $prev * (1 - $k) + $self->{'args'}->get_arg_values($calc, $i, 2) * $k; | ||
| 101 | } else { | ||
| 102 | $wilders = $indic->get($self->{'sma'}->get_name, $i); | ||
| 103 | $before = 0; | ||
| 104 | } | ||
| 105 | $indic->set($name, $i, $wilders); | ||
| 106 | } | ||
| 107 | |||
| 108 | sub calculate_interval { | ||
| 109 | my ($self, $calc, $first, $last) = @_; | ||
| 110 | my $indic = $calc->indicators; | ||
| 111 | my $name = $self->get_name; | ||
| 112 | |||
| 113 | my $nb = $self->{'args'}->get_arg_constant(1); | ||
| 114 | return if (! defined($nb) || $nb==0); | ||
| 115 | |||
| 116 | return if ($indic->is_available_interval($name, $first, $last)); | ||
| 117 | # Don't need to calculate all SMA values, just the first data point. | ||
| 118 | $self->{'sma'}->calculate($calc, $first); | ||
| 119 | |||
| 120 | return unless $self->dependencies_are_available($calc, $first); | ||
| 121 | |||
| 122 | my $k = 1 / $nb; | ||
| 123 | |||
| 124 | $indic->set($name, $first, $indic->get($self->{'sma'}->get_name, $first)); | ||
| 125 | |||
| 126 | for (my $i=$first+1;$i<=$last;$i++) { | ||
| 127 | my $prev = $indic->get($name, $i - 1); | ||
| 128 | my $wilders = $prev * (1 - $k) + $self->{'args'}->get_arg_values($calc, $i, 2) * $k; | ||
| 129 | $indic->set($name, $i, $wilders); | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | 1; |