-
-
Save JonCanning/293bb53773ee9f30de7cf09473e1b3eb to your computer and use it in GitHub Desktop.
Technical test to get the aggregated stock results for a given symbol, originally done in C# but I tried it out in F#
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:Newtonsoft.Json" | |
open System.Collections.Generic | |
open System | |
open System.Net.Http | |
open Newtonsoft.Json | |
let renderStockUrl = | |
sprintf | |
"https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=%s&outputsize=full&apikey=A3U8E3F7N3A85K86" | |
type TimeSeries = | |
{ ``2. high``: decimal | |
``3. low``: decimal } | |
type Response = | |
{ ``Time Series (Daily)``: Dictionary<DateTime, TimeSeries> } | |
let queryApi = | |
let httpClient = new HttpClient() | |
fun (url: string) -> httpClient.GetStringAsync(url).Result | |
let aggregateStockTimeSeries (response: Response) = | |
response.``Time Series (Daily)`` | |
|> Seq.map (|KeyValue|) | |
|> Seq.groupBy (fun (date, _) -> date.Year, date.Month) | |
|> Seq.map (fun ((year, month), timeSeries) -> | |
let minLow = | |
timeSeries | |
|> Seq.map snd | |
|> Seq.map (fun timeSeries -> timeSeries.``3. low``) | |
|> Seq.min | |
let maxHigh = | |
timeSeries | |
|> Seq.map snd | |
|> Seq.map (fun timeSeries -> timeSeries.``2. high``) | |
|> Seq.max | |
{| Year = year | |
Month = month | |
MinLow = minLow | |
MaxHigh = maxHigh |}) | |
let getResultsFor symbol = | |
renderStockUrl symbol | |
|> queryApi | |
|> JsonConvert.DeserializeObject<Response> | |
|> aggregateStockTimeSeries | |
for result in getResultsFor "MSFT" do | |
printfn "%i/%i %M %M" result.Month result.Year result.MinLow result.MaxHigh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment