GeniusTrader/DB/MetaStockReader.pm untested

filter: Statementstmt 0.0% 0 / 189 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 293 CC 19 SCAR 32.5
CC
19
Cov
0%
initialize
56.0
get_prices
56.0
read_master
12.0
CRAP 25.8
1 package Finance::GeniusTrader::DB::MetaStockReader;
2 # Copyright 2003-2005 Yannick Tournedouet
3 # This file is distributed under the terms of the General Public License
4 # version 2 or (at your option) any later version.
5  
6 # v1.0 : Initial version
7 # v1.1 29/07/2004 : Function "initialize" read now all the MASTER and XMASTER files find in the directory and the sub directory
8 # : Use ref for the hash
9 # : Use "/" instead of "\\" for a better compatiblity with system UNIX
10 # v1.2 30/07/2004 : use hash of hash for the security list for a better performance.
11 # : bug 1.1 fixed -> XMASTER read MASTER file
12 # v1.3 27/08/2004 : fixed bug in the read_xmaster and read_master method when the code is the not ISIN code
13 # v1.4 23/05/2005 : fixed a bug in the coding float number format.
14 # v1.5 27/05/2005 : fixed a warning with the 'pack' function when a Byte was > 255.
15 # : test if security already exists before putting in the table.
16 # v1.6 04/06/2005 : Changed the DB object interface to allow better timeframe support (João Costa)
17  
18 # ras 9mar08 : pod explaining Finance::GeniusTrader::DB::MetaStockReader and Finance::GeniusTrader::DB::MetaStock usage
19  
20 use strict;
21 use vars qw(@ISA);
22  
23 use Finance::GeniusTrader::DB;
24 use Finance::GeniusTrader::Prices;
25 use Finance::GeniusTrader::Conf;
26 use Finance::GeniusTrader::DateTime;
27  
28 @ISA = qw(Finance::GeniusTrader::DB);
29  
30 =head1 DB::MetaStockReader access module
31  
32 =head2 Overview
33  
34 The MetaStockReader access module is able to retrieve quotes from almost any
35 type of MetaStock database.
36  
37 This module does not require any other code
38 to support its operation and it is not intended to be used with
39 Finance::GeniusTrader::DB::MetaStock and, although named MetaStockReader, it is not
40 the companion program required by Finance::GeniusTrader::DB::MetaStock.
41  
42 =head2 Synopsis
43  
44 my $db = create_standard_object("DB::" . Finance::GeniusTrader::Conf::get("DB::module"));
45 $db->initialize;
46 $db->get_prices("FR0000130007");
47 $db->disconnect;
48 or
49 my $db = create_standard_object("DB::" . Finance::GeniusTrader::Conf::get("DB::module"));
50 $db->get_prices("FR0000130007");
51 $db->disconnect;
52  
53 $db->initialize is used to initialize the isin code list.
54 Function "get_prices" first test if the isin code is initiasize, if not
55 it call the function "initialize".
56  
57 =head2 Note
58  
59 This module read the MASTER and the XMASTER file of you security directory
60 to get quotes, with a directory and a symbol as main parameters.
61 The MASTER file contain only the 255 first file (*.DAT) security of your
62 directory.
63 The XMASTER file all the others security (*.MWD) of your directory.
64  
65 =head2 Configuration
66  
67 NOTE: this module supercedes the module Finance::GeniusTrader::DB::MetaStock. do not attempt
68 to use both.
69  
70 You can indicate the directory which contains the MetaStock database
71 by setting the DB::metastock::directory configuration item.
72  
73 =head2 new()
74  
75 Create a new DB object used to retry quotes from a MetaStock database.
76  
77 =cut
78 sub new {
79  
80 my $type = shift;
81 my $class = ref($type) || $type;
82  
83 Finance::GeniusTrader::Conf::default("DB::metastock::directory", "");
84  
85 my $self = { "directory" => Finance::GeniusTrader::Conf::get("DB::metastock::directory"),
86 "codes" => undef};
87  
88 return bless $self, $class;
89 }
90  
91 =head2 $db->disconnect
92  
93 Disconnects from the database.
94  
95 =cut
96 sub disconnect {
97 my $self = shift;
98 }
99  
100 =head2 $db->initialize
101  
102 Construct the list of isin code.
103  
104 =cut
105 sub initialize {
106 my ($self,$directory) = @_;
107 my $file;
108  
109 if (! defined $directory) {
110 $directory = $self->{'directory'};
111 }
112  
113 opendir (DIR,$directory) or die "Can't open the current directory $directory";
114  
115 foreach $file (readdir DIR) {
116  
117 # is a directory
118 if (-d "$directory/$file") {
119  
120 if (($file ne ".") && ($file ne "..")) {
121  
122 $self->initialize("$directory/$file");
123 }
124  
125 # is a file
126 } elsif (-f "$directory/$file") {
127  
128 if ($file eq "MASTER") {
129  
130 $self->read_master($directory);
131  
132 } elsif ($file eq "XMASTER") {
133  
134 $self->read_xmaster($directory);
135 }
136 }
137 }
138  
139 close(DIR);
140 }
141  
142 =head2 $db->set_directory("/new/directory")
143  
144 Indicate the directory containing your equity.
145  
146 =cut
147 sub set_directory {
148 my ($self, $directory) = @_;
149 $self->{'directory'} = $directory;
150 }
151  
152  
153 =head2 $db->read_master
154  
155 Read the MASTER file of your directory containing your equity.
156  
157 =cut
158 sub read_master {
159  
160 my($self,$dir) = @_;
161  
162 undef my @nom_titre;
163 undef my @code;
164 my %enregistrement = ();
165 my $file = $dir . "/MASTER";
166  
167 if (!(-e $file)) {
168 return;
169 }
170  
171 open(MASTER,"<$file") or die "Can't open file $file\n";
172 binmode(MASTER);
173 my $unpack = "S S A49";
174 my $Index = 0;
175 my ($NombreValeur,$Padding,$IndexValeurMax,$Record);
176 read(MASTER,$Record,53) || die "Can't read a block in the file MASTER";
177  
178 ($NombreValeur,$IndexValeurMax,$Padding) = unpack($unpack,$Record);
179 $unpack = "C s C C s A16 C A C C C C C C C C A S A14 C A C";
180 my ($NumeroFichier,$TypeFichier,$TailleEnregistrement,
181 $NumeroChamp,$NomValeur,$CTFlag,$DateDebut00,$DateDebut01,
182 $DateDebut02,$DateDebut03,$DateFin00,$DateFin01,
183 $DateFin02,$DateFin03,$Temps,$IDA,$ISIN,$Flag);
184  
185 while ($Index++ < $NombreValeur) {
186  
187 read(MASTER,$Record,53) || die "Can't read a block in the file MASTER";
188 ($NumeroFichier,$TypeFichier,$TailleEnregistrement,
189 $NumeroChamp,$Padding,$NomValeur,$Padding,$CTFlag,
190 $DateDebut00,$DateDebut01,$DateDebut02,$DateDebut03,
191 $DateFin00,$DateFin01,$DateFin02,$DateFin03,
192 $Temps,$IDA,$ISIN,$Padding,$Flag,$Padding) = unpack($unpack,$Record);
193  
194 $enregistrement{'DIR'} = $dir;
195 $enregistrement{'FILE'} = $NumeroFichier;
196 $enregistrement{'TAILLE'} = $TailleEnregistrement;
197 @code = split(/\x00/,$ISIN);
198 @nom_titre = split(/\x00/,$NomValeur);
199 $enregistrement{'ISIN'} = $code[0];
200 $enregistrement{'NOM'} = $nom_titre[0];
201 $enregistrement{'DATE_DEBUT'} = $self->convertFloat($DateDebut00,$DateDebut01,$DateDebut02,$DateDebut03);
202 $enregistrement{'DATE_FIN'} = $self->convertFloat($DateFin00,$DateFin01,$DateFin02,$DateFin03);
203 if (! exists ($self->{'codes'}{$ISIN})) {
204 $self->{'codes'}{$ISIN} = {%enregistrement};
205 }
206 }
207  
208 close(MASTER);
209 }
210  
211  
212 =head2 $db->read_xmaster
213  
214 Read the XMASTER file of your directory containing your equity.
215  
216 =cut
217 sub read_xmaster {
218  
219 my($self,$dir) = @_;
220  
221 my %enregistrement = ();
222 my @nom_titre;
223 my @code;
224 my $file = $dir . "/XMASTER";
225  
226 if (!(-e $file)) {
227 return;
228 }
229  
230 open(XMASTER,"<$file") or die "Can't open file $file\n";
231 binmode(XMASTER);
232 my $unpack = "A10 C C A6 C C A130";
233 my $Index = 0;
234 my $NombreFichier = 0;
235 my ($NombreFichierLow,$NombreFichierHigh,$NumeroFichierMaxLow,$NumeroFichierMaxHigh);
236 my ($NumeroFichierLow,$NumeroFichierHigh,$NomValeur,$ISIN,$Padding,$Record);
237 read(XMASTER,$Record,150) || die "Can't read a block in the file XMASTER";
238 ($Padding,$NombreFichierLow,$NombreFichierHigh,$Padding,$NumeroFichierMaxLow,$NumeroFichierMaxHigh,$Padding) = unpack($unpack,$Record);
239 $NombreFichier = $NombreFichierLow + ($NombreFichierHigh << 8);
240 $unpack = "C A15 A22 A27 C C A83";
241  
242 while ($Index++ < $NombreFichier) {
243  
244 read(XMASTER,$Record,150) || die "Can't read a block in the file XMASTER";
245 ($Padding,$ISIN,$NomValeur,$Padding,$NumeroFichierLow,$NumeroFichierHigh,$Padding) = unpack($unpack,$Record);
246  
247 $enregistrement{'DIR'} = $dir;
248 $enregistrement{'FILE'} = $NumeroFichierLow + ($NumeroFichierHigh << 8);
249 $enregistrement{'TAILLE'} = 0;
250 @code = split(/\x00/,$ISIN);
251 @nom_titre = split(/\x00/,$NomValeur);
252 $enregistrement{'ISIN'} = $code[0];
253 $enregistrement{'NOM'} = $nom_titre[0];
254  
255 $enregistrement{'DATE_DEBUT'} = 0;
256 $enregistrement{'DATE_FIN'} = 0;
257 if (! exists ($self->{'codes'}{$ISIN})) {
258 $self->{'codes'}{$enregistrement{'ISIN'}} = {%enregistrement};
259 }
260 }
261  
262 close(XMASTER);
263 }
264  
265 =head2 $db->find_isin($code)
266  
267 Return the description for the symbol $code.
268  
269 =cut
270 sub find_isin {
271  
272 my($self, $ISIN) = @_;
273 my %retour = ();
274 my $hash = \%{$self->{'codes'}};
275  
276 if (exists ($hash->{$ISIN})) {
277  
278 %retour = %{$hash->{$ISIN}};
279 }
280  
281 return (\%retour);
282 }
283  
284 =head2 $db->get_db_name($code)
285  
286 Return the name for the symbol $code.
287  
288 =cut
289 sub get_db_name {
290  
291 my($self, $isin) = @_;
292 my $equity = $self->find_isin($isin);
293 return $equity->{'NOM'};
294 }
295  
296 =head2 $db->get_prices($code, $timeframe)
297  
298 Returns a Finance::GeniusTrader::Prices object containing all known prices for the symbol
299 $code.
300  
301 =cut
302 sub get_prices {
303  
304 my($self, $isin, $timeframe) = @_;
305 $timeframe = $DAY unless ($timeframe);
306 die "Intraday support not implemented in DB::MetaStockReader" if ($timeframe < $DAY);
307 return Finance::GeniusTrader::Prices->new() if ($timeframe > $DAY);
308 my $reg;
309 my $file;
310 my $unpack = "S S A24";
311 my $Index = 0;
312 undef my @tab;
313 my $extension;
314 my ($MaxRec,$LastRec,$Record);
315 my ($Date00,$Date01,$Date02,$Date03,
316 $Ouverture00,$Ouverture01,$Ouverture02,$Ouverture03,
317 $PlusHaut00,$PlusHaut01,$PlusHaut02,$PlusHaut03,
318 $PlusBas00,$PlusBas01,$PlusBas02,$PlusBas03,
319 $Fermeture00,$Fermeture01,$Fermeture02,$Fermeture03,
320 $Volume00,$Volume01,$Volume02,$Volume03,
321 $OpInt00,$OpInt01,$OpInt02,$OpInt03);
322  
323 my ($open, $high, $low, $close, $volume, $date, $time);
324 my ($year, $month, $day);
325  
326 if (!defined $self->{'codes'}) {
327  
328 $self->initialize($self->{'directory'});
329 }
330  
331 my $prices = Finance::GeniusTrader::Prices->new();
332 $prices->set_timeframe($timeframe);
333  
334 $reg = $self->find_isin($isin);
335  
336 if (!defined $reg->{'FILE'}) {
337 die ("Can't find $isin security\n");
338 exit;
339 }
340  
341 if ($reg->{'FILE'} > 255) {
342 $extension = ".MWD";
343 } else {
344 $extension = ".DAT";
345 }
346  
347 $file = $reg->{'DIR'} . "/F" .$reg->{'FILE'} . $extension;
348  
349 open(DATA,"<$file") or die "Can't open the file $file\n";
350 binmode(DATA);
351  
352 read(DATA,$Record,28) == 28 || die "Can't read a block";
353 ($MaxRec,$LastRec) = unpack($unpack,$Record);
354 $unpack = "C C C C C C C C C C C C C C C C C C C C C C C C C C C C";
355  
356 while ($Index < $LastRec-1) {
357  
358 read(DATA,$Record,28) == 28 || die "Can't read a block";
359 ($Date00,$Date01,$Date02,$Date03,
360 $Ouverture00,$Ouverture01,$Ouverture02,$Ouverture03,
361 $PlusHaut00,$PlusHaut01,$PlusHaut02,$PlusHaut03,
362 $PlusBas00,$PlusBas01,$PlusBas02,$PlusBas03,
363 $Fermeture00,$Fermeture01,$Fermeture02,$Fermeture03,
364 $Volume00,$Volume01,$Volume02,$Volume03,
365 $OpInt00,$OpInt01,$OpInt02,$OpInt03) = unpack($unpack,$Record);
366  
367 $date = $self->convertFloat($Date00,$Date01,$Date02,$Date03);
368 $open = $self->convertFloat($Ouverture00,$Ouverture01,$Ouverture02,$Ouverture03);
369 $high = $self->convertFloat($PlusHaut00,$PlusHaut01,$PlusHaut02,$PlusHaut03);
370 $low = $self->convertFloat($PlusBas00,$PlusBas01,$PlusBas02,$PlusBas03);
371 $close = $self->convertFloat($Fermeture00,$Fermeture01,$Fermeture02,$Fermeture03);
372 $volume = $self->convertFloat($Volume00,$Volume01,$Volume02,$Volume03);
373 $date += 19000000;
374 $date =~ /^(\d{4})(\d{2})(\d{2})/;
375 $date = $1 . "-" . $2 . "-" .$3;
376 $prices->add_prices([ $open, $high, $low, $close, $volume, $date ]);
377 $Index++;
378 }
379  
380 close(DATA);
381  
382 return $prices;
383 }
384  
385  
386 =head2 $db->puissance($value00,$value01,$value02,$value03)
387  
388 Convert a MSBIN format to a float perl format (4 bytes).
389 It convert first to a IEEE float format (4 bytes).
390  
391 =cut
392 sub convertFloat {
393  
394 my($self,$value00,$value01,$value02,$value03) = @_;
395  
396 my $signe = 0x00;
397 my $exp = 0x00;
398 my $resultat = 0;
399 my $virgule = 0;
400  
401 my $resultat00 = $value00;
402 my $resultat01 = $value01;
403 my $resultat02 = 0x00;
404 my $resultat03 = 0x00;
405  
406 $signe = $value02 & 0x80;
407 $resultat03 |= $signe;
408 $exp = $value03 - 2;
409 $resultat03 |= ($exp >> 1) & 0xff;
410 $resultat02 |= ($exp << 7) & 0xff;
411 $resultat02 |= ($value02 & 0x7F);
412 $resultat = pack("CCCC",$resultat00,$resultat01,$resultat02,$resultat03);
413 $resultat = unpack("f",$resultat);
414 return $resultat;
415 }
416  
417 =head2 $db->get_last_prices($code, $limit, $timeframe)
418  
419 NOT YET SUPPORTED for MetaStockReader module.
420  
421 Returns a Finance::GeniusTrader::Prices object containing the $limit last known prices for
422 the symbol $code.
423  
424 =cut
425 sub get_last_prices {
426 my ($self, $code, $limit, $timeframe) = @_;
427  
428 return get_prices($self, $code, $timeframe) if ($limit==-1);
429 die "get_last_prices not yet supported with metastock database\n";
430 }
431  
432 1;
433 =back
434  
435 =head1 COPYRIGHT
436  
437 Copyright 2003-2005 Tournedouet Yannick.
438  
439 =cut
440