我正在努力寻找一种将 IPO 股票从雅虎金融 API 中撤出的方法

Posted

技术标签:

【中文标题】我正在努力寻找一种将 IPO 股票从雅虎金融 API 中撤出的方法【英文标题】:I am struggling finding a way to pull IPO stocks out of the yahoo finance api 【发布时间】:2016-07-16 16:21:05 【问题描述】:

我对 Alamofire 很陌生,我正在尝试找到一种方法,只在搜索队列中显示 IPO,而不是在雅虎财务数据库中显示每只股票。有没有一种方法可以限制日期和时间戳显示的结果。作为参考,我使用了很多 swift 股票代码/示例,所以我希望有人能提供帮助。谢谢。我放了一些来自 yahoo api 的代码。我希望只显示特定价格的股票和/或按日期显示相对较新的股票。谢谢你。

import UIKit

导入 Alamofire

结构库存搜索结果 var 符号:字符串? 变量名称:字符串? var 交换:字符串? 变量资产类型:字符串?

结构库存

var ask: String?
var averageDailyVolume: String?
var bid: String?
var bookValue: String?
var changeNumeric: String?
var changePercent: String?
var dayHigh: String?
var dayLow: String?
var dividendShare: String?
var dividendYield: String?
var ebitda: String?
var epsEstimateCurrentYear: String?
var epsEstimateNextQtr: String?
var epsEstimateNextYr: String?
var eps: String?
var fiftydayMovingAverage: String?
var lastTradeDate: String?
var last: String?
var lastTradeTime: String?
var marketCap: String?
var companyName: String?
var oneYearTarget: String?
var open: String?
var pegRatio: String?
var peRatio: String?
var previousClose: String?
var priceBook: String?
var priceSales: String?
var shortRatio: String?
var stockExchange: String?
var symbol: String?
var twoHundreddayMovingAverage: String?
var volume: String?
var yearHigh: String?
var yearLow: String?

var dataFields: [[String : String]]

结构图点 变量日期:NSDate? var 音量:整数? var open: CGFloat? var close: CGFloat? var 低:CGFloat? var high: CGFloat?

枚举 ChartTimeRange 案例 OneDay, FiveDays, TenDays, OneMonth, ThreeMonths, OneYear, FiveYears

类 SwiftStockKit

class func fetchStocksFromSearchTerm(term term: String, completion:(stockInfoArray: [StockSearchResult]) -> ()) 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) 

        let searchURL = "http://autoc.finance.yahoo.com/autoc"

        Alamofire.request(.GET, searchURL, parameters: ["query": term, "region": 2, "lang": "en"]).responseJSON  response in

            if let resultJSON = response.result.value as? [String : AnyObject]  

                if let jsonArray = (resultJSON["ResultSet"] as! [String : AnyObject])["Result"] as? [[String : String]] 

                    var stockInfoArray = [StockSearchResult]()
                    for dictionary in jsonArray 
                        stockInfoArray.append(StockSearchResult(symbol: dictionary["symbol"], name: dictionary["name"], exchange: dictionary["exchDisp"], assetType: dictionary["typeDisp"]))
                    

                    dispatch_async(dispatch_get_main_queue()) 
                        completion(stockInfoArray: stockInfoArray)
                    
                
            
        
    


