Пример #1
0
        //public HistoricPrices(): base()
        //{
        //}

        public async Task <IEnumerable <CandleData> > GetPrices(string product, string startTime = "", string endTime = "", string granularity = "")
        {
            StringBuilder urlBuilder = new StringBuilder();

            urlBuilder.Append(string.Format(@"products/{0}/candles", product));
            string endpoint = string.Format(urlBuilder.ToString());


            if (!string.IsNullOrWhiteSpace(startTime))
            {
                urlBuilder.Append(string.Format(@"?start={0}&end={1}", startTime, endTime));
                endpoint = urlBuilder.ToString();
            }

            if (!string.IsNullOrWhiteSpace(granularity))
            {
                if (endpoint.Contains("?"))
                {
                    urlBuilder.Append(string.Format(@"&granularity={0}", granularity));
                }
                else
                {
                    urlBuilder.Append(string.Format(@"?granularity={0}", granularity));
                }
                endpoint = urlBuilder.ToString();
            }

            HistoricPriceRequest request = new HistoricPriceRequest(endpoint);


            ExchangeResponse genericResponse = null;

            try
            {
                genericResponse = await this.GetResponse(request);
            }
            catch (Exception ex)
            {
                Logger.WriteLog("Error getting historic data prices: " + ex.Message + " InnerEx msg:" + ex.InnerException.Message);
                throw new Exception("HistoricDataError");
            }



            IEnumerable <CandleData> result;

            if (genericResponse.IsSuccessStatusCode)
            {
                var json = genericResponse.ContentBody;

                //Logger.WriteLog(json);



                //JArray job = new JArray(str);
                //JArray jObject = new JArray();
                //JObject.Parse(json);
                //var prices = jObject.First.Select(x => (JArray)x).ToArray();

                //var a = await Task.Factory.StartNew(() => JsonConvert.DeserializeObject<CandleData>(json));

                result = await Task.Factory.StartNew(() => JsonConvert.DeserializeObject <List <JArray> >(json).Select((x) => new CandleData
                {
                    Time     = DateTimeUtilities.DateTimeFromUnixTimestampSeconds((x[0].Value <string>())).ToLocalTime(),
                    Low      = x[1].Value <string>(),
                    High     = x[2].Value <string>(),
                    Open     = x[3].Value <string>(),
                    Close    = x[4].Value <decimal>(),
                    Volume   = x[5].Value <string>(),
                    LocalAvg = ((x[1].Value <decimal>() + x[2].Value <decimal>()) / 2)
                }));
            }
            else
            {
                throw new Exception("ExchangeRequestError");
            }

            return(result);
        }