| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Finance::GeniusTrader::DB::Text; |
|
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
|
|
|
|
|
|
|
# new version joao costa circa nov 07 |
|
8
|
|
|
|
|
|
|
# $Id$ |
|
9
|
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
16925
|
use strict; |
|
|
1
|
|
|
|
|
8
|
|
|
|
1
|
|
|
|
|
84
|
|
|
11
|
|
|
|
|
|
|
our @ISA = qw(Finance::GeniusTrader::DB); |
|
12
|
|
|
|
|
|
|
|
|
13
|
1
|
|
|
1
|
|
1440
|
use Finance::GeniusTrader::DB; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
38
|
|
|
14
|
1
|
|
|
1
|
|
1471
|
use Finance::GeniusTrader::Prices; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
use Finance::GeniusTrader::Conf; |
|
16
|
|
|
|
|
|
|
use Finance::GeniusTrader::DateTime; |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 DB::Text access module |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head2 Overview |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
This database access module enable you to work with a full directory of |
|
23
|
|
|
|
|
|
|
text files. |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=head2 Configuration |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
Most configuration items have default values, to alter these defaults |
|
28
|
|
|
|
|
|
|
you must indicate the configuration item and its value in your |
|
29
|
|
|
|
|
|
|
$HOME/.gt/options file. |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=over |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=item DB::module Text |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
Informs gt you are using the Text.pm module. This |
|
36
|
|
|
|
|
|
|
configuration item is always required in your $HOME/.gt/options file. |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=item DB::text::directory path |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
where files are stored. This |
|
41
|
|
|
|
|
|
|
configuration item is always required in your $HOME/.gt/options file. |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=item DB::text::marker string |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
Delimits fields in each row of the data file. |
|
46
|
|
|
|
|
|
|
The marker defaults to the tab character '\t'. |
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=item DB::text::header_lines number |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
The number of header lines in your data file |
|
51
|
|
|
|
|
|
|
that are to be skipped during processing. Lines with the either the |
|
52
|
|
|
|
|
|
|
comment symbol '#' or the less than symbol '<' as the first character |
|
53
|
|
|
|
|
|
|
do not need to be included in this value.. The header_lines default value is 0. |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=item DB::text::file_extension string |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
To be appended to the code file name when |
|
58
|
|
|
|
|
|
|
searching the data file. For instance, if the data file is called EURUSD.csv |
|
59
|
|
|
|
|
|
|
this variable would have the value '.csv' (without the quotes). |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
The default file_extension is '.txt'. |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
if you have data in different timeframes, for instance, EURUSD_hour.csv and |
|
64
|
|
|
|
|
|
|
EURUSD_day.csv, use the following value for this directive: |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=item DB::text::file_extension _$timeframe.csv |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=item DB::text::format 0|1|2|3 (default is 3) |
|
69
|
|
|
|
|
|
|
The format of the date/time string. Valid values are: |
|
70
|
|
|
|
|
|
|
0 - yyyy-mm-dd hh:nn:ss (the time string is optional) |
|
71
|
|
|
|
|
|
|
1 - US Format (month before day, any format understood by Date::Calc) |
|
72
|
|
|
|
|
|
|
2 - European Format (day before month, any format understood by Date::Calc) |
|
73
|
|
|
|
|
|
|
3 - Any format understood by Date::Manip |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=item DB::text::fields::datetime number |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
Column index where to find the period datetime |
|
78
|
|
|
|
|
|
|
field. Indexes are 0 based. For the particular case of datetime, can contain |
|
79
|
|
|
|
|
|
|
multiple indexes, useful when date and time are separate columns in the data |
|
80
|
|
|
|
|
|
|
file. The date time format is anything that can be understood by Date::Manip. |
|
81
|
|
|
|
|
|
|
A typical example would be YYYY-MM-DD HH:NN:SS. The default datetime index is 5. |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=item DB::text::fields::open number |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
Column index where to find the period open field. |
|
86
|
|
|
|
|
|
|
Indexes are 0 based. The default open index is 0. |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=item DB::text::fields::low number |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
Column index where to find the period low field. |
|
91
|
|
|
|
|
|
|
Indexes are 0 based. The default low index is 2. |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=item DB::text::fields::high number |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
Column index where to find the period high field. |
|
96
|
|
|
|
|
|
|
Indexes are 0 based. The default high index is 1. |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=item DB::text::fields::close number |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
Column index where to find the period close field. |
|
101
|
|
|
|
|
|
|
Indexes are 0 based. The default close index is 3. |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=item DB::text::fields::volume number |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
Column index where to find the period volume field. |
|
106
|
|
|
|
|
|
|
Indexes are 0 based. The default volume index is 4. |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head2 new() |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
Create a new DB object used to retrieve quotes from a directory |
|
112
|
|
|
|
|
|
|
full of text files containing prices. |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=cut |
|
115
|
|
|
|
|
|
|
sub new { |
|
116
|
|
|
|
|
|
|
my $type = shift; |
|
117
|
|
|
|
|
|
|
my $class = ref($type) || $type; |
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
my $self = { "directory" => Finance::GeniusTrader::Conf::get("DB::Text::directory")}; |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
Finance::GeniusTrader::Conf::default('DB::Text::header_lines', '0'); |
|
122
|
|
|
|
|
|
|
Finance::GeniusTrader::Conf::default('DB::Text::marker', "\t"); |
|
123
|
|
|
|
|
|
|
Finance::GeniusTrader::Conf::default('DB::Text::file_extension', '.txt'); |
|
124
|
|
|
|
|
|
|
Finance::GeniusTrader::Conf::default('DB::Text::format', '3'); |
|
125
|
|
|
|
|
|
|
Finance::GeniusTrader::Conf::default('DB::Text::fields::datetime', '5'); |
|
126
|
|
|
|
|
|
|
Finance::GeniusTrader::Conf::default('DB::Text::fields::open', '0'); |
|
127
|
|
|
|
|
|
|
Finance::GeniusTrader::Conf::default('DB::Text::fields::low', '2'); |
|
128
|
|
|
|
|
|
|
Finance::GeniusTrader::Conf::default('DB::Text::fields::high', '1'); |
|
129
|
|
|
|
|
|
|
Finance::GeniusTrader::Conf::default('DB::Text::fields::close', '3'); |
|
130
|
|
|
|
|
|
|
Finance::GeniusTrader::Conf::default('DB::Text::fields::volume', '4'); |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
$self->{'header_lines'} = Finance::GeniusTrader::Conf::get('DB::Text::header_lines'); |
|
133
|
|
|
|
|
|
|
$self->{'mark'} = Finance::GeniusTrader::Conf::get('DB::Text::marker'); |
|
134
|
|
|
|
|
|
|
$self->{'date_format'} = Finance::GeniusTrader::Conf::get('DB::Text::format'); |
|
135
|
|
|
|
|
|
|
$self->{'extension'} = Finance::GeniusTrader::Conf::get('DB::Text::file_extension'); |
|
136
|
|
|
|
|
|
|
$self->{'datetime'} = Finance::GeniusTrader::Conf::get('DB::Text::fields::datetime'); |
|
137
|
|
|
|
|
|
|
$self->{'open'} = Finance::GeniusTrader::Conf::get('DB::Text::fields::open'); |
|
138
|
|
|
|
|
|
|
$self->{'low'} = Finance::GeniusTrader::Conf::get('DB::Text::fields::low'); |
|
139
|
|
|
|
|
|
|
$self->{'high'} = Finance::GeniusTrader::Conf::get('DB::Text::fields::high'); |
|
140
|
|
|
|
|
|
|
$self->{'close'} = Finance::GeniusTrader::Conf::get('DB::Text::fields::close'); |
|
141
|
|
|
|
|
|
|
$self->{'volume'} = Finance::GeniusTrader::Conf::get('DB::Text::fields::volume'); |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
return bless $self, $class; |
|
144
|
|
|
|
|
|
|
} |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head2 $db->disconnect |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
Disconnects from the database. |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=cut |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
sub disconnect { |
|
153
|
|
|
|
|
|
|
my $self = shift; |
|
154
|
|
|
|
|
|
|
} |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=head2 $db->set_directory("/new/directory") |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
Indicate the directory containing all the text files. |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=cut |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
sub set_directory { |
|
163
|
|
|
|
|
|
|
my ($self, $dir) = @_; |
|
164
|
|
|
|
|
|
|
$self->{'directory'} = $dir; |
|
165
|
|
|
|
|
|
|
} |
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=head2 $db->get_prices($code, $timeframe) |
|
168
|
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
Returns a Finance::GeniusTrader::Prices object containing all known prices for the symbol $code. |
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=cut |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
sub get_prices { |
|
174
|
|
|
|
|
|
|
my ($self, $code, $timeframe) = @_; |
|
175
|
|
|
|
|
|
|
$timeframe = $DAY unless ($timeframe); |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
my $prices = Finance::GeniusTrader::Prices->new; |
|
178
|
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
# WARNING: Can only load data that is in daily format or smaller |
|
180
|
|
|
|
|
|
|
# Trying to load weekly or monthly data will fail. |
|
181
|
|
|
|
|
|
|
return $prices if ($timeframe > $DAY); |
|
182
|
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
$prices->set_timeframe($timeframe); |
|
184
|
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
my %fields = ('open' => $self->{'open'}, 'high' => $self->{'high'}, |
|
186
|
|
|
|
|
|
|
'low' => $self->{'low'}, 'close' => $self->{'close'}, |
|
187
|
|
|
|
|
|
|
'volume' => $self->{'volume'}, 'date' => $self->{'datetime'}); |
|
188
|
|
|
|
|
|
|
$self->{'fields'} = \%fields; |
|
189
|
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
my $extension = $self->{'extension'}; |
|
191
|
|
|
|
|
|
|
my $tfname = Finance::GeniusTrader::DateTime::name_of_timeframe($timeframe); |
|
192
|
|
|
|
|
|
|
$extension =~ s/\$timeframe/$tfname/g; |
|
193
|
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
my $file = $self->{'directory'} . "/$code" . $extension; |
|
195
|
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
$prices->loadtxt($file, $self->{'mark'}, $self->{'date_format'}, |
|
197
|
|
|
|
|
|
|
$self->{'header_lines'}, %fields); |
|
198
|
|
|
|
|
|
|
return $prices; |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
} |
|
201
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=pod |
|
203
|
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=head2 $db->get_last_prices($code, $limit, $timeframe) |
|
205
|
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
Returns a Finance::GeniusTrader::Prices object containing the $limit last known prices for |
|
207
|
|
|
|
|
|
|
the symbol $code. |
|
208
|
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=cut |
|
210
|
|
|
|
|
|
|
sub get_last_prices { |
|
211
|
|
|
|
|
|
|
my ($self, $code, $limit, $timeframe) = @_; |
|
212
|
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
### warn "$limit parameter ignored in DB::Text::get_last_prices. loading entire dataset." if ($limit > -1); |
|
214
|
|
|
|
|
|
|
return get_prices($self, $code, $timeframe, -1); |
|
215
|
|
|
|
|
|
|
} |
|
216
|
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
sub has_code { |
|
218
|
|
|
|
|
|
|
my ($self, $code) = @_; |
|
219
|
|
|
|
|
|
|
my $extension = $self->{'extension'}; |
|
220
|
|
|
|
|
|
|
$extension =~ s/\$timeframe/\.\*/g; |
|
221
|
|
|
|
|
|
|
my $file_exists = 0; |
|
222
|
|
|
|
|
|
|
my $file_pattern = $self->{'directory'} . "/$code$extension"; |
|
223
|
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
if ($extension =~ /\*/) { |
|
225
|
|
|
|
|
|
|
eval "use File::Find;"; |
|
226
|
|
|
|
|
|
|
find ( sub { $file_exists = 1 if ($_ =~ /$file_pattern/); },$self->{'directory'}); |
|
227
|
|
|
|
|
|
|
} else { |
|
228
|
|
|
|
|
|
|
$file_exists = 1 if (-e $file_pattern); |
|
229
|
|
|
|
|
|
|
} |
|
230
|
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
return $file_exists; |
|
232
|
|
|
|
|
|
|
} |
|
233
|
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
1; |