Skip to content

Instantly share code, notes, and snippets.

@onionhammer
Last active July 19, 2020 22:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onionhammer/24501f681f9fe10926d2e4e685ca81df to your computer and use it in GitHub Desktop.
Save onionhammer/24501f681f9fe10926d2e4e685ca81df to your computer and use it in GitHub Desktop.
#r "nuget: CsvHelper, 12.1.2"
using System;
using System.Linq;
using System.Net.Http;
using System.Web;
using CsvHelper;
using static System.Web.HttpUtility;
const string apikey = "YOUR KEY";
var alphaHttp = new HttpClient { BaseAddress = new Uri("https://www.alphavantage.co/query") };
string QueryString<T>(T type, string prefix = "?") =>
prefix + string.Join("&",
from prop in typeof(T).GetProperties()
let value = UrlEncode(prop.GetValue(type)?.ToString())
select $"{prop.Name}={value}"
);
async Task<IReadOnlyList<T>> QueryFunctionCsvAsync<T>(T header, string query)
{
using (var response = await alphaHttp.GetStreamAsync(query))
using (var sr = new StreamReader(response))
using (var csv = new CsvReader(sr))
return csv.GetRecords<T>().ToList();
}
var timeSeriesIntraDay =
await QueryFunctionCsvAsync(
// CSV header format:
header: new
{
timestamp = default(DateTime), open = 0.0m, high = 0.0m, low = 0.0m, close = 0.0m, volume = 0L
},
// Function query
query: QueryString(new
{
function = "TIME_SERIES_INTRADAY",
datatype = "csv",
symbol = "AAPL",
interval = "5min",
apikey,
})
);
foreach (var row in timeSeriesIntraDay)
Console.WriteLine(row);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment