使用IEX API获取实时股票信息(Yahoo Finance replacement)?

时间:2017-11-16 15:50:33

标签: excel excel-vba api stockquotes iex vba

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

我的问题是如何将数据实际存入Excel ...我正在考虑通过VBA,因为这是我用来从雅虎获取数据的。但是,我认为我想要做的事情远远超出我目前的能力......我也尝试使用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

4 个答案:

答案 0 :(得分:3)

这可能有点过分了,但这是一个开始:

Sub IEX()
Dim Price As Single

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

End Sub

答案 1 :(得分: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/

答案 2 :(得分:1)

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

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

在Excel for Office 365中使用。

答案 3 :(得分:0)

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