GeniusTrader/CloseStrategy/Stop/VAR.pm untested

filter: Statementstmt 0.0% 0 / 92 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 127 CC 7 SCAR 17.6
CC
7
Cov
0%
get_indicative_long_stop
12.0
get_indicative_short_stop
12.0
long_position_opened
6.0
CRAP 5.8
1 package Finance::GeniusTrader::CloseStrategy::Stop::VAR;
2  
3 # Copyright 2000-2002 Raphaƫl Hertzog, Fabien Fulhaber
4 # Modified 2004 by Oliver Bossert
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 # Standards-Version: 1.0
9  
10 use strict;
11 use vars qw(@ISA @NAMES @DEFAULT_ARGS);
12  
13 use Finance::GeniusTrader::CloseStrategy;
14 use Finance::GeniusTrader::Prices;
15 use Finance::GeniusTrader::Indicators::StandardDeviation;
16  
17 @ISA = qw(Finance::GeniusTrader::CloseStrategy);
18 @NAMES = ("VAR[#1]");
19 @DEFAULT_ARGS = (10);
20  
21 =head1 Finance::GeniusTrader::CloseStrategy::Stop::VAR
22  
23 =head2 Overview
24  
25 This method uses market volatility and the concept of value at risk (VAR)
26 to help determine meaningful stop-loss prices and position limits for
27 trading securities.
28
29 =head2 References
30  
31 "Value At Risk And Technical Analysis" by Luis Ballesca-Loyo
32 Technical Analysis of Stocks and Commodities - August 1999
33  
34 =cut
35  
36 sub initialize {
37 my ($self) = @_;
38  
39 $self->{'sd'} = Finance::GeniusTrader::Indicators::StandardDeviation->new([ $self->{'args'}->get_arg_names(1) ]);
40 $self->add_indicator_dependency($self->{'sd'}, 1);
41 }
42  
43 sub get_indicative_long_stop {
44 my ($self, $calc, $i, $order, $pf_manager, $sys_manager) = @_;
45 my $stop = 0;
46  
47 my $nb = $self->{'args'}->get_arg_values($calc, $i, 1);
48 $self->remove_volatile_dependencies();
49 $self->add_volatile_prices_dependency( $nb );
50  
51 return 0 if (! $self->check_dependencies($calc, $i));
52 my $limit = $self->get_lower_limit($calc, $i);
53 if ($order->price)
54 {
55 return $order->price * $limit;
56 } else {
57 return $calc->prices->at($i)->[$LAST] * $limit;
58 }
59 }
60  
61 sub get_indicative_short_stop {
62 my ($self, $calc, $i, $order, $pf_manager, $sys_manager) = @_;
63 my $stop = 0;
64 my $nb = $self->{'args'}->get_arg_values($calc, $i, 1);
65 $self->remove_volatile_dependencies();
66 $self->add_volatile_prices_dependency( $nb );
67  
68 return 0 if (! $self->check_dependencies($calc, $i));
69 my $limit = $self->get_upper_limit($calc, $i);
70 if ($order->price)
71 {
72 return $order->price * $limit;
73 } else {
74 return $calc->prices->at($i)->[$LAST] * $limit;
75 }
76 }
77  
78 sub long_position_opened {
79 my ($self, $calc, $i, $position, $pf_manager, $sys_manager) = @_;
80 my $nb = $self->{'args'}->get_arg_values($calc, $i, 1);
81 $self->remove_volatile_dependencies();
82 $self->add_volatile_prices_dependency( $nb );
83
84 return if (! $self->check_dependencies($calc, $i));
85
86 my $lower_limit = $self->get_lower_limit($calc, $i);
87 $position->set_stop($position->open_price * $lower_limit);
88
89 return;
90 }
91  
92 sub get_lower_limit {
93 my ($self, $calc, $i) = @_;
94 my $prices = $calc->prices;
95 my $indic = $calc->indicators;
96 my $sd_name = $self->{'sd'}->get_name;
97 my $sum = 0;
98 for (my $j = 1; $j < $i; $j++) {
99
100 # Sum ln of (Today's Close / Yesterday's Close)
101 $sum += log($prices->at($j)->[$LAST] / $prices->at($j - 1)->[$LAST]);
102 }
103
104 my $mean = $sum / ($i - 1);
105 my $standard_deviation = $indic->get($sd_name, $i);
106  
107 # X is the confidence coefficient depending on the degree of certainty
108 # required. For 90 % certainty, X = 1.645, and for 95 % X = 1.96
109 my $X = 1.6448;
110
111 # Lower Threshold = Today's Close * e^(mean + X * standard deviation)
112 my $lower_threshold = $prices->at($i)->[$LAST] * exp($mean - $X * $standard_deviation / 100);
113
114 # In a normal distribution, 95 % of the data will be above :
115 my $lower_limit = $lower_threshold / $prices->at($i)->[$LAST];
116 return $lower_limit;
117 }
118  
119 sub short_position_opened {
120 my ($self, $calc, $i, $position, $pf_manager, $sys_manager) = @_;
121
122 return if (! $self->check_dependencies($calc, $i));
123
124 my $upper_limit = $self->get_upper_limit($calc, $i);
125 $position->set_stop($position->open_price * $upper_limit);
126  
127 return;
128 }
129  
130 sub get_upper_limit {
131 my ($self, $calc, $i) = @_;
132 my $prices = $calc->prices;
133 my $indic = $calc->indicators;
134 my $sd_name = $self->{'sd'}->get_name;
135 my $sum = 0;
136 for (my $j = 1; $j < $i; $j++) {
137
138 # Sum ln of (Today's Close / Yesterday's Close)
139 $sum += log($prices->at($j)->[$LAST] / $prices->at($j - 1)->[$LAST]);
140 }
141
142 my $mean = $sum / ($i - 1);
143 my $standard_deviation = $indic->get($sd_name, $i);
144  
145 # X is the confidence coefficient depending on the degree of certainty
146 # required. For 90 % certainty, X = 1.645, and for 95 % X = 1.96
147 my $X = 1.6448;
148
149 # Lower Threshold = Today's Close * e^(mean + X * standard deviation)
150 my $upper_threshold = $prices->at($i)->[$LAST] * exp($mean + $X * $standard_deviation / 100);
151
152 # In a normal distribution, 95 % of the data will be above :
153 my $upper_limit = $upper_threshold / $prices->at($i)->[$LAST];
154 return $upper_limit;
155 }
156  
157 sub manage_long_position {
158 my ($self, $calc, $i, $position, $pf_manager, $sys_manager) = @_;
159  
160 return;
161 }
162  
163 sub manage_short_position {
164 my ($self, $calc, $i, $position, $ps_manager, $sys_manager) = @_;
165
166 return;
167 }
168