Code, Code, Revolution!
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:
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.
// 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 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.
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:
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.
- (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!"); }
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.
- (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!"); }
YahooFinance API and the demo application is available at GitHub. Enjoy!
With this blog I try to provide useful tips and solutions for programming .NET, Objective-C and more. My name is Björn Sållarp, and I love writing code.
It's now available on AppStore. It's free and open source. Read more about the app here: Swedish / English
Atif
May 12th, 2011 at 2:22 pm
Thanks for this great work!