【问题标题】:Using IEX API for real-time stock info (Yahoo Finance replacement)?使用 IEX API 获取实时股票信息(雅虎财经替代)?
【发布时间】:2018-04-30 05:47:47
【问题描述】:

正如标题所说,我正在寻找股票信息的替代来源,因为雅虎已经禁用了许多人一直在使用的 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

【问题讨论】:

  • 更新:尝试了以下代码(在问题部分,因为它对于评论来说太长了)并且它适用于价格,但不适用于市盈率或除“报价”之外的任何其他参数。知道为什么吗?

标签: excel api stockquotes elixir-iex vba


【解决方案1】:

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

Sub IEX()
Dim Price As Single

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

End Sub

【讨论】:

  • 我只能使用 Excel 在线测试 web 服务功能作为标准功能,因为我目前使用的计算机正在运行 Excel 2010。如果我走这条路,它似乎也不会很难转换为工作簿函数,然后我可以用它来引用单元格中的代码。但是,我倾向于远离 web 服务的想法,只是为了让功能通过不同版本的 Excel 保留。感谢您的回复!在此期间,我可能最终会使用您的建议,因为它似乎正在实现我的目标。
【解决方案2】:

我想我已经基本解决了这个问题。这是任何有兴趣的人的代码。这可以直接替代那些使用 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/

【讨论】:

  • 嘿,谢谢你……它解决了我的问题。我还发现这个 JSON 记录了每个函数,它的参数和响应:cloud.iexapis.com/stable
【解决方案3】:

如果您不需要向后兼容 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

确保在打开工作簿时启用宏,这样才能正常工作。 (这在 2017 年使用 Mac Excel 2011 和 High Sierra 进行了测试。

【讨论】:

    【解决方案4】:

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

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

    适用于 Office 365 的 Excel。

    【讨论】:

    • 您的回答措辞不当。请在发布前阅读 SO 指南。
    • @sparkplug,我完全理解这篇文章的措辞。赞成,因为它是正确的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-11
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    相关资源
    最近更新 更多