Example #1
0
        async internal static Task GetHistoricValues(int granularity, string currency_pair)
        {
            String URL = "https://api.gdax.com/products/" + currency_pair + "/candles";

            if (granularity != 0)
            {
                URL += "?granularity=" + granularity; //https://api.gdax.com/products/ETH-EUR/candles?granularity=60
            }
            HttpClient httpClient = new HttpClient();

            //Add a user-agent header to the GET request.
            var headers = httpClient.DefaultRequestHeaders;

            //The safe way to add a header value is to use the TryParseAdd method and verify the return value is true,
            //especially if the header value is coming from user input.
            string header = "ie";

            if (!headers.UserAgent.TryParseAdd(header))
            {
                throw new Exception("Invalid header value: " + header);
            }

            header = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)";
            if (!headers.UserAgent.TryParseAdd(header))
            {
                throw new Exception("Invalid header value: " + header);
            }

            Uri requestUri = new Uri(URL);

            HttpResponseMessage httpResponse = new HttpResponseMessage();
            string response = "";

            try {
                //Send the GET request
                httpResponse = await httpClient.GetAsync(requestUri);

                httpResponse.EnsureSuccessStatusCode();

                response = await httpResponse.Content.ReadAsStringAsync();

                var data = JRaw.Parse(response);

                int count = ((JContainer)data).Count;

                pp.Clear();
                //List<PricePoint> p = new List<PricePoint>();
                for (int i = 0; i < count; i++)
                {
                    pp.Add(PricePoint.GetPricePoint(data[i], currency_pair));
                }
            }
            catch (Exception ex) {
                response = "Error: " + ex.HResult.ToString("X") + " Message: " + ex.Message;
            }
        }