[tech-spec] Re: Yahoo! quotes

  • From: Dirk Eddelbuettel <edd@xxxxxxxxxx>
  • To: tech-spec@xxxxxxxxxxxxx
  • Date: Tue, 19 Oct 2004 10:01:32 -0500

On Tue, Oct 19, 2004 at 07:29:53AM -0700, BBands wrote:
> Does anybody know how to get a current quote from Yahoo! via R. Getting
> history is easy enough via tseries or its, but I would like to get today's
> quote to complete the history.

Should be straightforward. 

I "inherited" the Perl module Finance::YahooQuote from being the Debian
maintainer for it as well as a regular user. The code inside it, almost all
of which was written by Dj Padzensky, is a little hairy as it is rather
efficient in batching requests for many securities into one call. But it
essentially just builds a fancy URL string which it then dispatches before
parsing the .csv return. 

As I should know better than to force any convinced Python hacker to look at
Perl code, I include an example shell script below. Given that R can
retrieve directly from read.csv() via url() as its file argument, this
should adapt easily. Could you report your snippets back?  Maybe this could
be something to contribute back as a little helper function? So below is the
old-ish script 'examine_server.sh' which has some documentation on which cgi
query symbol stands for which datum. Yahoo covers more, but I think I only
documented that in the Perl docs inside the .pl and .pm files of the module.

And just to fully prove the point, here is a test using some of the values
from below:

> read.csv(file=url("http://finance.yahoo.com/d/quotes.csv?f=sl1t1d1c1p2pov&s=IBM+MSFT+XIU.TO";),
>  header=FALSE)
      V1    V2      V3         V4   V5     V6    V7    V8       V9
1    IBM 89.41 10:37am 10/19/2004 3.49 +4.06% 85.92 88.20  5604900
2   MSFT 28.45 10:43am 10/19/2004 0.04 +0.14% 28.41 28.54 16286461
3 XIU.TO 49.59 10:40am 10/19/2004 0.26 +0.53% 49.33 49.53   611286
> 

Hope this helps, Dirk

#! /bin/bash
#
# from the source of http://edit.my.yahoo.com/config/edit_pfview/?.vk=v1
#     <option value="s">Symbol
#     <option value="n">Name
#     <option value="l">Last Trade (With Time)
#     <option value="l1">Last Trade (Price Only)
#                    t1  time of last trade
#                    d1  date of last trade
#     <option value="c">Change &amp; Percent
#     <option value="c1">Change
#                    p2  percentage change
#     <option value="v">Volume
#     <option value="a2">Average Daily Volume
#     <option value="i">More Info
#     <option value="b">Bid
#     <option value="a">Ask
#     <option value="p">Previous Close
#     <option value="o">Open
#     <option value="m">Day's Range
#                    g  Day's low
#                    h  Day's high
#     <option value="w">52-Week Range
#                    j  52-week low
#                    k  52-week high
#     <option value="e">Earnings/Share
#     <option value="r">P/E Ratio
#     <option value="r1">Dividend Pay Date
#     <option value="q">Ex-Dividend Date
#     <option value="d">Dividend/Share
#     <option value="y">Dividend Yield
#     <option value="j1">Market Capitalization
#     <option value="s1">Shares Owned
#     <option value="p1">Price Paid
#     <option value="c3">Commission
#     <option value="v1">Holdings Value
#     <option value="w1">Day's Value Change
#     <option value="g1">Holdings Gain &amp; Percent
#     <option value="g4">Holdings Gain
#     <option value="d2">Trade Date
#     <option value="g3">Annualized Gain
#     <option value="l2">High Limit
#     <option value="l3">Low Limit
#     <option value="n4">Notes


#SERVERURL=http://uk.finance.yahoo.com
#STOCKS=UKX.L+BT.A.L+BII.L
#FORMAT="snl1t1d1c1p2vpoghw"

#SERVERURL=http://finance.yahoo.com
#STOCKS=IBM+MSFT+XIU.TO

#SERVERURL=http://au.finance.yahoo.com/d/quotes.csv
#STOCKS=TLSCB.AX+CDL.NZ+JARD.SI
#+CTI.AX+AFI.AX+CDL.NZ
#FORMAT=snl1d1t1c1poghjkdyv=
#FORMAT=snl1d1t1c1p2pomwverdyba

#FORMAT=snl1d1t1c1p2ghvva2bapomwerr1dyj1x
#FORMAT=snl1t1d1c1p2poghv 

SERVERURL=http://sg.finance.yahoo.com
STOCKS=OCBC.SI+JARD.SI+0307.HK
FORMAT=snl1d1t1c1p2pomwverdybaxq

#SERVERURL=http://finanzen.de.yahoo.com
#STOCKS=185775.F
##FORMAT=snl1d1t1c1p2poghwerdy
#FORMAT=snl1d1t1c1p2poghwv
#a2werr1dyj1

cd /tmp
rm -vf quote*csv*
wget -q "$SERVERURL/d/quotes.csv?f=$FORMAT&s=$STOCKS" 
cat quote*csv*




-- 
Those are my principles, and if you don't like them... well, I have others.
                                                -- Groucho Marx

Other related posts: