替代Google Finance API的方法


430

我想使用Google Finance API来获取有关该公司的股票数据,但是自2011/26/05以来已弃用该API。

您如何使用免费API实时获取库存数据?


1
您还可以使用另一个Google API。我在这里记录过:jarloo.com/real-time-google-stock-api
凯利

11
作为软件开发人员,我推荐Alpha Vantage。他们提供免费的JSON API,用于实时和历史股票报价。是他们的MSFT实时盘中数据。是他们的完整API文档。您确实需要一个API密钥,可以在其网站上免费获得。
史蒂夫·卡里诺

4
截至2017年9月,请参阅以下问题:stackoverflow.com/questions/46070126/…–
augustomen

2
Alpha Vantage很棒。我刚写了一个博客帖子大约是the-data-wrangler.com/...
阿什利·戴维斯

Answers:


427

更新答案

1.试用Alpha Vantage API

对于初学者,您可以尝试从查询中获取JSON输出,例如

https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=demo

不要尝试Yahoo Finance API(已弃用或现在不可用)。

对于初学者,您可以通过简单的API调用生成CSV:

http://finance.yahoo.com/d/quotes.csv?s=AAPL+GOOG+MSFT&f=sb2b3jk

(这将为AAPL,GOOG和MSFT生成并保存CSV)

请注意,您必须将格式附加到查询字符串(f=..)。有关所有格式的概述,请参见此页面

有关更多示例,请访问此页面

对于基于XMLJSON的数据,您可以执行以下操作:

不要使用YQL(雅虎查询语言)**

例如:

http://developer.yahoo.com/yql/console/?q=select%20*%20from%20yahoo.finance
.quotes%20where%20symbol%20in%20(%22YHOO%22%2C%22AAPL%22%2C%22GOOG%22%2C%22
MSFT%22)%0A%09%09&env=http%3A%2F%2Fdatatables.org%2Falltables.env

2.使用网络服务

例如,要获取所有股票报价XML

http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote

要获取中的所有股票报价JSON,只需将其添加format=JSON到URL的末尾:

http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format=json

备择方案:

1. 货币API

  • 超过165种实时货币汇率,包括少量加密货币。文档在这里

2. 1Forge金融API

  • 此处提供约40种货币对的实时汇率。

3. 财务内容API

  • 该API 的文档非常好。

4. 开放汇率

5. Oanda API

6. XE API

7. Xignite API

8. currencylayer API

9.其他API-programmableWeb上讨论



7
值得注意的是Yahoo! 关于拉出股票价格的说法:“似乎有些人对它们用来提取财务数据的API进行了反向工程,但它们违反了我们的服务条款(不重新分配财务数据)...仅当您使用时才允许重新分配徽章的团队已经创建:finance.yahoo.com/badges否则,你可以用YQL或任何方法来获取数据归个人使用”。developer.yahoo.com/forum/General-Discussion-at-YDN/...
poshaughnessy

3
而不是保存到csv ...可以代替JSON抓取数据吗?
bouncingHippo 2012年

3
值得注意的是,雅虎的端点不是实时的,延迟了15分钟(例如,请参见YQL中的LastTradeWithTime字段)
artur 2014年

37
答案需要更新,因为Yahoo Finance API现在已失效。
Vivek Vijayan

47

我来晚了,但请查看Quandl。他们有一个关于股价和基本面API

这是一个示例调用,使用csv中的Quandl-api下载

例:

https://www.quandl.com/api/v1/datasets/WIKI/AAPL.csv?column=4&sort_order=asc&collapse=quarterly&trim_start=2012-01-01&trim_end=2013-12-31

他们支持这些语言。他们的源数据来自Yahoo Finance,Google Finance,NSE,BSE,FSE,HKEX,LSE,SSE,TSE等(请参见此处)。


您使用此API是否有一些技术指标(例如rsi)?
基瓦2014年

我不知道。我实际上不隶属于Quandl,我只知道这是一个获取数据的地方。
user2023861 2014年

这看起来是一个不错的选择,但是您可以在此处获取实时库存数据吗?似乎最短的时间范围是每日数据?
Trevor 2014年

8
Quandl无法提供实时库存数据-我已经尝试过了。
Brian Goodwin 2015年

2
Quandl的WIKI数据集中也缺少很多股票。
杰里米·霍洛瓦克斯

16

我建议使用TradeKing的开发人员API。它非常好,可以免费使用。所需要做的就是与他们建立一个帐户,据我所知,您不必携带余额...仅需注册即可。


2
并且创建该帐户是如此“有趣”,以至于我在10分钟后取消了...
inselberg 2013年

2
如果我要问,那有什么“搞笑”的地方?您是指他们要求一些敏感的个人信息吗?任何在线股票经纪人都会要求提供相同的信息...这并不稀奇。
2015年

11
SIN,出生日期,婚姻状况,家属...?认真吗 我只想调用API进行测试
Daniel B

我发现的其他API是quotemedia和盟友。
Deepan Prabhu Babu

7

我遵循了最重要的答案,并开始关注Yahoo Finance。可以通过许多不同的方式访问其API,但我在此处找到了以CSV格式获取股票信息的不错参考:http : //www.jarloo.com/

使用它,我编写了这个脚本。我不是一个真正的红宝石家伙,但这可能会帮助您一起破解某些东西。我还没有为yahoo提供的所有字段提供变量名,因此您可以在需要时填写它们。

这是用法

TICKERS_SP500 = "GICS,CIK,MMM,ABT,ABBV,ACN,ACE,ACT,ADBE,ADT,AES,AET,AFL,AMG,A,GAS,APD,ARG,AKAM,AA,ALXN,ATI,ALLE,ADS,ALL,ALTR,MO,AMZN,AEE,AAL,AEP,AXP,AIG,AMT,AMP,ABC,AME,AMGN,APH,APC,ADI,AON,APA,AIV,AAPL,AMAT,ADM,AIZ,T,ADSK,ADP,AN,AZO,AVGO,AVB,AVY,BHI,BLL,BAC,BK,BCR,BAX,BBT,BDX,BBBY,BBY,BIIB,BLK,HRB,BA,BWA,BXP,BSX,BMY,BRCM,BFB,CHRW,CA,CVC,COG,CAM,CPB,COF,CAH,HSIC,KMX,CCL,CAT,CBG,CBS,CELG,CNP,CTL,CERN,CF,SCHW,CHK,CVX,CMG,CB,CI,XEC,CINF,CTAS,CSCO,C,CTXS,CLX,CME,CMS,COH,KO,CCE,CTSH,CL,CMA,CSC,CAG,COP,CNX,ED,STZ,GLW,COST,CCI,CSX,CMI,CVS,DHI,DHR,DRI,DVA,DE,DLPH,DAL,XRAY,DVN,DO,DTV,DFS,DG,DLTR,D,DOV,DOW,DPS,DTE,DD,DUK,DNB,ETFC,EMN,ETN,EBAY,ECL,EIX,EW,EA,EMC,EMR,ENDP,ESV,ETR,EOG,EQT,EFX,EQIX,EQR,ESS,EL,ES,EXC,EXPE,EXPD,ESRX,XOM,FFIV,FB,FDO,FAST,FDX,FIS,FITB,FSLR,FE,FISV,FLIR,FLS,FLR,FMC,FTI,F,FOSL,BEN,FCX,FTR,GME,GCI,GPS,GRMN,GD,GE,GGP,GI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URBN,VFC,VLO,VAR,VTR,VRSN,VZ,VRTX,VIAB,V,VNO,VMC,WMT,WBA,DIS,WM,WAT,ANTM,WFC,WDC,WU,WY,WHR,WFM,WMB,WIN,WEC,WYN,WYNN,XEL,XRX,XLNX,XL,XYL,YHOO,YUM,ZMH,ZION,ZTS,SAIC,AP"

AllData = loadStockInfo(TICKERS_SP500, allParameters())

SpecificData = loadStockInfo("GOOG,CIK", "ask,dps")

loadStockInfo返回一个散列,以使SpecificData [“ GOOG”] [“ name”]为“ Google Inc.”。

最后,运行该代码的实际代码...

require 'net/http'

# Jack Franzen & Garin Bedian
# Based on http://www.jarloo.com/yahoo_finance/

$parametersData = Hash[[

    ["symbol", ["s", "Symbol"]],
    ["ask", ["a", "Ask"]],
    ["divYield", ["y", "Dividend Yield"]],
    ["bid", ["b", "Bid"]],
    ["dps", ["d", "Dividend per Share"]],
    #["noname", ["b2", "Ask (Realtime)"]],
    #["noname", ["r1", "Dividend Pay Date"]],
    #["noname", ["b3", "Bid (Realtime)"]],
    #["noname", ["q", "Ex-Dividend Date"]],
    #["noname", ["p", "Previous Close"]],
    #["noname", ["o", "Open"]],
    #["noname", ["c1", "Change"]],
    #["noname", ["d1", "Last Trade Date"]],
    #["noname", ["c", "Change & Percent Change"]],
    #["noname", ["d2", "Trade Date"]],
    #["noname", ["c6", "Change (Realtime)"]],
    #["noname", ["t1", "Last Trade Time"]],
    #["noname", ["k2", "Change Percent (Realtime)"]],
    #["noname", ["p2", "Change in Percent"]],
    #["noname", ["c8", "After Hours Change (Realtime)"]],
    #["noname", ["m5", "Change From 200 Day Moving Average"]],
    #["noname", ["c3", "Commission"]],
    #["noname", ["m6", "Percent Change From 200 Day Moving Average"]],
    #["noname", ["g", "Day’s Low"]],
    #["noname", ["m7", "Change From 50 Day Moving Average"]],
    #["noname", ["h", "Day’s High"]],
    #["noname", ["m8", "Percent Change From 50 Day Moving Average"]],
    #["noname", ["k1", "Last Trade (Realtime) With Time"]],
    #["noname", ["m3", "50 Day Moving Average"]],
    #["noname", ["l", "Last Trade (With Time)"]],
    #["noname", ["m4", "200 Day Moving Average"]],
    #["noname", ["l1", "Last Trade (Price Only)"]],
    #["noname", ["t8", "1 yr Target Price"]],
    #["noname", ["w1", "Day’s Value Change"]],
    #["noname", ["g1", "Holdings Gain Percent"]],
    #["noname", ["w4", "Day’s Value Change (Realtime)"]],
    #["noname", ["g3", "Annualized Gain"]],
    #["noname", ["p1", "Price Paid"]],
    #["noname", ["g4", "Holdings Gain"]],
    #["noname", ["m", "Day’s Range"]],
    #["noname", ["g5", "Holdings Gain Percent (Realtime)"]],
    #["noname", ["m2", "Day’s Range (Realtime)"]],
    #["noname", ["g6", "Holdings Gain (Realtime)"]],
    #["noname", ["k", "52 Week High"]],
    #["noname", ["v", "More Info"]],
    #["noname", ["j", "52 week Low"]],
    #["noname", ["j1", "Market Capitalization"]],
    #["noname", ["j5", "Change From 52 Week Low"]],
    #["noname", ["j3", "Market Cap (Realtime)"]],
    #["noname", ["k4", "Change From 52 week High"]],
    #["noname", ["f6", "Float Shares"]],
    #["noname", ["j6", "Percent Change From 52 week Low"]],
    ["name", ["n", "Company Name"]],
    #["noname", ["k5", "Percent Change From 52 week High"]],
    #["noname", ["n4", "Notes"]],
    #["noname", ["w", "52 week Range"]],
    #["noname", ["s1", "Shares Owned"]],
    #["noname", ["x", "Stock Exchange"]],
    #["noname", ["j2", "Shares Outstanding"]],
    #["noname", ["v", "Volume"]],
    #["noname", ["a5", "Ask Size"]],
    #["noname", ["b6", "Bid Size"]],
    #["noname", ["k3", "Last Trade Size"]],
    #["noname", ["t7", "Ticker Trend"]],
    #["noname", ["a2", "Average Daily Volume"]],
    #["noname", ["t6", "Trade Links"]],
    #["noname", ["i5", "Order Book (Realtime)"]],
    #["noname", ["l2", "High Limit"]],
    #["noname", ["e", "Earnings per Share"]],
    #["noname", ["l3", "Low Limit"]],
    #["noname", ["e7", "EPS Estimate Current Year"]],
    #["noname", ["v1", "Holdings Value"]],
    #["noname", ["e8", "EPS Estimate Next Year"]],
    #["noname", ["v7", "Holdings Value (Realtime)"]],
    #["noname", ["e9", "EPS Estimate Next Quarter"]],
    #["noname", ["s6", "evenue"]],
    #["noname", ["b4", "Book Value"]],
    #["noname", ["j4", "EBITDA"]],
    #["noname", ["p5", "Price / Sales"]],
    #["noname", ["p6", "Price / Book"]],
    #["noname", ["r", "P/E Ratio"]],
    #["noname", ["r2", "P/E Ratio (Realtime)"]],
    #["noname", ["r5", "PEG Ratio"]],
    #["noname", ["r6", "Price / EPS Estimate Current Year"]],
    #["noname", ["r7", "Price / EPS Estimate Next Year"]],
    #["noname", ["s7", "Short Ratio"]

]]

