GeniusTrader/Analyzers/AvgGain.pm untested

filter: Statementstmt 0.0% 0 / 28 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 44 CC 5 SCAR 27.7
CC
5
Cov
0%
calculate
30.0
initialize
2.0
BEGIN
2.0
CRAP 16.0
1 package Finance::GeniusTrader::Analyzers::AvgGain;
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 = ("AvgGain[#*]");
17 @DEFAULT_ARGS = ("{A:CumGain}", "{A:Sum {A:IsGain}}");
18  
19 =head1 NAME
20  
21 Finance::GeniusTrader::Analyzers::AvgGain - Average Gain per trade
22  
23 =head1 DESCRIPTION
24  
25 This analyzer calculates the mean of the gains.
26  
27 =head2 Parameters
28  
29 First parameter: Cumulated Gain
30  
31 Second parameter: Number of Gains
32  
33 =cut
34  
35 sub initialize {
36 1;
37 }
38  
39 sub calculate {
40 my ($self, $calc, $last, $first, $portfolio) = @_;
41 my $name = $self->get_name;
42  
43 if ( !defined($portfolio) ) {
44 $portfolio = $calc->{'pf'};
45 }
46 if ( !defined($first) ) {
47 $first = $calc->{'first'};
48 }
49 if ( !defined($last) ) {
50 $last = $calc->{'last'};
51 }
52  
53 my $cum = $self->{'args'}->get_arg_values($calc, $last, 1);
54 my $nb = $self->{'args'}->get_arg_values($calc, $last, 2);
55 my $ret = 0;
56 $ret = $cum ** (1 / $nb) - 1 if ($nb != 0);
57  
58 $calc->indicators->set($name, $last, $ret);
59 }
60  
61 1;