2
votes

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" },

2
Check this answer, it might help you to manage with Yahoo Finance API. - omegastripes

2 Answers

2
votes

The "CSV" option from the website is a downloadable file, not a text document to parse like this. A simpler solution is to use the site's JSON option instead, which is easily loaded into a string.

Since you only need one value, it's easiest to just search for the string "Close", and return the value after it.

Here's a quick solution that you could adapt as needed:

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
-1
votes

It was a sad day when Yahoo ended their API service. Nevertheless, there are MANY alternatives. You can use Python to get time series data for stocks. You can use R as well. I'll leave it to you to figure out how to utilize these technologies, because you are an Excel-VBA user and I don't want to force those options on you if you don't wan them. As an alternative, consider one of these two options, listed below.

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

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

enter image description here

For the second URL, click the link titled 'Get Excel Spreadsheet to Download Bulk Historical Stock Data from Google Finance' and you can download a tool that should do everything you want.