GeniusTrader/MoneyManagement/VAR.pm untested

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 93 CC 8 SCAR 27.8
CC
8
Cov
0%
manage_quantity
30.0
new
20.0
initialize
2.0
CRAP 16.1
1 package Finance::GeniusTrader::MoneyManagement::VAR;
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(@NAMES @ISA);
9  
10 use Finance::GeniusTrader::MoneyManagement;
11 use Finance::GeniusTrader::Indicators::StandardDeviation;
12 use Finance::GeniusTrader::Prices;
13  
14 @NAMES = ("VAR[#1,#2]");
15 @ISA = qw(Finance::GeniusTrader::MoneyManagement);
16  
17 =head1 Finance::GeniusTrader::MoneyManagement::VAR
18  
19 =head2 Overview
20  
21 This method uses market volatility and the concept of value at risk (VAR)
22 to help determine meaningful stop-loss prices and position limits for
23 trading securities.
24  
25 =head2 References
26  
27 "Value At Risk And Technical Analysis" by Luis Ballesca-Loyo
28 Technical Analysis of Stocks and Commodities - August 1999
29  
30 =cut
31  
32 sub new {
33 my $type = shift;
34 my $class = ref($type) || $type;
35 my $args = shift;
36
37 my $self = { 'args' => defined($args) ? $args : [10,2] };
38  
39 $args->[0] = 10 if (! defined($args->[0]));
40 $args->[1] = 2 if (! defined($args->[1]));
41
42 return manage_object(\@NAMES, $self, $class, $self->{'args'}, '');
43 }
44  
45 sub initialize {
46 my $self = shift;
47  
48 $self->{'sd'} = Finance::GeniusTrader::Indicators::StandardDeviation->new([ $self->{'args'}[0] ]);
49  
50 $self->add_indicator_dependency($self->{'sd'}, 1);
51 $self->add_prices_dependency($self->{'args'}[0]);
52 }
53  
54 sub manage_quantity {
55 my ($self, $order, $i, $calc, $portfolio) = @_;
56 my $prices = $calc->prices;
57 my $period = $prices->count();
58 my $indic = $calc->indicators;
59 my $sd = $self->{'sd'};
60 my $sd_name = $sd->get_name;
61 my $maximum_percentage_of_portfolio_to_lose_per_trade = $self->{'args'}[1];
62 my $sum = 0;
63
64 return if (! $self->check_dependencies($calc, $i));
65
66 for (my $j = 1; $j < $i; $j++) {
67  
68 # Sum ln of (Today's Close / Yesterday's Close)
69 $sum += log($prices->at($j)->[$LAST] / $prices->at($j - 1)->[$LAST]);
70  
71 }
72  
73 my $mean = $sum / ($period - 1);
74 my $standard_deviation = $indic->get($sd_name, $i);
75  
76 # X is the confidence coefficient depending on the degree of certainty
77 # required. For 90 % certainty, X = 1.645, and for 95 % X = 1.96
78 # We should be able to get the confidence level as an input value and
79 # calculate automatically the X confidence coefficient, but let's try
80 # your money management strategy without it with a 90 % confidence :
81 my $X = 1.6448;
82
83 # Upper Threshold = Today's Close * e^(mean + X * standard deviation)
84 my $upper_threshold = $prices->at($i)->[$LAST] * exp($mean + $X * $standard_deviation / 100);
85
86 # Lower Threshold = Today's Close * e^(mean + X * standard deviation)
87 my $lower_threshold = $prices->at($i)->[$LAST] * exp($mean - $X * $standard_deviation / 100);
88  
89 # In a normal distribution, 90 % of the data and hence of the
90 # probability lies in the range :
91 my $upper_limit = $upper_threshold / $prices->at($i)->[$LAST] - 1;
92 my $lower_limit = $lower_threshold / $prices->at($i)->[$LAST] - 1;
93
94 # Initialization of portfolio value
95 my $cash = $portfolio->current_cash;
96 my $positions = $portfolio->current_evaluation;
97 my $upcoming_gains_or_losses = $portfolio->current_marged_gains;
98 my $portfolio_value = $cash + $positions + $upcoming_gains_or_losses;
99  
100 if ($portfolio_value > 0) {
101  
102 # Calculate the quantity to buy for a long trade
103 if ($order->{'order'} eq "B") {
104  
105 return int( - $portfolio_value * $maximum_percentage_of_portfolio_to_lose_per_trade / 100 / $lower_limit / $prices->at($i)->[$LAST] );
106 }
107  
108 # Calculate the quantity to sell for a short trade
109 if ($order->{'order'} eq "S") {
110
111 return int( $portfolio_value * $maximum_percentage_of_portfolio_to_lose_per_trade / 100 / $upper_limit / $prices->at($i)->[$LAST] );
112 }
113 } else {
114 return 0;
115 }
116 }
117  
118 1;