« GeniusTrader/MoneyManagement/Portfolio/FixedFractional.pm
↑ Summary
GeniusTrader/Analyzers/GrossGain.pm »
| 1 | package Finance::GeniusTrader::Analyzers::AvgNZ; | ||
| 2 | |||
| 3 | # Copyright 2003 Oliver Bossert | ||
| 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 | # Standards-Version: 1.0 | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use vars qw(@ISA @NAMES @DEFAULT_ARGS); | ||
| 11 | |||
| 12 | use Finance::GeniusTrader::Analyzers; | ||
| 13 | use Finance::GeniusTrader::Calculator; | ||
| 14 | |||
| 15 | @ISA = qw(Finance::GeniusTrader::Analyzers); | ||
| 16 | @NAMES = ("AvgNZ[#*]"); | ||
| 17 | @DEFAULT_ARGS = ("{A:Costs}"); | ||
| 18 | |||
| 19 | =head1 NAME | ||
| 20 | |||
| 21 | Finance::GeniusTrader::Analyzers::AvgNZ - Calculates the Average | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | Calculates the Average of Arg1, but only of the days where Arg1 is | ||
| 26 | non-zero. | ||
| 27 | |||
| 28 | =head2 Parameters | ||
| 29 | |||
| 30 | First argument: Array reference to be averaged | ||
| 31 | |||
| 32 | =cut | ||
| 33 | |||
| 34 | sub initialize { | ||
| 35 | 1; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub calculate { | ||
| 39 | my ($self, $calc, $last, $first, $portfolio) = @_; | ||
| 40 | my $name = $self->get_name; | ||
| 41 | |||
| 42 | if ( !defined($portfolio) ) { | ||
| 43 | $portfolio = $calc->{'pf'}; | ||
| 44 | } | ||
| 45 | if ( !defined($first) ) { | ||
| 46 | $first = $calc->{'first'}; | ||
| 47 | } | ||
| 48 | if ( !defined($last) ) { | ||
| 49 | $last = $calc->{'last'}; | ||
| 50 | } | ||
| 51 | |||
| 52 | if ( defined($portfolio) ) { | ||
| 53 | $self->{'portfolio'} = $portfolio; | ||
| 54 | } | ||
| 55 | |||
| 56 | my $array = $self->{'args'}->get_arg_values($calc, $last, 1); | ||
| 57 | |||
| 58 | my $sum = 0; | ||
| 59 | my $anz = 0; | ||
| 60 | foreach my $f (@{$array}) | ||
| 61 | { | ||
| 62 | if ( $f != 0 ) { | ||
| 63 | $sum += $f; | ||
| 64 | $anz++; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | $sum = $sum / $anz if ($anz != 0); | ||
| 68 | |||
| 69 | $calc->indicators->set($name, $last, $sum); | ||
| 70 | } | ||
| 71 | |||
| 72 | 1; |