[R-SIG-Finance] Brazilian Equities

Dirk Eddelbuettel edd at debian.org
Sat Jan 2 18:58:04 CET 2010


On 2 January 2010 at 10:36, Joshua Ulrich wrote:
| To get 25, I had to change the "yahoo.URL" object in
| quantmod::getSymbols.yahoo.  You can do this via the
| fixInNamespace(getSymbols.yahoo,"quantmod") command.
| 
| I changed it from
| yahoo.URL <- "http://chart.yahoo.com/table.csv?"
| to
| yahoo.URL <- "http://ichart.yahoo.com/table.csv?"

That is interesting topic. A bit over a decade ago, I became CPAN maintainer
of Finance::YahooQuote as two of my (then current, now almost stale :) Perl
projects use it to acquire current data.  Over the years, but more so in the
beginning, I had to make a number of adjustments to the URL that Yahoo uses,
often with explicit if/else or switches based on ticker extension
("country").  But as I said, that is for _current_ data.

For historical data, I never got around to releasing the accessor as a
standalone module and the functionality is hidden within the Beancounter
code. There I use "http://table.finance.yahoo.com/table.csv?" as the base URL
and that has worked fairly reliably for all historical equity data I (or
other users) have tried to retrieve.  FX is a different story, and so are
other asset classes they don't even cover.  But that interface has been much
stabler

If you're into Perl, the function is 

sub GetHistoricalData {         # get a batch of historical quotes from Yahoo!
  my ($symbol,$from,$to) = @_;
  my $ua = RequestAgent->new;
  $ua->env_proxy;               # proxy settings from *_proxy env. variables.
  $ua->proxy('http', $Config{proxy}) if $Config{proxy};  # or config vars
  my ($a,$b,$c,$d,$e,$f);       # we need the date as yyyy, mm and dd
  ($c,$a,$b) = ($from =~ m/(\d\d\d\d)(\d\d)(\d\d)/);
  ($f,$d,$e) = ($to =~ m/(\d\d\d\d)(\d\d)(\d\d)/);
  --$a; --$d; # month is zero-based
  my $req = new HTTP::Request GET => "http://table.finance.yahoo.com/" .
    "table.csv?a=$a&b=$b&c=$c&d=$d&e=$e&f=$f&s=$symbol&y=0&g=d&ignore=.csv";
  my $res = $ua->request($req);  # Pass request to user agent and get response
  if ($res->is_success) {       # Check the outcome of the response
    return split(/\n/, $res->content);
  } else {
    warn "No luck with symbol $symbol\n";
  }
}

One of these days I sit down and rewrite all of Beancounter in R....  But
I've been saying that for a few years and haven't done it yet so don't hold
your breath.

Anyway, to prove Josh's point, you can 'hand-translate' this into a R request
for, say, Petrobas:

R> X <- read.csv("http://table.finance.yahoo.com/table.csv?a=2009&b=01&c=01&d=2009&e=01e&f=01&s=PETR4.SA&y=0&g=d&ignore=.csv", header=TRUE)
R> head(X)
        Date  Open  High   Low Close   Volume Adj.Close
1 2009-12-30 36.57 36.69 36.35 36.69 13901400     36.69
2 2009-12-29 36.89 36.94 36.65 36.80  5198500     36.80
3 2009-12-28 36.91 37.00 36.63 36.75  9099900     36.75
4 2009-12-23 36.50 36.76 36.15 36.70 11004500     36.70
5 2009-12-22 35.73 36.34 35.60 36.34 15008900     36.34
6 2009-12-21 36.80 36.85 35.20 35.20 20905000     35.20
R>

It's too bad that Yahoo! doesn't simply declare this a public interface and
document it so that we have to poke in the dark for the best URL...

Dirk

PS Beancounter lives at http://dirk.eddelbuettel.com/code/beancounter.html

-- 
Three out of two people have difficulties with fractions.



More information about the R-SIG-Finance mailing list