| 1 | package Finance::GeniusTrader::DB::HTTP; | ||
| 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 | |||
| 9 | use Finance::GeniusTrader::DB; | ||
| 10 | use Finance::GeniusTrader::Prices; | ||
| 11 | use Finance::GeniusTrader::Conf; | ||
| 12 | use Finance::GeniusTrader::DateTime; | ||
| 13 | use LWP; | ||
| 14 | use POSIX; | ||
| 15 | |||
| 16 | =head1 NAME | ||
| 17 | |||
| 18 | DB::HTTP - Retrieve prices from a CGI | ||
| 19 | |||
| 20 | =head1 DESCRIPTION | ||
| 21 | |||
| 22 | =head2 Overview | ||
| 23 | |||
| 24 | This access module enable you to download prices from a remote server | ||
| 25 | using a CGI script (cf web/quotes.pl). | ||
| 26 | |||
| 27 | =head2 Configuration | ||
| 28 | |||
| 29 | Most configuration items have default values, to alter these defaults | ||
| 30 | you must indicate the configuration item and its value in your | ||
| 31 | $HOME/.gt/options file, especially for authentification purpose. | ||
| 32 | |||
| 33 | =over | ||
| 34 | |||
| 35 | =item DB::module HTTP | ||
| 36 | |||
| 37 | Informs gt you are using the HTTP.pm module. This | ||
| 38 | configuration item is always required in your $HOME/.gt/options file. | ||
| 39 | |||
| 40 | =item DB::HTTP::url : The URL that will be requested to download | ||
| 41 | |||
| 42 | =item DB::HTTP::location : The location of the server (www.geniustrader.org) | ||
| 43 | |||
| 44 | =item DB::HTTP::zone : The server zone (ie: admin) | ||
| 45 | |||
| 46 | =item DB::HTTP::username : The user name (ie : guest) | ||
| 47 | |||
| 48 | =item DB::HTTP::password : The password (ie : anonymous) | ||
| 49 | |||
| 50 | =item DB::HTTP::marker string | ||
| 51 | |||
| 52 | Delimits fields in each row of the data file. | ||
| 53 | The marker defaults to the tab character '\t'. | ||
| 54 | |||
| 55 | =item DB::HTTP::header_lines number | ||
| 56 | |||
| 57 | The number of header lines in your data file | ||
| 58 | that are to be skipped during processing. Lines with the either the | ||
| 59 | comment symbol '#' or the less than symbol '<' as the first character | ||
| 60 | do not need to be included in this value.. The header_lines default value is 0. | ||
| 61 | |||
| 62 | =item DB::HTTP::format 0|1|2|3 (default is 3) | ||
| 63 | The format of the date/time string. Valid values are: | ||
| 64 | 0 - yyyy-mm-dd hh:nn:ss (the time string is optional) | ||
| 65 | 1 - US Format (month before day, any format understood by Date::Calc) | ||
| 66 | 2 - European Format (day before month, any format understood by Date::Calc) | ||
| 67 | 3 - Any format understood by Date::Manip | ||
| 68 | |||
| 69 | =item DB::HTTP::fields::datetime number | ||
| 70 | |||
| 71 | Column index where to find the period datetime | ||
| 72 | field. Indexes are 0 based. For the particular case of datetime, can contain | ||
| 73 | multiple indexes, useful when date and time are separate columns in the data | ||
| 74 | file. The date time format is anything that can be understood by Date::Manip. | ||
| 75 | A typical example would be YYYY-MM-DD HH:NN:SS. The default datetime index is 5. | ||
| 76 | |||
| 77 | =item DB::HTTP::fields::open number | ||
| 78 | |||
| 79 | Column index where to find the period open field. | ||
| 80 | Indexes are 0 based. The default open index is 0. | ||
| 81 | |||
| 82 | =item DB::HTTP::fields::low number | ||
| 83 | |||
| 84 | Column index where to find the period low field. | ||
| 85 | Indexes are 0 based. The default low index is 2. | ||
| 86 | |||
| 87 | =item DB::HTTP::fields::high number | ||
| 88 | |||
| 89 | Column index where to find the period high field. | ||
| 90 | Indexes are 0 based. The default high index is 1. | ||
| 91 | |||
| 92 | =item DB::HTTP::fields::close number | ||
| 93 | |||
| 94 | Column index where to find the period close field. | ||
| 95 | Indexes are 0 based. The default close index is 3. | ||
| 96 | |||
| 97 | =item DB::HTTP::fields::volume number | ||
| 98 | |||
| 99 | Column index where to find the period volume field. | ||
| 100 | Indexes are 0 based. The default volume index is 4. | ||
| 101 | |||
| 102 | =back | ||
| 103 | |||
| 104 | You can set the DB::HTTP::directory configuration item to tell where | ||
| 105 | the quotes are cached. | ||
| 106 | |||
| 107 | =head2 Functions | ||
| 108 | |||
| 109 | =over | ||
| 110 | |||
| 111 | =item C<< new() >> | ||
| 112 | |||
| 113 | Create a new DB object used to retry quotes from a CGI on a remote | ||
| 114 | server. | ||
| 115 | |||
| 116 | =cut | ||
| 117 | sub new { | ||
| 118 | my $type = shift; | ||
| 119 | my $class = ref($type) || $type; | ||
| 120 | |||
| 121 | Finance::GeniusTrader::Conf::default("DB::HTTP::directory", | ||
| 122 | Finance::GeniusTrader::Conf::_get_home_path() . "/.gt/http-db-cache"); | ||
| 123 | Finance::GeniusTrader::Conf::default('DB::HTTP::header_lines', '0'); | ||
| 124 | Finance::GeniusTrader::Conf::default('DB::HTTP::marker', "\t"); | ||
| 125 | Finance::GeniusTrader::Conf::default('DB::HTTP::file_extension', '.txt'); | ||
| 126 | Finance::GeniusTrader::Conf::default('DB::HTTP::format', '3'); | ||
| 127 | Finance::GeniusTrader::Conf::default('DB::HTTP::fields::datetime', '5'); | ||
| 128 | Finance::GeniusTrader::Conf::default('DB::HTTP::fields::open', '0'); | ||
| 129 | Finance::GeniusTrader::Conf::default('DB::HTTP::fields::low', '2'); | ||
| 130 | Finance::GeniusTrader::Conf::default('DB::HTTP::fields::high', '1'); | ||
| 131 | Finance::GeniusTrader::Conf::default('DB::HTTP::fields::close', '3'); | ||
| 132 | Finance::GeniusTrader::Conf::default('DB::HTTP::fields::volume', '4'); | ||
| 133 | |||
| 134 | my $self = { "directory" => Finance::GeniusTrader::Conf::get("DB::HTTP::directory"), | ||
| 135 | "header_lines" => Finance::GeniusTrader::Conf::get('DB::HTTP::header_lines'), | ||
| 136 | "mark" => Finance::GeniusTrader::Conf::get('DB::HTTP::marker'), | ||
| 137 | "date_format" => Finance::GeniusTrader::Conf::get('DB::HTTP::format'), | ||
| 138 | "extension" => Finance::GeniusTrader::Conf::get('DB::HTTP::file_extension'), | ||
| 139 | "datetime" => Finance::GeniusTrader::Conf::get('DB::HTTP::fields::datetime'), | ||
| 140 | "open" => Finance::GeniusTrader::Conf::get('DB::HTTP::fields::open'), | ||
| 141 | "low" => Finance::GeniusTrader::Conf::get('DB::HTTP::fields::low'), | ||
| 142 | "high" => Finance::GeniusTrader::Conf::get('DB::HTTP::fields::high'), | ||
| 143 | "close" => Finance::GeniusTrader::Conf::get('DB::HTTP::fields::close'), | ||
| 144 | "volume" => Finance::GeniusTrader::Conf::get('DB::HTTP::fields::volume'), | ||
| 145 | "url" => Finance::GeniusTrader::Conf::get("DB::HTTP::url"), | ||
| 146 | "location" => Finance::GeniusTrader::Conf::get("DB::HTTP::location"), | ||
| 147 | "zone" => Finance::GeniusTrader::Conf::get("DB::HTTP::zone"), | ||
| 148 | "username" => Finance::GeniusTrader::Conf::get("DB::HTTP::username"), | ||
| 149 | "password" => Finance::GeniusTrader::Conf::get("DB::HTTP::password") | ||
| 150 | }; | ||
| 151 | |||
| 152 | return bless $self, $class; | ||
| 153 | } | ||
| 154 | |||
| 155 | =item C<< $db->disconnect >> | ||
| 156 | |||
| 157 | Disconnects from the database. | ||
| 158 | |||
| 159 | =cut | ||
| 160 | sub disconnect { | ||
| 161 | my $self = shift; | ||
| 162 | } | ||
| 163 | |||
| 164 | =item C<< $db->set_directory("/new/directory") >> | ||
| 165 | |||
| 166 | Indicate the directory containing all the cached data. | ||
| 167 | |||
| 168 | =cut | ||
| 169 | sub set_directory { | ||
| 170 | my ($self, $dir) = @_; | ||
| 171 | $self->{'directory'} = $dir; | ||
| 172 | } | ||
| 173 | |||
| 174 | |||
| 175 | =item C<< $db->get_prices($code, $timeframe) >> | ||
| 176 | |||
| 177 | Returns a Finance::GeniusTrader::Prices object containing all known prices for the symbol $code. | ||
| 178 | |||
| 179 | =cut | ||
| 180 | sub get_prices { | ||
| 181 | my ($self, $code, $timeframe) = @_; | ||
| 182 | $timeframe = $DAY unless ($timeframe); | ||
| 183 | die "Intraday support not implemented in DB::HTTP" if ($timeframe < $DAY); | ||
| 184 | return Finance::GeniusTrader::Prices->new() if ($timeframe > $DAY); | ||
| 185 | |||
| 186 | my $prices = Finance::GeniusTrader::Prices->new(); | ||
| 187 | $prices->set_timeframe($timeframe); | ||
| 188 | |||
| 189 | my %fields = ('open' => $self->{'open'}, 'high' => $self->{'high'}, | ||
| 190 | 'low' => $self->{'low'}, 'close' => $self->{'close'}, | ||
| 191 | 'volume' => $self->{'volume'}, 'date' => $self->{'datetime'}); | ||
| 192 | $self->{'fields'} = \%fields; | ||
| 193 | |||
| 194 | my $file = $self->download_prices($code); | ||
| 195 | |||
| 196 | $prices->loadtxt($file, $self->{'mark'}, $self->{'date_format'}, | ||
| 197 | $self->{'header_lines'}, %fields); | ||
| 198 | return $prices; | ||
| 199 | } | ||
| 200 | |||
| 201 | =item C<< $db->get_last_prices($code, $limit, $timeframe) >> | ||
| 202 | |||
| 203 | NOT SUPPORTED for HTTP db. | ||
| 204 | |||
| 205 | Returns a Finance::GeniusTrader::Prices object containing the $limit last known prices for | ||
| 206 | the symbol $code. | ||
| 207 | |||
| 208 | =cut | ||
| 209 | sub get_last_prices { | ||
| 210 | my ($self, $code, $limit, $timeframe) = @_; | ||
| 211 | |||
| 212 | return get_prices($self, $code, $timeframe) if ($limit==-1); | ||
| 213 | die "get_last_prices not supported with HTTP database\n"; | ||
| 214 | } | ||
| 215 | |||
| 216 | sub download_prices { | ||
| 217 | my ($self, $code) = @_; | ||
| 218 | |||
| 219 | # Create a new directory if it doesn't already exist | ||
| 220 | if (! -d $self->{'directory'}) | ||
| 221 | { | ||
| 222 | mkdir $self->{'directory'}; | ||
| 223 | } | ||
| 224 | |||
| 225 | # Do not request again a file already in the cache directory, | ||
| 226 | # when the local file is less than 16 hours old. | ||
| 227 | my $cache_file = "$self->{'directory'}/$code.txt"; | ||
| 228 | if (-e $cache_file && ( (stat(_))[9] > time - 16 * 3600 )) | ||
| 229 | { | ||
| 230 | return $cache_file; | ||
| 231 | } | ||
| 232 | |||
| 233 | # At this point, we know that our local file needs to be update. | ||
| 234 | # Have a look at the latest date available on our local set of | ||
| 235 | # data and request quotes to the server since then. | ||
| 236 | my ($latest_date, $last_close); | ||
| 237 | my $prices = Finance::GeniusTrader::Prices->new(); | ||
| 238 | $prices->set_timeframe($DAY); | ||
| 239 | if (-e $cache_file) { | ||
| 240 | $prices->loadtxt($cache_file); | ||
| 241 | |||
| 242 | my ($sec, $min, $hour, $d, $m, $y, $wd, $yd) = localtime; | ||
| 243 | my $today = sprintf("%04d-%02d-%02d", $y + 1900, $m + 1, $d); | ||
| 244 | $latest_date = $prices->find_nearest_date($today); | ||
| 245 | $last_close = $prices->at_date($latest_date)->[$CLOSE]; | ||
| 246 | } | ||
| 247 | |||
| 248 | # Set up a new UserAgent with credentials | ||
| 249 | my $ua = new LWP::UserAgent; | ||
| 250 | $ua->agent("GeniusTrader HTTP method/0.2"); | ||
| 251 | if (defined($self->{'username'}) && | ||
| 252 | defined($self->{'password'})) { | ||
| 253 | $ua->credentials($self->{'location'}, $self->{'zone'}, | ||
| 254 | $self->{'username'}, $self->{'password'}); | ||
| 255 | } | ||
| 256 | |||
| 257 | # Create a new HTTP request to download data for $code since $latest_date | ||
| 258 | my $args = "?code=$code"; | ||
| 259 | $args .= "&since=$latest_date" if (defined($latest_date) && $latest_date); | ||
| 260 | $args .= "&lastclose=$last_close" if (defined($last_close) && $last_close); | ||
| 261 | my $req = new HTTP::Request GET => $self->{'url'} . $args; | ||
| 262 | |||
| 263 | # Download and update the cache file | ||
| 264 | my $res = $ua->request($req); | ||
| 265 | if ($res->is_success) | ||
| 266 | { | ||
| 267 | open (CACHE, "> $cache_file.tmp") || | ||
| 268 | die "Can't write in $cache_file.tmp : $!\n"; | ||
| 269 | print CACHE $res->content; | ||
| 270 | close CACHE; | ||
| 271 | my $new_prices = Finance::GeniusTrader::Prices->new(); | ||
| 272 | $new_prices->loadtxt("$cache_file.tmp", $self->{'mark'}, $self->{'date_format'}, %{$self->{'fields'}}); | ||
| 273 | $new_prices->set_timeframe($DAY); | ||
| 274 | for(my $i = 0; $i < $new_prices->count(); $i++) { | ||
| 275 | if ($prices->has_date($new_prices->at($i)->[$DATE])) { | ||
| 276 | my $new_p = $new_prices->at($i); | ||
| 277 | my $p = $prices->at_date($new_p->[$DATE]); | ||
| 278 | $p->[$OPEN] = $new_p->[$OPEN]; | ||
| 279 | $p->[$HIGH] = $new_p->[$HIGH]; | ||
| 280 | $p->[$LOW] = $new_p->[$LOW]; | ||
| 281 | $p->[$CLOSE] = $new_p->[$CLOSE]; | ||
| 282 | $p->[$VOLUME] = $new_p->[$VOLUME]; | ||
| 283 | } else { | ||
| 284 | $prices->add_prices( [ @{$new_prices->at($i)} ] ); | ||
| 285 | } | ||
| 286 | } | ||
| 287 | $prices->savetxt($cache_file); | ||
| 288 | unlink "$cache_file.tmp"; | ||
| 289 | } else { | ||
| 290 | die "Download failed.\n"; | ||
| 291 | } | ||
| 292 | return $cache_file; | ||
| 293 | } | ||
| 294 | |||
| 295 | =pod | ||
| 296 | |||
| 297 | =back | ||
| 298 | |||
| 299 | =cut | ||
| 300 | 1; |