/// <summary>
        /// Fetches daily time series from the Alpha Vantage API
        /// </summary>
        /// <param name="ticker">Stock symbol to fetch price data for</param>
        /// <returns>json string containing pricing data</returns>
        public async Task <TimeSeriesDailyAdjustedResponse> GetTimeSeriesDailyAdjusted(string ticker)
        {
            // build the URL to fetch data
            NameValueCollection param = new NameValueCollection();

            param.Add("function", "TIME_SERIES_DAILY_ADJUSTED");
            param.Add("symbol", ticker);
            param.Add("outputSize", _outputSize);
            param.Add("apikey", _apiKey);

            string[] array = (from key in param.AllKeys
                              from value in param.GetValues(key)
                              select $"{key}={HttpUtility.UrlEncode(value)}").ToArray();

            string queryString = string.Join("&", array);
            string uri         = string.Concat(_baseUrl, queryString);

            // call the api
            HttpResponseMessage response = await _client.GetAsync(uri);

            // parse the response
            string statusCode = response.StatusCode.ToString();
            string json       = await response.Content.ReadAsStringAsync();

            TimeSeriesDailyAdjustedResponse r = AlphaVantageReader.ReadTimeSeriesDailyAdjusted(statusCode, json);

            return(r);
        }
예제 #2
0
        public static TimeSeriesDailyAdjustedResponse ReadTimeSeriesDailyAdjusted(string statusCode, string json)
        {
            TimeSeriesDailyAdjustedResponse t = new TimeSeriesDailyAdjustedResponse();
            JObject response = JObject.Parse(json);

            // fill out the time series object
            t.ResponseCode = statusCode;

            // set the error message if needed
            if (!statusCode.Equals("OK"))
            {
                t.ErrorMessage = response["Information"].ToString();
            }
            else
            {
                // parse the metadata
                t.Metadata = response["Meta Data"].ToString();
                string symbol = response["Meta Data"]["2. Symbol"].ToString();

                // parse the quotes
                foreach (var q in response["Time Series (Daily)"].Values())
                {
                    Quote quote = new Quote();
                    quote.QuoteDate        = Convert.ToDateTime(((JProperty)q.Parent).Name);
                    quote.Symbol           = symbol;
                    quote.Open             = (decimal)q["1. open"];
                    quote.High             = (decimal)q["2. high"];
                    quote.Low              = (decimal)q["3. low"];
                    quote.Close            = (decimal)q["4. close"];
                    quote.AdjustedClose    = (decimal)q["5. adjusted close"];
                    quote.Volume           = (int)q["6. volume"];
                    quote.Dividend         = (decimal)q["7. dividend amount"];
                    quote.SplitCoefficient = (decimal)q["8. split coefficient"];

                    // add quote to the list of quotes
                    t.Quotes.Add(quote);
                }
            }

            return(t);
        }