2017-07-30 51 views
1

由于从雅虎财务转换为不支持自动下载,我查看了其他资源,www.alphavantage.co似乎符合我的要求。但是,数据不会以excel的形式到达。有没有人在那里编程过?我正在使用的测试链接是https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=MSFT&apikey=demo&datatype=csv。它在浏览器中打开它时将数据下载到一个csv文件中,但没有数据到达excel。将股票数据从www.alphavantage.co下载到excel

提前许多感谢, 扬

+1

即链路为我工作。 – jamheadart

+0

“到达Excel”是什么意思?他们似乎承诺可以在Excel中打开的CSV文件。所以,应该到达的是csv文件。 – Variatus

+0

我使用以下代码来下载: –

回答

2

在VBA中,选择工具>参考并选择以下引用:

  • Microsoft脚本控制1.0
  • Microsoft脚本运行时

以下函数将检索最近的数据(ope N,高,低,关闭,和体积)为特定符号:

Public Function GetLastCloseData(symbol As String) As Dictionary 
    Dim scriptControl As Object 
    Dim json As Object 
    Dim time_series As Object 
    Dim date_data As Object 
    Dim date_label As String 
    Dim date_offset As Integer 

    Set scriptControl = CreateObject("MSScriptControl.ScriptControl") 
    scriptControl.Language = "JScript" 

    'Retrieve historical price data in JSON format 
    With CreateObject("MSXML2.XMLHTTP") 
     .Open "GET", "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=" & symbol & "&apikey=" & API_KEY, False 
     .send 
     Set json = scriptControl.Eval("(" + .responseText + ")") 
     .abort 
    End With 

    'CallByName returns an error if it cannot find the requested selection. 
    'The code is written to skip over those errors. 
    On Error Resume Next 

    Set time_series = CallByName(json, "Time Series (Daily)", VbGet) 

    'If time series property was found... 
    If Not time_series Is Nothing Then 
     date_offset = 0 
     'Retrieve the most recent closing price by looking for todays date. If it's not found, 
     'iterate through the last week of dates, stopping whenever the most recent is found. 
     While date_data Is Nothing And date_offset < 8 
      date_label = Format(DateAdd("d", -date_offset, Date), "yyyy-mm-dd") 
      Set date_data = CallByName(time_series, date_label, VbGet) 
      date_offset = date_offset + 1 
     Wend 
    End If 

    If Not date_data Is Nothing Then 
     Set GetLastCloseData = New Dictionary 
     With GetLastCloseData 
      .Add "Open", CDbl(CallByName(date_data, "1. open", VbGet)) 
      .Add "High", CDbl(CallByName(date_data, "2. high", VbGet)) 
      .Add "Low", CDbl(CallByName(date_data, "3. low", VbGet)) 
      .Add "Close", CDbl(CallByName(date_data, "4. close", VbGet)) 
      .Add "Volume", CLng(CallByName(date_data, "5. volume", VbGet)) 
     End With 
    End If 

    'set error handling back to normal 
    On Error GoTo 0 

End Function 

下面的Sub演示了如何使用结果:

Public Sub GetStockData() 
    Dim daily_data As Dictionary 

    Set daily_data = GetLastCloseData("SPY") 
    Debug.Print daily_data("Open") 
    Debug.Print daily_data("High") 
    Debug.Print daily_data("Low") 
    Debug.Print daily_data("Close") 
    Debug.Print daily_data("Volume") 
End Sub 

输出:

260 
260.15 
259.57 
259.76 
45033392 
+0

注意:此功能要求您在模块的开头定义您的Alphavantage API密钥,如下所示: 'Private Const API_KEY =“api_key_here”' 或者,您可以手动将API Key添加到URL字符串。 – NYITGUY