public List <ExchangeHistory> GetHistory(string currency, int days) { int addDays = 0; var tm = AmountHelper.DateTimeToUnixTimestamp(DateTime.UtcNow); DateTimeOffset tmCheck = new DateTimeOffset(DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day, 3, 0, 0, new TimeSpan(0, 0, 0)); long tmChekDate = ExchangeHelper.DateTimeToUnixTimestamp(tmCheck.UtcDateTime); if (tmChekDate + 60 * 60 < tmChekDate) { addDays = -1; } var exchange_stats = _dtoMain.mainDb.GetCollection <ExchangeTicker>("aaexchanges_real_each"); DateTimeOffset dateTimeYesterday = new DateTimeOffset(DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day, 0, 0, 0, new TimeSpan(0, 0, 0)).AddDays(-days + 1 + addDays); long unixTimeHistory = AmountHelper.DateTimeToUnixTimestamp(dateTimeYesterday.UtcDateTime); var exchangeStats = exchange_stats.Find(m => m.InCurrency == currency && m.OutCurrency == "BTC" && m.Time >= unixTimeHistory).ToList(); var history = new List <ExchangeHistory>(); var tmp = new Dictionary <long, List <Decimal> >(); var tmpVolume = new Dictionary <long, Decimal>(); foreach (var ex in exchangeStats) { if (!tmpVolume.ContainsKey(ex.Time)) { tmpVolume.Add(ex.Time, 0.0m); } tmpVolume[ex.Time] += ex.Volume; } foreach (var ex in exchangeStats) { if (tmpVolume.ContainsKey(ex.Time) && tmpVolume[ex.Time] > 0 && ex.Volume * 100 / tmpVolume[ex.Time] < 3) { continue; } if (!tmp.ContainsKey(ex.Time)) { tmp.Add(ex.Time, new List <decimal>()); } tmp[ex.Time].Add(ex.Last); } int i = 1; foreach (var ex in tmp) { if (i > days) { break; } var date = UnixTimeStampToDateTime(ex.Key); var dateStr = date.Day + "-" + date.Month + "-" + date.Year; history.Add(new ExchangeHistory() { Date = dateStr, Price = ex.Value.Average() }); i++; } return(history); }