def replaceCommas(data)
    s = ""
    inQuote = false
    data.split("").each do |a|
        if a=='"'
            inQuote = !inQuote
            s += '"'
        elsif !inQuote && a == ","
            s += "#"
        else
            s += a
        end
    end
    return s
end

def allParameters()
    s = ""
    $parametersData.keys.each do |i|
        s  = s + i + ","
    end
    return s
end

def prepareParameters(parametersText)
    pt = parametersText.split(",")
    if !pt.include? 'symbol'; pt.push("symbol"); end;
    if !pt.include? 'name'; pt.push("name"); end;
    p = []
    pt.each do |i|
        p.push([i, $parametersData[i][0]])
    end
    return p
end

def prepareURL(tickers, parameters)
    urlParameters = ""
    parameters.each do |i|
        urlParameters += i[1]
    end
    s = "http://download.finance.yahoo.com/d/quotes.csv?"
    s = s + "s=" + tickers + "&"
    s = s + "f=" + urlParameters
    return URI(s)
end

def loadStockInfo(tickers, parametersRaw)
    parameters = prepareParameters(parametersRaw)
    url = prepareURL(tickers, parameters)
    data = Net::HTTP.get(url)
    data = replaceCommas(data)
    h = CSVtoObject(data, parameters)
    logStockObjects(h, true)
end

#parse csv
def printCodes(substring, length)

    a = data.index(substring)
    b = data.byteslice(a, 10)
    puts "printing codes of string: "
    puts b
    puts b.split('').map(&:ord).to_s
end

def CSVtoObject(data, parameters)
    rawData = []
    lineBreaks = data.split(10.chr)
    lineBreaks.each_index do |i|
        rawData.push(lineBreaks[i].split("#"))
    end

    #puts "Found " + rawData.length.to_s + " Stocks"
    #puts "   w/ " + rawData[0].length.to_s + " Fields"

    h = Hash.new("MainHash")
    rawData.each_index do |i|
        o = Hash.new("StockObject"+i.to_s)
        #puts "parsing object" + rawData[i][0]
        rawData[i].each_index do |n|
            #puts "parsing parameter" + n.to_s + " " +parameters[n][0]
            o[ parameters[n][0] ] = rawData[i][n].gsub!(/^\"|\"?$/, '')
        end
        h[o["symbol"]] = o;
    end
    return h
end

def logStockObjects(h, concise)
    h.keys.each do |i|
        if concise
            puts "(" + h[i]["symbol"] + ")\t\t" + h[i]["name"]
        else
            puts ""
            puts h[i]["name"]
            h[i].keys.each do |p|
                puts "    " + $parametersData[p][1] + " : " + h[i][p].to_s
            end
        end
    end
end

1
这真的很有用,我希望我能给你更多的选票。快速问题-您是否发现此API可靠,引号是否真正实时?我知道Yahoo的某些信息会延迟,这似乎取决于您访问的特定API。
洪德利2015年

我这样做是为了帮助一个朋友,我认为这最终对他很有效。该信息非常快,如果我记得正确的话,我想他们每10/15分钟更新一次。
杰克·弗兰森

1
C#版本和教程在这里:jarloo.com/yahoo_finance
Kelly,

是的,那个家伙显然知道如何获取股票信息哈哈。他在过去2个月中还有其他教程,声称您仍然可以从Google获得实时股票数据
Jack Franzen

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.