Downloading Stock quotes to App

限于喜欢 提交于 2019-12-03 21:32:35
Dima

Just deleted my whole answer and rewrote it to avoid confusion:

I looked into how to use YQL to query the yahoo finance API and here's what I ended up with:

Here is the completed code to fully formulate the request string. You can throw this directly into the NSURL for a NSMutableURLRequest and will get a json response. This code will fetch every property of each ticker. To change this you will need to specify individual properties instead of the * in this bit in the prefix (select%20*%20). I took part of it from the sample code in this post. I modified the code to fit into an asynchronous request (also changed it slightly because part of it seemed outdated and wasn't working.

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

+ (NSString *)formulateYQLRequestFor:(NSArray *)tickers
{
    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];

    return query;
}

You would invoke this by doing something like:

NSArray *tickerArray = [[NSArray alloc] initWithObjects:@"AAPL", @"VZ", nil];
NSString *queryURL = [MyClass formulateYQLRequestFor:tickerArray];

Use this answer to see how to formulate the request and consume the json that comes back. Essentially the part you need to change is

NSURL *url = [NSURL URLWithString:queryURL];

You're also not sending JSON over so you should to change the request to reflect that.

My ScriptScraper tool does something similar, it loads the stocks into an Excel table.

cheers, Martin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!