class func fetchStockForSymbol(symbol symbol: String, completion:(stock: Stock) -> ()) 

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) 

        let stockURL = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22\(symbol)%22)&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&format=json"

        Alamofire.request(.GET, stockURL).responseJSON  response in

            if let resultJSON = response.result.value as? [String : AnyObject]  

                if let stockData = ((resultJSON["query"] as! [String : AnyObject])["results"] as! [String : AnyObject])["quote"] as? [String : AnyObject] 

                    // lengthy creation, yeah
                    var dataFields = [[String : String]]()

                    dataFields.append(["Ask" : stockData["Ask"] as? String ?? "N/A"])
                    dataFields.append(["Average Daily Volume" : stockData["AverageDailyVolume"] as? String ?? "N/A"])
                    dataFields.append(["Bid" : stockData["Bid"] as? String ?? "N/A"])
                    dataFields.append(["Book Value" : stockData["BookValue"] as? String ?? "N/A"])
                    dataFields.append(["Change" : stockData["Change"] as? String ?? "N/A"])
                    dataFields.append(["Percent Change" : stockData["ChangeinPercent"] as? String ?? "N/A"])
                    dataFields.append(["Day High" : stockData["DaysHigh"] as? String ?? "N/A"])
                    dataFields.append(["Day Low" : stockData["DaysLow"] as? String ?? "N/A"])
                    dataFields.append(["Div/Share" : stockData["DividendShare"] as? String ?? "N/A"])
                    dataFields.append(["Div Yield" : stockData["DividendYield"] as? String ?? "N/A"])
                    dataFields.append(["EBITDA" : stockData["EBITDA"] as? String ?? "N/A"])
                    dataFields.append(["Current Yr EPS Estimate" : stockData["EPSEstimateCurrentYear"] as? String ?? "N/A"])
                    dataFields.append(["Next Qtr EPS Estimate" : stockData["EPSEstimateNextQuarter"] as? String ?? "N/A"])
                    dataFields.append(["Next Yr EPS Estimate" : stockData["EPSEstimateNextYear"] as? String ?? "N/A"])
                    dataFields.append(["Earnings/Share" : stockData["EarningsShare"] as? String ?? "N/A"])
                    dataFields.append(["50D MA" : stockData["FiftydayMovingAverage"] as? String ?? "N/A"])
                    dataFields.append(["Last Trade Date" : stockData["LastTradeDate"] as? String ?? "N/A"])
                    dataFields.append(["Last" : stockData["LastTradePriceOnly"] as? String ?? "N/A"])
                    dataFields.append(["Last Trade Time" : stockData["LastTradeTime"] as? String ?? "N/A"])
                    dataFields.append(["Market Cap" : stockData["MarketCapitalization"] as? String ?? "N/A"])
                    dataFields.append(["Company" : stockData["Name"] as? String ?? "N/A"])
                    dataFields.append(["One Yr Target" : stockData["OneyrTargetPrice"] as? String ?? "N/A"])
                    dataFields.append(["Open" : stockData["Open"] as? String ?? "N/A"])
                    dataFields.append(["PEG Ratio" : stockData["PEGRatio"] as? String ?? "N/A"])
                    dataFields.append(["PE Ratio" : stockData["PERatio"] as? String ?? "N/A"])
                    dataFields.append(["Previous Close" : stockData["PreviousClose"] as? String ?? "N/A"])
                    dataFields.append(["Price-Book" : stockData["PriceBook"] as? String ?? "N/A"])
                    dataFields.append(["Price-Sales" : stockData["PriceSales"] as? String ?? "N/A"])
                    dataFields.append(["Short Ratio" : stockData["ShortRatio"] as? String ?? "N/A"])
                    dataFields.append(["Stock Exchange" : stockData["StockExchange"] as? String ?? "N/A"])
                    dataFields.append(["Symbol" : stockData["Symbol"] as? String ?? "N/A"])
                    dataFields.append(["200D MA" : stockData["TwoHundreddayMovingAverage"] as? String ?? "N/A"])
                    dataFields.append(["Volume" : stockData["Volume"] as? String ?? "N/A"])
                    dataFields.append(["52w High" : stockData["YearHigh"] as? String ?? "N/A"])
                    dataFields.append(["52w Low" : stockData["YearLow"] as? String ?? "N/A"])

                    let stock = Stock(
                        ask: dataFields[0].values.first,
                        averageDailyVolume: dataFields[1].values.first,
                        bid: dataFields[2].values.first,
                        bookValue: dataFields[3].values.first,
                        changeNumeric: dataFields[4].values.first,
                        changePercent: dataFields[5].values.first,
                        dayHigh: dataFields[6].values.first,
                        dayLow: dataFields[7].values.first,
                        dividendShare: dataFields[8].values.first,
                        dividendYield: dataFields[9].values.first,
                        ebitda: dataFields[10].values.first,
                        epsEstimateCurrentYear: dataFields[11].values.first,
                        epsEstimateNextQtr: dataFields[12].values.first,
                        epsEstimateNextYr: dataFields[13].values.first,
                        eps: dataFields[14].values.first,
                        fiftydayMovingAverage: dataFields[15].values.first,
                        lastTradeDate: dataFields[16].values.first,
                        last: dataFields[17].values.first,
                        lastTradeTime: dataFields[18].values.first,
                        marketCap: dataFields[19].values.first,
                        companyName: dataFields[20].values.first,
                        oneYearTarget: dataFields[21].values.first,
                        open: dataFields[22].values.first,
                        pegRatio: dataFields[23].values.first,
                        peRatio: dataFields[24].values.first,
                        previousClose: dataFields[25].values.first,
                        priceBook: dataFields[26].values.first,
                        priceSales: dataFields[27].values.first,
                        shortRatio: dataFields[28].values.first,
                        stockExchange: dataFields[29].values.first,
                        symbol: dataFields[30].values.first,
                        twoHundreddayMovingAverage: dataFields[31].values.first,
                        volume: dataFields[32].values.first,
                        yearHigh: dataFields[33].values.first,
                        yearLow: dataFields[34].values.first,
                        dataFields: dataFields
                    )
                    dispatch_async(dispatch_get_main_queue()) 
                        completion(stock: stock)
                    
                
            
        
    


class func fetchChartPoints(symbol symbol: String, range: ChartTimeRange, completion:(chartPoints: [ChartPoint]) -> ()) 

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) 

        //An Alamofire regular responseJSON wont parse the JSONP with a callback wrapper correctly, so lets work around that.
        let chartURL = SwiftStockKit.chartUrlForRange(symbol, range: range)

        Alamofire.request(.GET, chartURL).responseData  response in

            if let data = response.result.value 

                var jsonString =  NSString(data: data, encoding: NSUTF8StringEncoding)!

                jsonString = jsonString.substringFromIndex(30)
                jsonString = jsonString.substringToIndex(jsonString.length-1)

                if let data = jsonString.dataUsingEncoding(NSUTF8StringEncoding) 
                    if let resultJSON = (try? NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(rawValue: 0)))  as? [String : AnyObject] 

                        let series = resultJSON["series"] as! [[String : AnyObject]]
                        var chartPoints = [ChartPoint]()
                        for dataPoint in series 
                            //GMT off by 5 hrs
                             let date = NSDate(timeIntervalSince1970: (dataPoint["Timestamp"] as? Double ?? dataPoint["Date"] as! Double) - 18000.0)

                            chartPoints.append(
                                ChartPoint(
                                date:  date,
                                volume: dataPoint["volume"] as? Int,
                                open: dataPoint["open"] as? CGFloat,
                                close: dataPoint["close"] as? CGFloat,
                                low: dataPoint["low"] as? CGFloat,
                                high: dataPoint["high"] as? CGFloat
                                )
                            )
                        
                        dispatch_async(dispatch_get_main_queue()) 
                             completion(chartPoints: chartPoints)
                        
                    
                
            
        
    

【问题讨论】:

请分享您使用的一些代码。 我只是附上了一些 【参考方案1】:

您可以过滤从服务器获取的 Json 数据。 不确定你到底想要什么。请分享一些代码或详细说明问题

【讨论】:

所以,我只想显示一个月内新的股票。我使用了这个 Github 项目 (github.com/ackleymi/SwiftStock) 中的 swiftStockKit.swift 文件,这就是我访问 yahoo api 的方式。我是在这里过滤数据,还是在另一个 .swift 文件中过滤? 我还附上了一些将应用程序连接到 yahoo api 的代码 有趣的问题。您解决了这个问题,还是仍在寻找答案?

以上是关于我正在努力寻找一种将 IPO 股票从雅虎金融 API 中撤出的方法的主要内容,如果未能解决你的问题,请参考以下文章

定期抓取雅虎财经

我正在努力寻找一种将我的服务器链接到我的 Roblox 游戏的好方法

金融股票知识入门

day32 Python与金融量化分析

雅虎财经:按 ISIN 或 Ticker 搜索,不带后缀

使用股票代码输入、雅虎或谷歌 API 检索公司名称