public async Task <DataTablesResponse> GetTradePairOrderBookDataTable(DataTablesModel model, int tradePairId, TradeType tradeType) { var cacheKey = tradeType == TradeType.Buy ? TradeCacheKeys.GetOpenBuyOrdersKey(tradePairId) : TradeCacheKeys.GetOpenSellOrdersKey(tradePairId); var cacheResult = await CacheService.GetOrSetASync(cacheKey, TimeSpan.FromMinutes(60), async() => { using (var context = DataContextFactory.CreateContext()) { var query = context.Trade .Where(x => x.TradePairId == tradePairId && x.TradeType == tradeType && (x.Status == TradeStatus.Partial || x.Status == TradeStatus.Pending)) .GroupBy(t => t.Rate) .Select(x => new TradeOrderBookModel { Rate = x.Key, Total = x.Sum(o => o.Remaining), //SumTotal = x.Sum(o => o.Remaining) * x.Key, //OrderCount = x.Count() }); query = tradeType == TradeType.Buy ? query.OrderByDescending(o => o.Rate) : query.OrderBy(x => x.Rate); return(await query.GetDataTableResultNoLockAsync(model)); } }); return(cacheResult); }
public async Task <ChartDataViewModel> GetTradePairChart(int tradePairId) { var cacheResult = await CacheService.GetOrSetASync(TradeCacheKeys.GetTradePairChartKey(tradePairId), TimeSpan.FromMinutes(1), async() => { using (var context = DataContextFactory.CreateContext()) { var tradePairData = await context.TradeHistory .Where(x => x.TradePairId == tradePairId) .Select(x => new { Amount = x.Amount, Rate = x.Rate, Timestamp = x.Timestamp }).ToListAsync(); if (tradePairData.IsNullOrEmpty()) { return(MapChartData(new[] { new ChartDataModel(DateTime.UtcNow.ToJavaTime(), 0, 0, 0, 0, 0) })); } var chartData = new List <ChartDataModel>(); var start = tradePairData.Min(x => x.Timestamp); var finish = DateTime.UtcNow; var totalhours = (finish - start).TotalHours * 4; var lastClose = 0m; var interval = TimeSpan.FromMinutes(15); var tickData = tradePairData.GroupBy(s => s.Timestamp.Ticks / interval.Ticks); for (int i = 0; i < totalhours; i++) { var date = start.AddMinutes(15 * i); var data = tickData.FirstOrDefault(x => x.Any(c => c.Timestamp > date && c.Timestamp < date.Add(interval))); if (data.IsNullOrEmpty()) { if (lastClose == 0) { var firstOrDefault = tradePairData.FirstOrDefault(); if (firstOrDefault != null) { lastClose = firstOrDefault.Rate; } } chartData.Add(new ChartDataModel(date.ToJavaTime(), lastClose, lastClose, lastClose, lastClose, 0)); continue; } var max = data.Max(x => x.Rate); lastClose = data.Last().Rate; chartData.Add(new ChartDataModel(date.ToJavaTime(), data.First().Rate, max, data.Min(x => x.Rate), lastClose, data.Sum(x => x.Amount *x.Rate))); } return(MapChartData(chartData)); } }); var result = await SetTickerData(cacheResult, tradePairId); return(result); }
public async Task <DataTablesResponse> GetTradePairTradeHistoryDataTable(DataTablesModel model, int tradePairId) { var cacheResult = await CacheService.GetOrSetASync(TradeCacheKeys.GetTradeHistoryKey(tradePairId), TimeSpan.FromMinutes(60), async() => { using (var context = DataContextFactory.CreateContext()) { var query = context.TradeHistory .Where(x => x.TradePairId == tradePairId && x.TradePair.Status != TradePairStatus.Closed) .Select(x => new TradeHistoryDataTableModel { Amount = x.Amount, Rate = x.Rate, Timestamp = x.Timestamp, TradeHistoryType = x.TradeHistoryType, }).OrderByDescending(x => x.Timestamp).Take(500); return(await query.GetDataTableResultNoLockAsync(model)); } }); return(cacheResult); }