GeniusTrader/DB/MetaStock.pm untested

filter: Statementstmt 0.0% 0 / 79 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 127 CC 15 SCAR 48.2
CC
15
Cov
0%
get_prices
210.0
get_last_prices
6.0
new
2.0
CRAP 123.7
1 package Finance::GeniusTrader::DB::MetaStock;
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 Date::Calc qw(Decode_Date_EU);
9  
10 use Finance::GeniusTrader::DB;
11 use Finance::GeniusTrader::Prices;
12 use Finance::GeniusTrader::DateTime;
13  
14 =head1 DB::MetaStock access module
15  
16 =head2 Overview
17  
18 The MetaStock access module is able to retrieve quotes from almost any
19 type of MetaStock/Computrac database.
20  
21 =head2 Note
22  
23 This module calls the binary program identified by the $HOME/.gt/options
24 file option "DB::metastock::program" ("/bourse/tools/MetaStockReader" by default)
25 to get quotes from your metastock database.
26  
27 $HOME/.gt/options file option
28 "DB::metastock::directory" must indicate the directory of the metastock database.
29 (no default).
30  
31 Please refer to /bourse/tools/MetaStockReader source if you want to learn
32 more about it.
33  
34 NOTE: this module and the companion binary program has been depreciated
35 in favor of the stand-alone perl module (unfortunately) also named
36 MetaStockReader. refer to that modules pod
37  
38 =head2 Configuration
39  
40 You can indicate the directory which contains the MetaStock database
41 by setting the DB::metastock::directory configuration item. You can
42 also set DB::metastock::program to indicate where the MetaStockReader
43 binary program is located (complete pathname).
44  
45 =head2 new()
46  
47 Create a new DB object used to retry quotes from a MetaStock database.
48  
49 =cut
50 sub new {
51 my $type = shift;
52 my $class = ref($type) || $type;
53  
54 Finance::GeniusTrader::Conf::default("DB::metastock::directory", "");
55 Finance::GeniusTrader::Conf::default("DB::metastock::program",
56 "/bourse/tools/MetaStockReader");
57  
58 my $self = { "directory" => Finance::GeniusTrader::Conf::get("DB::metastock::directory"),
59 "program" => Finance::GeniusTrader::Conf::get("DB::metastock::program") };
60
61 return bless $self, $class;
62 }
63  
64 =head2 $db->disconnect
65  
66 Disconnects from the database.
67  
68 =cut
69 sub disconnect {
70 my $self = shift;
71 }
72  
73 =head2 $db->set_directory("/new/directory")
74  
75 Indicate the directory containing all the text files.
76  
77 =cut
78 sub set_directory {
79 my ($self, $directory) = @_;
80 $self->{'directory'} = $directory;
81 }
82  
83 =head2 $db->get_prices($code, $timeframe)
84  
85 Returns a Finance::GeniusTrader::Prices object containing all known prices for the symbol $code.
86  
87 =cut
88 sub get_prices {
89 my ($self, $code, $timeframe) = @_;
90 $timeframe = $DAY unless ($timeframe);
91 die "Intraday support not implemented in DB::MetaStock" if ($timeframe < $DAY);
92 return Finance::GeniusTrader::Prices->new() if ($timeframe > $DAY);
93  
94 my ($open, $high, $low, $close, $volume, $date, $time);
95 my ($year, $month, $day);
96 my %fields;
97 my $position = 0;
98
99 # Launch MetaStockReader with the correct arguments
100 # in order to get all quotes for the specified symbol
101 my @results = `$self->{'program'} -r $self->{'directory'} $code`;
102
103 my $prices = Finance::GeniusTrader::Prices->new();
104 $prices->set_timeframe($timeframe);
105
106 foreach (@results) {
107  
108 if ($position eq 0) {
109
110 # Get HEADER and set up %fields according to the fields map
111 # usually : date [time] [open] high low close volume
112
113 my @header = split(/\t/, $results[0]);
114 my $i = 0;
115
116 foreach (@header) {
117  
118 my $field = $header[$i];
119 $field =~ s/\n//;
120  
121 if ($field eq "Open") {
122 $fields{'open'} = $i;
123 }
124 if ($field eq "High") {
125 $fields{'high'} = $i;
126 }
127 if ($field eq "Low") {
128 $fields{'low'} = $i;
129 }
130 if ($field eq "Close") {
131 $fields{'close'} = $i;
132 }
133 if ($field eq "Volume") {
134 $fields{'volume'} = $i;
135 }
136 if ($field eq "Date") {
137 $fields{'date'} = $i;
138 }
139 if ($field eq "Time") {
140 $fields{'time'} = $i;
141 }
142 $i++;
143 }
144
145 } else {
146  
147 # Get and split each line with a tabulation
148 my @line = split(/\t/, $results[$position]);
149  
150 # Get and swap all necessary fields according to the header
151 $open = $line[$fields{'open'}];
152 $high = $line[$fields{'high'}];
153 $low = $line[$fields{'low'}];
154 $close = $line[$fields{'close'}];
155 $volume = $line[$fields{'volume'}];
156 $date = $line[$fields{'date'}];
157 if ($fields{'time'}) {
158 $time = $line[$fields{'time'}];
159 }
160  
161 # Decode MetaStock date format to something more usefull
162 ($year, $month, $day) = Decode_Date_EU($date);
163
164 # And convert it to GeniusTrader standard format
165 $date = $year . "-" . $month . "-" .$day;
166 if ($fields{'time'}) {
167 $date .= "-" . $time;
168 }
169
170 # Add all data within the Finance::GeniusTrader::Prices object
171 $prices->add_prices([ $open, $high, $low, $close, $volume, $date ]);
172
173 }
174 $position++;
175 }
176 return $prices;
177 }
178  
179 =head2 $db->get_last_prices($code, $limit, $timeframe)
180  
181 NOT SUPPORTED for text db.
182  
183 Returns a Finance::GeniusTrader::Prices object containing the $limit last known prices for
184 the symbol $code.
185  
186 =cut
187 sub get_last_prices {
188 my ($self, $code, $limit, $timeframe) = @_;
189  
190 return get_prices($self, $code, $timeframe) if ($limit==-1);
191 die "get_last_prices not yet supported with metastock database\n";
192 }
193  
194 1;