private async Task <List <TradeSummaryData> > GetOrCacheBaseSummary(string baseCurrency) { var cacheResult = await CacheService.GetOrSetHybridAsync(CacheKey.ExchangeSummary(baseCurrency), TimeSpan.FromMinutes(5), async() => { var tradeHistory = await GetOrCacheTradeHistory().ConfigureAwait(false); var tradePairs = await TradePairReader.GetTradePairs().ConfigureAwait(false); var basepairs = tradePairs.Where(x => x.BaseSymbol == baseCurrency).ToList(); var ids = basepairs.Select(x => x.TradePairId); var tradeHistoryData = tradeHistory.Where(tp => ids.Contains(tp.TradePairId)).ToList(); var results = new List <TradeSummaryData>(); foreach (var x in basepairs) { var data = tradeHistoryData.Where(t => t.TradePairId == x.TradePairId); results.Add(new TradeSummaryData { CurrencyId = x.CurrencyId, TradePairId = x.TradePairId, Name = x.Name, Symbol = x.Symbol, Change = GetChangePercent(data.FirstOrDefault()?.Rate ?? x.LastTrade, x.LastTrade).ToString("F2"), BaseVolume = data.Sum(t => t.Amount *t.Rate).ToString("F8"), Volume = data.Sum(t => t.Amount).ToString("F8"), Low = (data.OrderBy(t => t.Rate).FirstOrDefault()?.Rate ?? x.LastTrade).ToString("F8"), High = (data.OrderByDescending(t => t.Rate).FirstOrDefault()?.Rate ?? x.LastTrade).ToString("F8"), Last = x.LastTrade.ToString("F8"), BaseSymbol = x.BaseSymbol }); } return(results.ToList()); }).ConfigureAwait(false); return(cacheResult); }
public async Task <ExchangeSummary> GetExchangeSummary() { var cacheResult = await CacheService.GetOrSetHybridAsync(CacheKey.ExchangeSummary(), TimeSpan.FromSeconds(20), async() => { var tradeHistoryData = await GetOrCacheTradeHistory().ConfigureAwait(false); var tradePairs = await TradePairReader.GetTradePairs().ConfigureAwait(false); var currencies = await CurrencyReader.GetCurrencies().ConfigureAwait(false); var baseCurrencies = await CurrencyReader.GetBaseCurrencies().ConfigureAwait(false); var summaryData = new List <ExchangeSummaryModel>(); foreach (var x in tradePairs) { var data = tradeHistoryData.Where(t => t.TradePairId == x.TradePairId); summaryData.Add(new ExchangeSummaryModel { TradePairId = x.TradePairId, Symbol = x.Symbol, BaseSymbol = x.BaseSymbol, TotalTrades = data.Count(), Low = (data.OrderBy(t => t.Rate).FirstOrDefault()?.Rate ?? x.LastTrade), High = (data.OrderByDescending(t => t.Rate).FirstOrDefault()?.Rate ?? x.LastTrade), Volume = data.Sum(t => t.Amount), TotalBase = data.Sum(t => t.Amount *t.Rate), Change = GetChangePercent(data.FirstOrDefault()?.Rate ?? x.LastTrade, x.LastTrade) }); } var now = DateTime.UtcNow; var baseData = summaryData.GroupBy(x => x.BaseSymbol); var totalVolume = baseData.Select(x => new { Symbol = x.Key, Total = x.Sum(u => u.TotalBase) }).ToList(); var topMarket = baseData.Select(x => x.MaxBy(u => u.TotalBase)).OrderBy(x => x.BaseSymbol).ToList(); var featured = currencies.Any(x => x.FeaturedExpires > now) ? currencies.Where(x => x.FeaturedExpires > now).ToList() : currencies.Where(x => x.CurrencyId == Constant.DOTCOIN_ID).ToList(); var baseCurrencyData = new List <ExchangeSummaryBaseCurrency>(); foreach (var currency in baseCurrencies) { var totalVol = totalVolume.FirstOrDefault(x => x.Symbol == currency.Symbol); baseCurrencyData.Add(new ExchangeSummaryBaseCurrency { CurrencyId = currency.CurrencyId, Name = currency.Name, Symbol = currency.Symbol, TotalVolume = totalVol != null ? totalVol.Total : 0, Rank = currency.Rank }); } return(new ExchangeSummary { TopMarkets = topMarket, BaseCurrencies = baseCurrencyData, TotalMarkets = tradePairs.Count, TotalTrades = summaryData.Sum(x => x.TotalTrades), Featured = featured.Select(x => new FeaturedCurrency { Name = x.Name, Symbol = x.Symbol, Website = x.Website, Summary = x.Summary }).ToList() }); }).ConfigureAwait(false); return(cacheResult); }