File Coverage

blib/lib/Finance/Quant/Quotes.pm
Criterion Covered Total %
statement 9 37 24.3
branch 0 4 0.0
condition n/a
subroutine 3 5 60.0
pod 0 1 0.0
total 12 47 25.5


line stmt bran cond sub pod time code
1             #!/usr/bin/perl -X
2             package Finance::Quant::Quotes;
3              
4 1     1   23858 use strict;
  1         2  
  1         35  
5 1     1   5 use warnings;
  1         2  
  1         27  
6 1     1   1670 use LWP::UserAgent;
  1         63159  
  1         612  
7              
8             require Exporter;
9              
10             our @ISA = qw(Exporter);
11              
12             # Items to export into callers namespace by default. Note: do not export
13             # names by default without a very good reason. Use EXPORT_OK instead.
14             # Do not simply export all your public functions/methods/constants.
15              
16             # This allows declaration use Finance::Quant::Quotes ':all';
17             # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
18             # will save memory.
19             our %EXPORT_TAGS = ( 'all' => [ qw(
20            
21             ) ] );
22              
23             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
24              
25             our @EXPORT = qw(
26             get
27             );
28              
29             our $VERSION = '0.01';
30              
31              
32              
33            
34             sub get {
35 0     0 0   my ($symbol, $startdate, $enddate, $agent) = @_;
36 0           print "fetching data...\n";
37 0           my $dat = _fetch($symbol, $startdate, $enddate, $agent); # csv file, 1st row = header
38            
39            
40 0 0         return if(!$dat);
41            
42 0           my @q = split /\n/, $dat;
43 0           my @header = split /,/, shift @q;
44 0           my %quotes = map { $_ => [] } @header;
  0            
45 0           for my $q (@q) {
46 0           my @val = split ',', $q;
47 0           unshift @{$quotes{$header[$_]}}, $val[$_] for 0..$#val; # unshift instead of push if data listed latest 1st & oldest last
  0            
48             }
49             # open OUT, ">$symbol.csv";
50             # print OUT $dat;
51             # close OUT;
52             # print "data written to $symbol.csv.\n";
53              
54              
55 0           return \%quotes;
56             }
57             sub _fetch {
58 0     0     my ($symbol, $startdate, $enddate, $interval, $agent) = @_;
59 0           my $url = "http://chart.yahoo.com/table.csv?";
60 0           my $freq = "g=$interval"; # d: daily, w: weekly, m: monthly
61 0           my $stock = "s=$symbol";
62 0           my @start = split '-', $startdate;
63 0           my @end = split '-', $enddate;
64 0           $startdate = "a=" . ($start[0]-1) . "&b=$start[1]&c=$start[2]";
65 0           $enddate = "d=" . ($end[0]-1) . "&e=$end[1]&f=$end[2]";
66 0           $url .= "$startdate&$enddate&$stock&y=0&$freq&ignore=.csv";
67 0           my $ua = new LWP::UserAgent(agent=>$agent,timeout=>5);
68 0           my $request = new HTTP::Request('GET',$url);
69 0           my $response = $ua->request($request);
70 0 0         if ($response->is_success) {
71 0           return $response->content;
72             } else {
73              
74             # warn "Cannot fetch $url (status ", $response->code, " ", $response->message, ")\n";
75 0           return 0;
76             }
77             }
78              
79              
80              
81              
82             1;
83             __END__