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- 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 (
We need to run following query on YQL Console to access Yahoo Finance (Thanks to YDN)
use "" 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- 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 -  
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

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

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 is in standard CSV (Comma Separated Values) format and can be easily parsed in any Programming Language using in-built String parsing methods.