Resolved Using Alpha Vantage API

Runescope

Well-known member
Joined
Jan 6, 2011
Messages
53
Programming Experience
Beginner
I was curious if anyone has ever used the Alpha Vantage free stock feed API in VB.Net.

I've looked at several pages, but my knowledge of installing third-party ... things into VB is dismal at best. Sorry I can't be more specific about what I want. I know so little, I don't know how to describe what I want.

I think I want to add a reference? But I can't find anything on their site that looks like a 'com' or 'exe' or 'dll' to add.

Any help would be greatly appreciated.
 
Okay, so I've been working on this for a bit and I've got something sort of working. It's a place to start if others are in the same boat.
There's nothing to add to .net, it's all web response.

Here's the code I'm using.

VB.NET:
Imports System.IO
Imports System.Net

Public Class frmMain

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        TextBox1.AppendText(StockPrices("AAPL"))

    End Sub

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Function StockPrices(ByVal Ticker As String) As String

        Dim URL As String, stkResponse As String, apiKey As String, xmlHTTP As Object
        Dim sPos As Integer
        Dim splitResponse() As String

        apiKey = "*Redacted*"

        URL = "https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=" & Ticker & "&datatype=csv&apikey=" & apiKey
        xmlHTTP = CreateObject("MSXML2.XMLHTTP")
        xmlHTTP.Open("GET", URL, False)
        xmlHTTP.Send()
        stkResponse = xmlHTTP.responseText
        sPos = InStr(stkResponse, "changePercent") + 14
        stkResponse = Mid$(stkResponse, sPos)

        xmlHTTP = Nothing

        splitResponse = Split(stkResponse, ",")

        StockPrices = "Symbol: " & splitResponse(0) & " • Price: " & splitResponse(4) & " • $ Change: " & splitResponse(8) & " • % Change: " & splitResponse(9)

    End Function

End Class
 
I would prefer any .Net way to download data instead of XmlHttp COM object, there are several including HttpWebRequest, WebClient and HttpClient which is the latest and greatest.
Here's a sample with that:
VB.NET:
Public Shared client As New Net.Http.HttpClient() '(intended to use as single instance for app lifetime)

Private Async Sub Sample()
    Dim url = "https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=MSFT&apikey=demo&datatype=csv"
    Dim csv = Await client.GetStringAsync(url)
    Dim lines = csv.Split(New String() {vbNewLine}, StringSplitOptions.None)
    Dim data = lines(1).Split(","c)
    Dim symbol = data(0) ' by index

End Sub
Building on that you can get fancy and create lookup by key:
VB.NET:
Dim keys = lines(0).Split(","c)
Dim values = data.Select(Function(value, ix) Tuple.Create(ix, value)).ToDictionary(Function(y) keys(y.Item1), Function(y) y.Item2)
'symbol,open,high,low,price,volume,latestDay,previousClose,change,changePercent
Dim symbol = values("symbol")
Dim price = CDbl(values("price"))
The API also provides Json data, I find that mostly easier to work with, but you need a library for it, this one is common:
Here's a sample using Json, the keys they return is weird though, including numbers:
VB.NET:
Private Async Sub Sample2()
    Dim url = "https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=MSFT&apikey=demo"
    Dim json = Await client.GetStringAsync(url)
    Dim j = JObject.Parse(json)("Global Quote")
    Dim symbol = j("01. symbol").ToString
    Dim price = j("05. price").Value(Of Double)

End Sub
 
Thanks for all of that! I was getting there myself after working with it for a while, but I like the way you do things!
 
Back
Top