예제 #1
0
        public static async Task <List <HistoricPrice> > GetHistoric_(this ICryptoCompare service,
                                                                      string crypto, string time, int limit, int aggregate = 1)
        {
            var currency = Ioc.Default.GetService <LocalSettings>().Get <string>(UserSettings.Currency);


            object resp;

            try {
                if (limit == 0)
                {
                    resp = await service.GetHistoricAll(time, crypto, currency);
                }
                else
                {
                    resp = await service.GetHistoric(time, crypto, currency, limit, aggregate);
                }

                var response = JsonSerializer.Deserialize <object>(resp.ToString());

                var okey = ((JsonElement)response).GetProperty("Response").ToString();
                var data = ((JsonElement)response).GetProperty("Data");

                var timeTo   = data.GetProperty("TimeTo").ToString();
                var timeFrom = data.GetProperty("TimeFrom").ToString();
                if (!okey.Equals("Success", StringComparison.InvariantCultureIgnoreCase) || timeTo == timeFrom)
                {
                    throw new Exception();
                }

                var historic = JsonSerializer.Deserialize <List <HistoricPrice> >(data.GetProperty("Data").ToString());

                // Add calculation of dates and average values
                DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
                foreach (var h in historic)
                {
                    h.Average = (h.high + h.low) / 2;
                    DateTime d = dtDateTime.AddSeconds(h.time).ToLocalTime();
                    h.DateTime = d;
                    h.Date     = d.ToString();
                }

                // if getting all history, remove null prices
                if (limit == 0)
                {
                    int i = historic.FindIndex(x => x.Average != 0);
                    if (i != 0)
                    {
                        historic.RemoveRange(0, i - 1);
                    }
                }

                return(historic);
            }
            catch (Exception ex) {
                var z = ex.Message;
                return(new List <HistoricPrice>());
            }
        }
예제 #2
0
        public static async Task <double> GetPrice_Extension(this ICryptoCompare service, string crypto, string currency)
        {
            double price = 0;

            try {
                var response = await service.GetPrice(crypto, currency);

                var data = response.ToString();
                double.TryParse(data.Split(":")[1].Replace("}", ""), out price);
                return(price);
            }
            catch (Exception ex) {
                return(price);
            }
        }
예제 #3
0
        public static async Task <List <CoinCryptoCompare> > GetAllCoins_(this ICryptoCompare service)
        {
            try {
                var resp = await service.GetAllCoins();

                var response = JsonSerializer.Deserialize <object>(resp.ToString());
                var okey     = ((JsonElement)response).GetProperty("Response").ToString();
                if (okey != "Success")
                {
                    throw new Exception();
                }

                var data = ((JsonElement)response).GetProperty("Data");
                var d    = JsonSerializer.Deserialize <Dictionary <string, CoinCryptoCompare> >(data.ToString());

                return(d.Values.ToList());
            }
            catch (Exception ex) {
                return(new List <CoinCryptoCompare>());
            }
        }