Exemple #1
0
        /// <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
            {
                { "function", "TIME_SERIES_DAILY_ADJUSTED" },
                { "symbol", ticker },
                { "outputSize", _outputSize },
                { "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);
        }
        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();
                t.Ticker = symbol;
                // 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             = q["1. open"].Value <decimal>();
                    quote.High             = q["2. high"].Value <decimal>();
                    quote.Low              = q["3. low"].Value <decimal>();
                    quote.Close            = q["4. close"].Value <decimal>();
                    quote.AdjustedClose    = q["5. adjusted close"].Value <decimal>();
                    quote.Volume           = q["6. volume"].Value <int>();
                    quote.Dividend         = q["7. dividend amount"].Value <decimal>();
                    quote.SplitCoefficient = q["8. split coefficient"].Value <decimal>();

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

            return(t);
        }