Exemple #1
0
        public Quotation GetDayQuotation(DateTime dateTime, int daysAgo)
        {
            var previousDay = dateTime.DayOfWeek == DayOfWeek.Sunday ? dateTime.AddDays(-1 - daysAgo).Date
                                                         : (dateTime.DayOfWeek == DayOfWeek.Monday ? dateTime.AddDays(-2 - daysAgo)
                                                                                                   : dateTime.AddDays(0 - daysAgo));

            if (!Interval.Equals(Inteval.D1))
            {
                var quotationByDay = Quotations.Where(x => x.Time.Date == previousDay.Date);
                if (!quotationByDay.Any())
                {
                    throw new DomainException("There is no previous quotations");
                }
                var open   = quotationByDay.OrderBy(x => x.Time).FirstOrDefault().Open;
                var high   = quotationByDay.Max(x => x.High);
                var low    = quotationByDay.Min(x => x.Low);
                var close  = quotationByDay.OrderByDescending(x => x.Time).FirstOrDefault().Close;
                var volume = quotationByDay.Sum(x => x.Volume);
                return(new Quotation(previousDay.Date, open, low, high, close, volume));
            }
            return(Quotations.FirstOrDefault(x => x.Time.Date == previousDay.Date) ?? throw new DomainException("There is no previous day quotation."));
        }