Last active
July 19, 2020 22:26
-
-
Save onionhammer/24501f681f9fe10926d2e4e685ca81df to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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