Skip to content

Instantly share code, notes, and snippets.

@RamonWill
Created July 26, 2020 11:53
Show Gist options
  • Save RamonWill/ab005f79bc8273951d94f60d7b419806 to your computer and use it in GitHub Desktop.
Save RamonWill/ab005f79bc8273951d94f60d7b419806 to your computer and use it in GitHub Desktop.
Code from my video on how to get stock prices from AlphaVantage and store those prices into a CSV and Microsoft.Data.Analysis DataFrame
using System;
using System.IO;
using System.Net;
using Microsoft.Data.Analysis;
namespace StockProject
{
// https://www.youtube.com/watch?v=ULGMStkOf7Y
class Program
{
static void Main(string[] args)
{
AVConnection conn = new AVConnection("demo");
conn.SaveCSVFromURL("IBM");
DataFrame df = DataFrame.LoadCsv("stockdata.csv");
Console.WriteLine(df);
}
}
public class AVConnection
{
private readonly string _apiKey;
public AVConnection(string apiKey)
{
this._apiKey = apiKey;
}
public void SaveCSVFromURL(string symbol)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://" + $@"www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol={symbol}&apikey={this._apiKey}&datatype=csv");
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(resp.GetResponseStream());
string results = sr.ReadToEnd();
sr.Close();
File.WriteAllText("stockdata.csv", results);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment