Lately I was in need of a financial gadget that can provide stock exchange and currency data. I guess it goes without saying that you can find many solutions for the problem; in my case though none of them (that I found) was good enough to work with. I needed a free, reliable and highly customizable API. Unfortunately most of the free ones either come with an ugly copyright line which means a no go (why would you want to advertise your competition?), or they have some customization issues.
Since Google is no longer supporting it’s financial APIs (also it was a huge overkill compared to my simple needs) I found that Yahoo!’s Open Data Tables provides exactly what I want. It is well documented, easy-to-use, customizable and free (for my notes about reliability check out the last section of this post).
The code utilizes built-in ajax feature of jQuery. The URL of the request consists of three parts: the base url (http://query.yahooapis.com/v1/public/yql), the YQL query (you can read more about YQL here) and the configuration parameters (format=JSON etc.).
Query Processing
function YQLRequest( queryString, callback ){
var url = "http://query.yahooapis.com/v1/public/yql?q=" + queryString +
"&format=json&env=http://datatables.org/alltables.env&callback=?",
jQuery.getJSON(url, callback);
}
Stock Quote
function getQuotes(){
var q = escape( 'select * from yahoo.finance.quotes where symbol in ("AAPL","GOOG")' );
YQLRequest( q, processQuotes );
}
function processQuotes( data ){
var quotes = data.query.results.quote;
for( var i in quotes ) {
// Do whatever you want with your data
alert( quotes[i].Name + ' - ' + quotes[i].Ask + ' - ' + quotes[i].Bid );
}
}
Exchange Rates
function getExchangeRates(){
var q = escape( 'select * from yahoo.finance.xchange where pair in ("EURUSD","USDCAD")' );
YQLRequest( q, processExchangeRates );
}
function processExchangeRates( data ){
var rates = data.query.results.rate;
for( var i in rates ) {
// Do whatever you want with your data
alert( rates[i].Name + ' - ' + rates[i].Ask + ' - ' + rates[i].Bid );
}
}
Reliability
While I was working on the script I had noticed some unexpected issues: null values and rejected requests. The latter one might be caused by the usage limit (1000 requests / hour / IP), but the problem was persistent even after an IP renewal. For this reason I implemented a cache which checks whether the response contains the expected values, if not, it serves the previous version from the cache.
To whom it may concern
When you work with YQL you might want to consider the following few rules and notes taken from the official site. You can find out more at the Yahoo! Developer Network.
Notice
- YQL can be used for commercial purposes
- If we’re going to shut down YQL, we will give you at least 6 months notice with an announcement on this web page and in our forum
- YQL has a performance uptime target of over 99.5%
Usage Limits
- Per application limit (identified by your Access Key): 100,000 calls per day
- Per IP limits: /v1/public/*: 1,000 calls per hour; /v1/yql/*: 10,000 calls per hour
Additional Notes
- All rates are subject to change
- You may also be subject to the underlying rate limits of other Yahoo! and 3rd party web services
- YQL relies on the correct operation of the web services and content providers it accesses
1st UPDATE
Since jQuery has introduced getJSON method I decided its time to refactor my code. This method is basically just a shortcode for an AJAX call optimized for JSON, but possible could save some bytes and improve readabilty.
2nd UPDATE
It turned out that Internet Explorer doesn’t want to play nicely with cross domain requests (thanks everyone for the feedbacks). To resolve the issue I appended the callback parameter to the URL to force jQuery to use JSONP instead of plain JSON. The new version should support IE from now on.

Pingback: Exchange Rates and Stock Quotes with YQL and jQuery | TravelSquare
Hi, nice post. I test it in Firefox and it works fine. However in IE8 doesn’t. Do you know what could be the problem?
Thanks in advance
Hi, Thanks for the interest!
Yeah, I’ve received a few complaints. I figured out the problem is with the XML parser I am using. The code shown in the post does not affected by this problem though. I will fix the demo soon.
Actually, my previous comment doesn’t make much sense, please ignore it. I found the real problem and fixed it. It should be working now even in IE. Thanks.
great post again
Happy Holidays!
Thanks Munkhtur! I wish you too!