GeniusTrader/Analyzers/R4.pm untested

filter: Statementstmt 0.0% 0 / 51 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 76 CC 7 SCAR 35.1
CC
7
Cov
0%
calculate
56.0
initialize
2.0
BEGIN
2.0
CRAP 33.5
1 package Finance::GeniusTrader::Analyzers::R4;
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 = ("R4[#*]");
17 @DEFAULT_ARGS = ("{A:WinRatio}", "{A:AvgNZ {A:Gain}}", "{A:AvgNZ {A:Losses}}", "{A:InitSum}" );
18  
19 =head1 NAME
20  
21 Finance::GeniusTrader::Analyzers::AvgCosts - Average Costs per trade
22  
23 =head1 DESCRIPTION
24  
25 The mean performance of the portfolio
26  
27 =head2 Parameters
28  
29 none
30  
31 =cut
32  
33 sub initialize {
34 1;
35 }
36  
37 sub calculate {
38 my ($self, $calc, $last, $first, $portfolio) = @_;
39 my $name = $self->get_name;
40  
41 if ( !defined($portfolio) ) {
42 $portfolio = $calc->{'pf'};
43 }
44 if ( !defined($first) ) {
45 $first = $calc->{'first'};
46 }
47 if ( !defined($last) ) {
48 $last = $calc->{'last'};
49 }
50  
51 # Calculate Vince's "R4" (Risk Of Ruin)
52 #
53 # Example on system TFS with Alcatel :
54 # There is a 19.2 % probability of the account falling 40 % below the start
55 # equity (10 000 EUR) before it rises above 20 000 EUR.
56  
57 # Probability of a win
58 my $PW = $self->{'args'}->get_arg_values($calc, $last, 1);
59  
60 # Average winning trade
61 my $AW = $self->{'args'}->get_arg_values($calc, $last, 2);
62  
63 # Average losing trade
64 my $AL = $self->{'args'}->get_arg_values($calc, $last, 3);
65  
66 # Size of starting account
67 my $Q = $self->{'args'}->get_arg_values($calc, $last, 4);;
68  
69 # Quit trading and celebrate if account reaches this
70 my $L = $Q * 2;
71  
72 # Drawdown to start equity that constitutes "ruin"
73 my $G = 0.40;
74  
75 # Initialize the risk of ruin to 100 % before we calculate it, so that
76 # the ratio is already set up if we have no winning trades...
77 my $R4 = 1.0;
78
79 # Calculate the risk or ruin only if we have some winning trades and
80 # losing ones !
81 if (($AW != 0) and ($AL != 0)) {
82 my $a = sqrt( ($PW * ($AW / $Q) * ($AW / $Q)) + ((1.0 - $PW) * ($AL / $Q) * ($AL / $Q)) );
83 my $z = (abs($AW / $Q) * $PW) - (abs($AL / $Q) * (1.0 - $PW));
84 my $p = 0.5 * (1.0 + ($z / $a));
85 my $U = $G / $a;
86 my $c = (($L - ((1.0 - $G) * $Q)) / $Q) / $a;
87 my $temp1 = exp($U * log((1.0 - $p) / $p));
88 my $temp2 = exp($c * log((1.0 - $p) / $p));
89 $R4 = (($temp2 - 1.0) != 0) ? 1.0 - (($temp1 - 1.0) / ($temp2 - 1.0)) : 0;
90 }
91  
92 # Set up the risk of ruin to 0 % if we don't have losing trades !
93 $R4 = 0 if ($AL eq 0);
94  
95 print STDERR $Q . " - " . $PW . "\n";
96  
97 $calc->indicators->set($name, $last, $R4);
98 }
99  
100 1;