Saturday, January 5, 2013

Accessing Yahoo Finance API

Since last few days I was wondering the right set of Web Service to read Country wise Stock Exchange index information. I found a bunch of scattered information, but no straight forward answer. It seems there are not many "reliable" and "flexible" options and Yahoo Finance is one of the top of this class.

Though Yahoo Finance is very powerful, some how its very less documented and it seems Yahoo doesn't care much about this wonderful web service and expect Developers to do some kind of "hacking". The only online resource that I (and most of you as well) found is one 3rd party web site- http://www.gummy-stuff.org/Yahoo-data.htm and it seems they know much more than what Yahoo dose..;-)

Anyway let me continue and share my experience and information to help budding developer who wants to use Yahoo Finance Web Service in their Mobile, Web or Desktop solution.

There are 2 set of APIs to access Yahoo Finance details-

YQL based Web Service: This API abstracts web services as a SQL Table. Unfortunately YQL doesn't work for Finance API. Yahoo has removed the Finance YQL table and only "work-around" is to use one deprecated table stored in Git Hub (http://github.com/spullara).
We need to run following query on YQL Console to access Yahoo Finance (Thanks to YDN)
use "http://github.com/spullara/yql-tables/raw/d60732fd4fbe72e5d5bd2994ff27cf58ba4d3f84/yahoo/finance/yahoo.finance.quotes.xml" as quotes;
select * from quotes where symbol in("YHOO","AAPL","GOOG","MSFT")

YQL (Yahoo Query Language)
Yahoo has their own version of Web Service Mashup solution (YQL), which can read data from various existing Yahoo web services (Flickr, Places etc.). It provides one wonderful execution environment (YQL Console- http://developer.yahoo.com/yql/console/) to test web service queries before using them in actual implementation.

CSV based Web Service: This API is stable and backbone for YQL. As YQL feature seems broken for Finance API, so only stable solution is CSV based API.  Following section covers more details about this API.
Yahoo Finance Symbols
each Finance element either Stock exchange or Business unit has one unique Symbol which is prefixed with ^ (ASCII code: 5E) e.g. ^BSESN (Bombay Stock Exchange SeNsex). You can find list of Country wise Stock Index symbols in - http://finance.yahoo.com/indices  
Fields or Columns of Finance API
we can specify fields we want in CSV response. Each field has unique code (e.g. t1 - Last Trading Time, p2 - % change in Stock Index), complete Fields and codes are mentioned in http://www.gummy-stuff.org/Yahoo-data.htm

Well that's all you need to know to access Yahoo Finance API. Now let's see one example URL which reads Stock Index of 3 countries- (BSESN- Index of India, NYA- Index of New York Stock Exchange and HSI- Index of Hong Kong)-

Request URL
http://download.finance.yahoo.com/d/quotes.csv?s=%5EBSESN+%5ENYA+%5EHSI&f=sl1d1t1

As you can easily understand, 
  • Symbols (s) parameter can contain multiple Indices separated by "+" and most important thing, ^ character should be escaped using HTTP URL Encoding.
  • Fields (f) parameter contains s(Symbol), l1(Last Trade Price), d1(Last Trade Date) and t1(Last Trade Time)
Response
"^BSESN",19784.08,"1/4/2013","5:10am"
"^NYA",8667.678,"1/4/2013","4:15pm"
"^HSI",23331.09,"1/4/2013","3:01am"


Response is in standard CSV (Comma Separated Values) format and can be easily parsed in any Programming Language using in-built String parsing methods.