GeniusTrader/BackTest/Spool.pm untested

filter: Statementstmt 0.0% 0 / 116 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 208 CC 18 SCAR 27.1
CC
18
Cov
0%
get_portfolio
42.0
add_results
30.0
new
6.0
CRAP 15.1
1 package Finance::GeniusTrader::BackTest::Spool;
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 warnings;
9  
10 use vars qw(@ISA);
11  
12 use Finance::GeniusTrader::Tools qw(:conf);
13 use Finance::GeniusTrader::Serializable;
14 #ALL# use Log::Log4perl qw(:easy);
15  
16 @ISA = qw(Finance::GeniusTrader::Serializable);
17  
18 =head1 Finance::GeniusTrader::BackTest::Spool
19  
20 This modules provides some functions to manage a backtest directory.
21  
22 =head2 $spool = Finance::GeniusTrader::BackTest::Spool->new($data_directory);
23  
24 Create and initialize a BackTest::Spool object with a specific directory,
25 where backtest data are stored.
26  
27 =cut
28  
29 sub new {
30 my $type = shift;
31 my $class = ref($type) || $type;
32 my ($directory) = @_;
33  
34 my $self = { "directory" => $directory,
35 "cache" => 1,
36 "index_modified" => 0 };
37  
38 # Open the index file of the specified directory
39 if (-e "$directory/index") {
40 $self->{'index'} = Finance::GeniusTrader::BackTest::Spool::File->create_from_file("$directory/index");
41 } else {
42 $self->{'index'} = Finance::GeniusTrader::BackTest::Spool::File->new();
43 $self->{'index'}->store("$directory/index");
44 }
45
46 bless $self, $class;
47  
48 return $self;
49 }
50  
51 =head2 $spool->use_cache(0|1)
52  
53 Tell if data are cached before being written. Default to 1. In that
54 case you have to call $spool->sync from time to time to write data
55 on the disk.
56  
57 =cut
58 sub use_cache {
59 my ($self, $cache) = @_;
60 $self->{'cache'} = $cache;
61 $self->sync() if (! $cache);
62 }
63  
64 =head2 $spool->update_index()
65  
66 Force an update of the index data. Use that if a long time has elapsed since
67 the read and the index may have been updated.
68  
69 =cut
70 sub update_index {
71 my ($self) = @_;
72 my $dir = $self->{'directory'};
73 if ($self->{'index_modified'}) {
74 warn "You just lost modifications to the index of backtests ...";
75 }
76 $self->{'index'} = Finance::GeniusTrader::BackTest::Spool::File->create_from_file("$dir/index");
77 }
78  
79 =head2 $spool->add_alias_name($sysname, $alias);
80  
81 This function will link an alias and a system name.
82  
83 =cut
84 sub add_alias_name {
85 my ($self, $sysname, $alias) = @_;
86 my $index = $self->{'index'};
87  
88 $index->{'alias'}{$sysname} = $alias;
89 $self->{'index_modified'} = 1;
90 $self->sync() if (! $self->{'cache'});
91  
92 return;
93 }
94  
95 =head2 $spool->get_alias_name($sysname)
96  
97 Return the alias name of the system if it exists.
98  
99 =cut
100 sub get_alias_name {
101 my ($self, $sysname) = @_;
102 if (exists $self->{'index'}{'alias'}{$sysname}) {
103 return $self->{'index'}{'alias'}{$sysname};
104 }
105 return undef;
106 }
107  
108 =head2 $spool->add_results($sysname, $code, $stats, $portfolio, [$set]);
109  
110 This function will add new data or update old ones in the spooler.
111  
112 =cut
113  
114 sub add_results {
115 my ($self, $sysname, $code, $stats, $portfolio, $set) = @_;
116 my $directory = $self->{'directory'};
117 my $index = $self->{'index'};
118
119 # Make sure $set is already defined, overwise initilize it
120 if (! (defined($set) && $set)) {
121 my $n = 1;
122 while (-e "$directory/$code-$n.bkt") { $n++ }
123 $set = $n;
124 }
125
126 # Open the "$code-$set.bkt" file if required
127 if (! exists $self->{'bkt'}{"$code-$set"}) {
128 if (-e "$directory/$code-$set.bkt") {
129 $self->{'bkt'}{"$code-$set"} = Finance::GeniusTrader::BackTest::Spool::File->create_from_file("$directory/$code-$set.bkt");
130 } else {
131 $self->{'bkt'}{"$code-$set"} = Finance::GeniusTrader::BackTest::Spool::File->new();
132 }
133 }
134  
135 # Add $stats, $portfolio and $set values
136 $index->{'results'}{$sysname}{$code} = $stats;
137 $index->{'set'}{$sysname}{$code} = $set;
138 $self->{'index_modified'} = 1;
139 $self->{'bkt'}{"$code-$set"}{$sysname}{$code}{'portfolio'} = $portfolio;
140
141 # Store updated data into right files
142 $self->sync() if (! $self->{'cache'});
143 }
144  
145 =head2 $spool->sync()
146  
147 Write the cache on disk.
148  
149 =cut
150 sub sync {
151 my ($self) = @_;
152 my $dir = $self->{'directory'};
153 if ($self->{'index_modified'}) {
154 $self->{'index'}->store("$dir/index");
155 $self->{'index_modified'} = 0;
156 }
157 my @data = keys %{$self->{'bkt'}};
158 foreach my $file (@data) {
159 $self->{'bkt'}{$file}->store("$dir/$file.bkt");
160 delete $self->{'bkt'}{$file};
161 }
162 }
163  
164 =head2 $hash = $spool->list_available_data([$set]);
165  
166 This function will return a list of systems/codes available.
167 $hash->{$sysname} = [ list of codes ];
168  
169 =cut
170 sub list_available_data {
171 my ($self, $set) = @_;
172 my $index = $self->{'index'};
173 my %hash;
174  
175 foreach my $sysname (keys %{$index->{'results'}}) {
176 my @codes;
177 foreach my $code (keys %{$index->{'results'}{$sysname}}) {
178 if ((!(defined($set) and $set)) or
179 ($index->{'set'}{$sysname}{$code} eq $set)) {
180 push @codes, $code;
181 }
182 }
183 $hash{$sysname} = \@codes;
184 }
185 return \%hash;
186 }
187  
188 =head2 $spool->get_stats($sysname, $code);
189  
190 This function will return all stats available for a given $sysname and $code.
191  
192 =cut
193 sub get_stats {
194 my ($self, $sysname, $code) = @_;
195 my $index = $self->{'index'};
196  
197 if (exists $index->{'results'}{$sysname}{$code}) {
198 return $index->{'results'}{$sysname}{$code};
199 }
200 return undef;
201 }
202  
203 =head2 $spool->get_portfolio($sysname, $code);
204  
205 This function will return a portfolio for a given $sysname and $code.
206  
207 =cut
208 sub get_portfolio {
209 my ($self, $sysname, $code) = @_;
210 my $directory = $self->{'directory'};
211 my $index = $self->{'index'};
212  
213 # Get the full name of the system if we have an alias
214 if ($sysname !~ /\|/) {
215 my $alias = resolve_alias($sysname);
216 die "Alias unknown '$sysname'" if (! $alias);
217 $sysname = $alias;
218 }
219
220 my $set;
221 if (exists $index->{'set'}{$sysname} and
222 exists $index->{'set'}{$sysname}{$code})
223 {
224 $set = $index->{'set'}{$sysname}{$code};
225 } else {
226 warn "The data you're asking is not available in the spool.\n";
227 return undef;
228 }
229  
230 # Look in the cache first, otherwise load it from the file
231 if (exists $self->{'bkt'}{"$code-$set"}) {
232 return $self->{'bkt'}{"$code-$set"}{$sysname}{$code}{'portfolio'};
233 } elsif (-e "$directory/$code-$set.bkt") {
234 my $bkt = Finance::GeniusTrader::BackTest::Spool::File->create_from_file("$directory/$code-$set.bkt");
235 return $bkt->{$sysname}{$code}{'portfolio'};
236 }
237 return undef;
238 }
239  
240 package Finance::GeniusTrader::BackTest::Spool::File;
241  
242 use vars qw(@ISA);
243  
244 @ISA = qw(Finance::GeniusTrader::Serializable);
245  
246 sub new {
247 my $type = shift;
248 my $class = ref($type) || $type;
249  
250 my $self = { };
251 bless $self, $class;
252 return $self;
253 }
254  
255 1;