Skip to main content

Fetching Stock Data From Yahoo For iOS Applications

Yahoo! provides a great service called Yahoo! Query Language (YQL) which provides a SQL-like interface to a whole bunch of Web services. YQL queries can also be performed via REST. A wide range of services can be accessed through YQL including Yahoo! Finance.


A convenient way to play with YQL and explore the available Web services, is to use the YQL Console. The bottom right of the console lists all the data tables, grouped by provider. (Be sure to click "Show Community Tables" to view all the available tables.) The yahoo provider includes the data table yahoo.finance.quote. This table provides access to stock quotes from Yahoo! Finance. As an example, here's the YQL console showing a query for stock data for AAPL. The console helpfully shows the REST URL for the query at the bottom.


Armed with the REST URL, it is trivial to write code in Objective-C/iOS to fetch quote data. Here's some code that takes in an NSArray of ticker symbols (NSStrings) and returns a NSDictionary with tickers as keys and quotes (realtime bid) as values.


#define QUOTE_QUERY_PREFIX @"http://query.yahooapis.com/v1/public/yql?q=select%20symbol%2C%20BidRealtime%20from%20yahoo.finance.quotes%20where%20symbol%20in%20("
#define QUOTE_QUERY_SUFFIX @")&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback="

+ (NSDictionary *)fetchQuotesFor:(NSArray *)tickers
{
NSMutableDictionary *quotes;
if (tickers && [tickers count] > 0) {
NSMutableString *query = [[NSMutableString alloc] init];
[query appendString:QUOTE_QUERY_PREFIX];
for (int i = 0; i < [tickers count]; i++) {
            NSString *ticker = [tickers objectAtIndex:i];
            [query appendFormat:@"%%22%@%%22", ticker];
            if (i != [tickers count] - 1) [query appendString:@"%2C"];
        }
        [query appendString:QUOTE_QUERY_SUFFIX];
        // NSLog(@"Query: %@", query);
        NSData *jsonData = [[NSString stringWithContentsOfURL:[NSURL URLWithString:query] encoding:NSUTF8StringEncoding error:nil] dataUsingEncoding:NSUTF8StringEncoding];
        NSError *error = nil;
        NSDictionary *results = jsonData ? [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error] : nil;
        if (error) NSLog(@"[%@ %@] JSON error: %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), error.localizedDescription);
        // NSLog(@"[%@ %@] received %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), results);
        NSArray *quoteEntries = [results valueForKeyPath:@"query.results.quote"];
        quotes = [[NSMutableDictionary alloc] initWithCapacity:[quoteEntries count]];
        for (NSDictionary *quoteEntry in quoteEntries) {
            [quotes setValue:[quoteEntry valueForKey:@"BidRealtime"] forKey:[quoteEntry valueForKey:@"symbol"]];
        }
    }
    return quotes;
}

Comments

Popular posts from this blog

FCC Aproves Sirius-XM Merger

This has been a long time coming but finally the FCC has finally approved the merger of Sirius Satellite Radio with XM Satellite Radio . The combined entity is pretty much a monopoly in the satellite radio space but they are still competing with terrestrial radio. Either way, their stocks, NASDAQ:SIRI and NASDAQ:XMSR should get a good boost on Monday. Yahoo! Finance Quote for SIRI Quote for XMSR

Lead Tide SIM Reader

I recently came across a cheap little device for reading SIM cards . It was available from Meritline for less than USD 5 with free shipping. Curious to see what it was like, I ordered one. The device came in a small package along with a mini CD containing drivers. The packaging advertised the device as the LEAD TIDE Sim reader . Like most things these days, it's made in China. The device has a USB 1.1 interface. There was no product code or number anywhere on the packaging. Installing the drivers for the device turned out to be harder than I expected. The mini CD's autorun installed some stuff but Microsoft Windows XP couldn't install any suitable driver for the device. The mini-CD had several top level directories with what appeared to be product codes but I couldn't match any to the device itself since it had no product code. Google searches revealed that I wasn't alone in my endeavors to get the device working . Further digging revealed pointers to some thir

Getting Mailvelope on Chrome to use GnuPG on macOS

Mailvelope is a browser add-on that helps use GPG encryption and signing on webmail systems like Gmail. Here are the steps on macOS (tested with "Big Sur") to get Mailvelope to use the GnuPG backend. Install gpgme via Homebrew: brew install gpgme This will install gpgme-json in /usr/local/bin by default.  Create a file called gpgmejson.json in  "~/Library/Application Support/Google/Chrome/NativeMessagingHosts" with the following contents: {     "name": "gpgmejson",     "description": "Integration with GnuPG",     "path": "/usr/local/bin/gpgme-json",     "type": "stdio",     "allowed_origins": [         "chrome-extension://kajibbejlbohfaggdiogboambcijhkke/"     ] } Now in Mailvelope > Options > General , GnuPG will show up as the encryption backend.