使用 IEX API 获取 real-time 股票信息(Yahoo Finance 替代品)?

Using IEX API for real-time stock info (Yahoo Finance replacement)?

正如标题所说,雅虎已经禁用了很多人一直在使用的 API,我正在寻找股票信息的替代来源。我一直在寻找的新资源可以在这里找到:https://iextrading.com/developer/

我的问题是如何将数据实际输入 Excel...我一直在考虑 VBA 因为这是我用来从 Yahoo 获取数据的方法。但是,我认为我想做的远远超出了我目前的能力...我还尝试使用 Excel 的 WEBSERVICE() 函数和以下 URL 来简单地查看价格:https://api.iextrading.com/1.0/stock/aapl/price 但这没有用。据我了解,IEX 为我们免费提供了大量数据,我只是不知道如何访问它们。我对 VBA 的推理是为了能够将工作簿中的输入列表用于代码,并且能够将此数据访问放入许多工作簿中。任何帮助深表感谢。此外,同样欢迎任何关于我可以从哪里开始学习的方向。谢谢。

更新:我评论中提到的代码

Function StockPrice(ticker As String, item As String) As Double

Dim strURL As String, strCSV As Double, itemFound As Integer, tag As String

itemFound = 0
If item = "lastprice" Then
    tag = "price"
    itemFound = 1
ElseIf item = "pe" Then
    tag = "peRatio"
    itemFound = 1

End If

If itemFound = 1 Then

    strURL = "https://api.iextrading.com/1.0/stock/" & ticker & "/" & tag
    Set XMLHTTP = CreateObject("MSXML2.XMLHTTP")
    XMLHTTP.Open "GET", strURL, False
    XMLHTTP.send
    StockPrice = XMLHTTP.responseText
    Set XMLHTTP = Nothing

Else

    StockPrice = "Item Not Found"

End If

End Function

这可能有点简单,但这是一个开始:

Sub IEX()
Dim Price As Single

    Price = Application.WebService("https://api.iextrading.com/1.0/stock/aapl/price")

End Sub

我想我已经基本解决了问题。这是任何感兴趣的人的代码。这可以直接替代那些使用 Yahoo Finance 的 API 的人。

Function StockPrice(ticker As String, item As String)

Dim strURL As String, strCSV As Double, itemFound As Integer, tag As String

itemFound = 0
If item = "lastprice" Then
    tag = "latestPrice"
    itemFound = 1

ElseIf item = "pe" Then
    tag = "peRatio"
    itemFound = 1

ElseIf item = "company" Then
    tag = "companyName"
    itemFound = 1

ElseIf item = "sector" Then
    tag = "sector"
    itemFound = 1

ElseIf item = "open" Then
    tag = "open"
    itemFound = 1

ElseIf item = "yclose" Then
    tag = "previousClose"
    itemFound = 1

ElseIf item = "change" Then
    tag = "change"
    itemFound = 1

ElseIf item = "%change" Then
    tag = "changePercent"
    itemFound = 1

ElseIf item = "marketcap" Then
    tag = "marketCap"
    itemFound = 1

ElseIf item = "52high" Then
    tag = "week52High"
    itemFound = 1

ElseIf item = "52low" Then
    tag = "week52Low"
    itemFound = 1

End If

If itemFound = 1 Then

    strURL = "https://api.iextrading.com/1.0/stock/" & ticker & "/quote/" & tag
    Set XMLHTTP = CreateObject("MSXML2.XMLHTTP")
    XMLHTTP.Open "GET", strURL, False
    XMLHTTP.send
    StockPrice = XMLHTTP.responseText
    Set XMLHTTP = Nothing

Else

    StockPrice = "Item Not Found"

End If

End Function

IEX 的功能比我在这里构建的要多得多。只是没有足够的经验来围绕它进行构建。在此处检查这些功能:https://iextrading.com/developer/docs/

如果您不需要与 Yahoo 的向后兼容性,而只需要简单的报价,此 VBA 函数会在 Excel 函数列表中添加报价功能。

它没有经过完善,但应该作为一个简单的示例来说明如何使用强大的 IEX API。使用 VBA 编辑器将其放入模块中:

Public Function tickerPrice(ticker As String)

Dim htmlCmd As String
Dim curlCmd As String
Dim shellCmd As String
Dim sResult As String

htmlCmd = "https://api.iextrading.com/1.0/stock/" & ticker & "/quote/delayedPrice"
curlCmd = "curl \""" & htmlCmd & "\"""
shellCmd = "do shell script "" " & curlCmd & " "" "

sResult = MacScript(shellCmd)

tickerPrice = Val(sResult)

End Function

打开工作簿时一定要开启Macros,才能正常使用。 (这是在 Mac Excel 2011 年和 High Sierra,2017 年测试的。

在一个单元格(本例中的单元格 E3)中使用股票代码,在另一个单元格中输入以下内容:

=WEBSERVICE("https://api.iextrading.com/1.0/stock/" & E3 & "/quote/delayedPrice")

适用于 Excel for Office 365。