2

I'm a Yahoo Finance API refugee (they discontinued their API service) trying to switch to Alpha Vantage. I've modified the below code which I previously used for Yahoo Finance, but I'm getting a #VALUE error in Excel.

The URL below works by itself (it opens a CSV if you type it into your web browser), so I guess my real problem lies in extracting the correct data from the CSV into my Excel spreadsheet. Would anyone be able to help with this?

I'm trying to extract from the CSV the data in row 2, column 5 (the last closing price). Many thanks in advance!

Function StockClose(Ticker As String)

Dim URL As String, CSV As String, apikey As String, SCRows() As String, SCColumns() As String, pxClose As Double

apikey = "*censored*"

URL = "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=" & Ticker & "&outputsize=full&" & apikey & "&datatype=csv"

Set xmlhttp = CreateObject("MSXML2.XMLHTTP")
    xmlhttp.Open "GET", URL, False
    xmlhttp.Send
    CSV = xmlhttp.responseText

    'split the CSV into rows
    SCRows() = Split(CSV, Chr(10))
    'split the relevant row into columns. 0 means 1st row, starting at index 0
    SCColumns() = Split(SCRows(1), ",")
    '6 means: 5th column; starting at index 0 - price close is in the 5th column
    pxClose = SCColumns(6)

    StockClose = pxClose

Set http = Nothing

End Function

Sample of the data returned if I extract json instead of csv:

{ "Meta Data": { "1. Information": "Daily Prices (open, high, low, close) and Volumes", "2. Symbol": "SGD=X", "3. Last Refreshed": "2017-11-10", "4. Output Size": "Full size", "5. Time Zone": "US/Eastern" }, "Time Series (Daily)": { "2017-11-13": { "1. open": "1.3588", "2. high": "1.3612", "3. low": "1.3581", "4. close": "1.3587", "5. volume": "0" }, "2017-11-10": { "1. open": "1.3588", "2. high": "1.3612", "3. low": "1.3581", "4. close": "1.3587", "5. volume": "0" },

4

2 回答 2

2

网站上的“CSV”选项是一个可下载的文件,而不是像这样解析的文本文档。一个更简单的解决方案是使用站点的 JSON 选项,它很容易加载到字符串中。

由于您只需要一个值,因此最简单的方法是搜索字符串“Close”,然后返回它后面的值。

这是一个快速解决方案,您可以根据需要进行调整:

Option Explicit

Function StockClose(Ticker As String) As Double

    Dim URL As String, json As String, apiKey As String, xmlHTTP As Object
    Dim posStart As Integer, posEnd As Integer, strClose As String

    apiKey = "demo"
    URL = "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=" & Ticker & "&outputsize=full&apikey=" & apikey 

    Set xmlHTTP = CreateObject("MSXML2.XMLHTTP")
    xmlHTTP.Open "GET", URL, False
    xmlHTTP.Send
    json = xmlHTTP.responseText
    Set xmlHTTP = Nothing

    posStart = InStr(json, "4. close") + 12
    posEnd = InStr(posStart, json, """") - 1
    strClose = Mid(json, posStart, posEnd - posStart)

    StockClose = Val(strClose)

End Function
于 2017-11-11T16:44:36.887 回答
-1

雅虎结束他们的 API 服务是一个悲伤的日子。然而,有很多选择。您可以使用 Python 获取股票的时间序列数据。您也可以使用 R。我将留给您弄清楚如何利用这些技术,因为您是 Excel-VBA 用户,如果您不想要这些选项,我不想将这些选项强加给您。作为替代方案,请考虑下面列出的这两个选项之一。

http://finance.jasonstrimpel.com/bulk-stock-download/

http://investexcel.net/multiple-stock-quote-downloader-for-excel/

在此处输入图像描述

对于第二个 URL,单击标题为链接的链接'Get Excel Spreadsheet to Download Bulk Historical Stock Data from Google Finance',您可以下载一个工具,该工具应该可以满足您的所有需求。

于 2017-11-22T03:02:29.460 回答