Yahoo Finance API for iOS

YahooFinance is a framework for accessing Yahoo! Finance information in your iOS application. The framework is a cornerstone in my latest project that hopefully will be released on App Store. Here’s a demonstration of what you can do with this framework:

Features

  • Stock symbol search. Free-text search for stocks and find symbols for global stock exchanges.
  • Stock quote details. Yahoo! provide a ton of information for each stock. Price, volume etc.
  • Currency conversion for all major world currencies.

Currency conversion

Yahoo! provide a simple API where you pass in symbols and return conversion rates in CSV-format. Here’s an example of how to use the API, conversion rate for SEK to USD:

http://quote.yahoo.com/d/quotes.csv?f=l1&s=SEKUSD=X

Conversion for multiple currencies can be done in one request by adding multiple “s”-query parameters. Each conversion rate is returned on a new line in the response. Beware though, the API does not accept URLs longer than ~2000 chars which equals around 170 conversions in one go. YahooFinance framework has methods for converting from one currency to another or from multiple currencies to multiple currencies in one go. Both synchronous and asynchronously.

Using YFCurrencyConverter

// Convert a from one currency to another
- (void)testSingleConversionAsynchronous 
{
    YFCurrencyConverter *converter = [YFCurrencyConverter currencyConverterWithDelegate:self];
    [converter convertFromCurrency:@"SEK" toCurrency:@"USD" asynchronous:YES];
}
 
// Convert from many currencies to many currencies in one go
- (void)testMultiConversionAsynchronous 
{
    NSArray *allCurrencies = [NSArray arrayWithObjects:@"SEK", "USD", "EUR", nil];
    YFCurrencyConverter *converter = [YFCurrencyConverter currencyConverterWithDelegate:self];
    [converter convertFromCurrencies:allCurrencies toCurrencies:allCurrencies asychronous:YES];
}
 
- (void)currencyConversionDidFinish:(YFCurrencyConverter *)currencyConverter
{
    if (currencyConverter.batchConversionRates != nil) {
        NSLog(@"Conversion from SEK to USD = %f", [[[currencyConverter.batchConversionRates valueForKey:@"SEK"] valueForKey:@"USD"] floatValue]);
    }
    else {
        NSLog(@"Conversion from %@ to %@ = %f", currencyConverter.fromCurrency, currencyConverter.toCurrency, currencyConverter.conversionRate);
    }
}
 
- (void)currencyConversionDidFail:(YFCurrencyConverter *)currencyConverter
{
   NSLog(@"FAIL!");
}

Yahoo! Finance stock quote information

Yahoo! Finance provide quotes for stocks on most of the large stock exchanges. This framework give you access to the same quote information found on their site. For a list of covered exchanges, check out this site: http://finance.yahoo.com/exchanges. Do note that because this is a free service the quotes are delayed by at least 15 minute! The stock exchange list has more information about delays for each exchange.

Stock symbol search

Yahoo doesn’t provide an official API for looking up symbols, however their own type ahead symbol lookup has JSONP support and works very well. Here’s an example of looking up symbols for Valtech:

http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=Valtech&callback=YAHOO.Finance.SymbolSuggest.ssCallback

The returned format is JSON wrapped with JSONP. To successfully parse this the wrapping function needs to be removed before JSON Framework can take care of conversion to NSDictionary.

Using YFStockSymbolSearch

- (void)testQuickSearch
{
    YFStockSymbolSearch *symbolSearch = [YFStockSymbolSearch symbolSearchWithDelegate:self];
    [symbolSearch findSymbols:@"Yahoo"];
}
 
- (void)symbolSearchDidFinish:(YFStockSymbolSearch *)symbolFinder
{
    for (YFStockSymbol *symbol in symbolFinder.symbols) {
        NSLog(@"Found symbol: %@", symbol.name);
    }
}
 
- (void)symbolSearchDidFail:(YFStockSymbolSearch *)symbolFinder
{
    NSLog(@"FAIL!");
}

Stock quote details

Yahoo! provide loads of stock details through YQL. YQL is a service providing data from loads of sources, one is stock quotes from Yahoo! Finance. Here’s a link to YQL Console giving you quote information for Yahoo!, Google and Apple. This is the API url giving you quote information in JSON format:

http://query.yahooapis.com/v1/public/yql?q=….

One missing bit of information in the YQL data is the stock quote currency. The stock symbol however has a suffix mapping it to a stock exchange and by visiting the stock exchange list you get the suffix mapping. Included in this framework is a plist file that map the quote suffix to the country of the stock exchange. If the suffix doesn’t match a currency in this plist-file it’s assumed that the currency is USD.

Using YFStockDetailsLoader

- (void)testLoadDetailsAsynchronous
{
    YFStockDetailsLoader *loader = [YFStockDetailsLoader loaderWithDelegate:self];
    [loader loadDetails:@"YHOO"];
}
 
- (void)stockDetailsDidLoad:(YFStockDetailsLoader *)detailsLoader
{
    NSLog(@"%@ Bid: %@", detailsLoader.stockDetails.name, detailsLoader.stockDetails.bid);
}
 
- (void)stockDetailsDidFail:(YFStockDetailsLoader *)detailsLoader
{
    NSLog(@"FAIL!");
}

Download sample project

YahooFinance API and the demo application is available at GitHub. Enjoy!

6 thoughts on “Yahoo Finance API for iOS

  1. Thanks for your nice tutorial sir…
    your code on github not working , here the file missing of ASI and JSOn , Apple folder. and secondly if i m replaced the file , i found the error LLVM3.0 (Not working on ios5.1). please check this.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>