/// <summary>
        /// Get price history for a symbol
        /// https://developer.tdameritrade.com/price-history/apis/get/marketdata/%7Bsymbol%7D/pricehistory
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task <string> GetPriceHistoryJson(TDPriceHistoryRequest model)
        {
            if (!HasConsumerKey)
            {
                throw (new Exception("ConsumerKey is null"));
            }

            var key = HttpUtility.UrlEncode(AuthResult.consumer_key);

            var builder = new UriBuilder($"https://api.tdameritrade.com/v1/marketdata/{model.symbol}/pricehistory");
            var query   = HttpUtility.ParseQueryString(builder.Query);

            if (!IsSignedIn)
            {
                query["apikey"] = key;
            }
            if (model.frequencyType.HasValue)
            {
                query["frequencyType"] = model.frequencyType.ToString();
                query["frequency"]     = model.frequency.ToString();
            }
            if (model.endDate.HasValue)
            {
                query["endDate"]   = model.endDate.Value.ToString();
                query["startDate"] = model.startDate.Value.ToString();
            }
            if (model.periodType.HasValue)
            {
                query["periodType"] = model.periodType.ToString();
                query["period"]     = model.period.ToString();
            }
            if (model.needExtendedHoursData.HasValue)
            {
                query["needExtendedHoursData"] = model.needExtendedHoursData.ToString();
            }
            builder.Query = query.ToString();
            string url = builder.ToString();

            using (var client = new HttpClient())
            {
                if (IsSignedIn)
                {
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AuthResult.access_token);
                }
                var res = await client.GetAsync(url);

                switch (res.StatusCode)
                {
                case HttpStatusCode.OK:
                    return(await res.Content.ReadAsStringAsync());

                default:
                    throw (new Exception($"{res.StatusCode} {res.ReasonPhrase}"));
                }
            }
        }
        /// <summary>
        /// Get price history for a symbol
        /// https://developer.tdameritrade.com/price-history/apis/get/marketdata/%7Bsymbol%7D/pricehistory
        /// https://developer.tdameritrade.com/content/price-history-samples
        /// </summary>
        public async Task <TDPriceCandle[]> GetPriceHistory(TDPriceHistoryRequest model)
        {
            var json = await GetPriceHistoryJson(model);

            if (!IsNullOrEmpty(json))
            {
                var doc   = JObject.Parse(json);
                var inner = doc["candles"].ToObject <TDPriceCandle[]>();
                return(inner);
            }
            return(null);
        }
示例#3
0
        /// <summary>
        /// Get price history for a symbol
        /// https://developer.tdameritrade.com/price-history/apis/get/marketdata/%7Bsymbol%7D/pricehistory
        /// </summary>
        public async Task <TDPriceCandle[]> GetPriceHistory(TDPriceHistoryRequest model)
        {
            var json = await GetPriceHistoryJson(model);

            if (!string.IsNullOrEmpty(json))
            {
                var doc   = JsonDocument.Parse(json);
                var inner = doc.RootElement.GetProperty("candles").GetRawText();
                return(JsonSerializer.Deserialize <TDPriceCandle[]>(inner));
            }
            return(null);
        }