Exemple #1
0
        public async Task <AnalyticsPrice> GetMonthlyAsync(string symbol, int year, int month)
        {
            // TODO: Add implementation for the monthly summary
            //Check if symbol exists first
            var symbolExists = await _shareRepository.ShareSymbolExists(symbol);

            if (!symbolExists)
            {
                throw new Exception("Share Symbol doesn't exist");
            }

            //Get all the days in the month
            var days        = DaysInMonth(year, month);
            var sharePrices = _shareRepository.MonthAllShares(symbol, days);

            if (sharePrices.Values.Count != 0)
            {
                AnalyticsPrice dayAnalyticsPrice = GetSharesDailyValues(sharePrices);

                return(dayAnalyticsPrice);
            }

            var defaultPrice = new AnalyticsPrice();

            return(defaultPrice);
        }
Exemple #2
0
        private static AnalyticsPrice GetSharesDailyValues(Dictionary <DateTime, List <HourlyShareRate> > weekValues)
        {
            List <decimal> valuesPrice = new List <decimal>();

            //Get week's share price
            foreach (var dateValue in weekValues.Values)
            {
                foreach (var hourlyShareRate in dateValue)
                {
                    valuesPrice.Add(hourlyShareRate.Rate);
                }
            }

            var analyticsPrice = new AnalyticsPrice();

            if (valuesPrice.Count != 0)
            {
                analyticsPrice.Open  = valuesPrice[0];
                analyticsPrice.High  = valuesPrice.Max();
                analyticsPrice.Low   = valuesPrice.Min();
                analyticsPrice.Close = valuesPrice[valuesPrice.Count - 1];
            }

            return(analyticsPrice);
        }
        public async Task <IActionResult> Daily([FromRoute] string symbol, [FromRoute] int year, [FromRoute] int month, [FromRoute] int day)
        {
            // TODO: Add implementation for the daily summary

            //validate route values
            if (symbol.Length > 3 || symbol.Length < 3)
            {
                return(BadRequest("Symbol characters exceeded or below"));
            }

            if (year == 0 || month == 0 || day == 0)
            {
                return(BadRequest("Incorrect Route Values for date"));
            }

            if (year < 1000)
            {
                return(BadRequest("The year value is less"));
            }

            if (!Enumerable.Range(1, 12).Contains(month))
            {
                return(BadRequest("The month value doesn't exist"));
            }

            if (!Enumerable.Range(1, 31).Contains(day))
            {
                return(BadRequest("The day value doesn't exist"));
            }


            //Parse from route values Datetime object
            var queryTime = new DateTime(year, month, day);

            AnalyticsPrice todaysAnalyticsPrice = new AnalyticsPrice();

            //Set validation for route values
            try
            {
                todaysAnalyticsPrice = await _analyticsService.GetDailyAsync(symbol, queryTime);
            }
            catch (Exception e)
            {
                return(NotFound(e.Message));
            }


            var result = new DailyModel()
            {
                Symbol = symbol,
                Day    = queryTime,
                Price  = Map(todaysAnalyticsPrice)
            };

            return(Ok(result));
        }
Exemple #4
0
 private PriceModel Map(AnalyticsPrice price)
 {
     return(new PriceModel()
     {
         Open = price.Open,
         Close = price.Close,
         High = price.High,
         Low = price.Low
     });
 }
        public async Task <AnalyticsPrice> GetWeeklyAsync(string symbol, int year, int week)
        {
            var orderedRatesForTheWeek = _shareRepository.Query()
                                         .Where(r => r.Symbol == symbol &&
                                                r.TimeStamp.Year == year &&
                                                GetWeekNumber(r.TimeStamp) == week)
                                         .OrderBy(r => r.TimeStamp);
            AnalyticsPrice analyticsPrice = CalculateAnalyticsPrice(orderedRatesForTheWeek);

            return(await Task.FromResult(analyticsPrice));
        }
        public async Task <AnalyticsPrice> GetMonthlyAsync(string symbol, int year, int month)
        {
            var orderedHourlyRatesForTheMonth = _shareRepository.Query()
                                                .Where(r => r.Symbol == symbol &&
                                                       r.TimeStamp.Year == year &&
                                                       r.TimeStamp.Month == month)
                                                .OrderBy(r => r.TimeStamp);

            AnalyticsPrice analyticsPrice = CalculateAnalyticsPrice(orderedHourlyRatesForTheMonth);

            return(await Task.FromResult(analyticsPrice));
        }
        private AnalyticsPrice SetAnalyticsPrice(HourlyShareRate[] hourlyShareRates)
        {
            var analyticsPrice = new AnalyticsPrice
            {
                Close = hourlyShareRates.LastOrDefault().Rate,
                Open  = hourlyShareRates.FirstOrDefault().Rate,
                High  = hourlyShareRates.Max(x => x.Rate),
                Low   = hourlyShareRates.Min(x => x.Rate)
            };

            return(analyticsPrice);
        }
        public async Task <AnalyticsPrice> GetDailyAsync(string symbol, DateTime day)
        {
            var orderedHourlyRatesForTheDay = _shareRepository.Query()
                                              .Where(r => r.Symbol == symbol &&
                                                     r.TimeStamp.Year == day.Year &&
                                                     r.TimeStamp.Month == day.Month &&
                                                     r.TimeStamp.Day == day.Day)
                                              .OrderBy(r => r.TimeStamp);

            AnalyticsPrice analyticsPrice = CalculateAnalyticsPrice(orderedHourlyRatesForTheDay);

            return(await Task.FromResult(analyticsPrice));
        }
Exemple #9
0
        public async Task <AnalyticsPrice> GetWeeklyAsync(string symbol, int year, int week)
        {
            // TODO: Add implementation for the weekly summary
            var symbolExists = await _shareRepository.ShareSymbolExists(symbol);

            if (!symbolExists)
            {
                throw new Exception("Share Symbol doesn't exist");
            }

            var dayValues  = WeekDays(year, week);
            var weekValues = _shareRepository.WeekAllShares(symbol, dayValues);

            AnalyticsPrice analyticsPrice = GetSharesDailyValues(weekValues);

            return(analyticsPrice);
        }
        public async Task <AnalyticsPrice> GetMonthlyAsync(string symbol, int year, int month)
        {
            // TODO: Add implementation for the monthly summary
            var listOfSharesByMonth = await EntityRepository
                                      .Query()
                                      .Where(x => x.Symbol.Equals(symbol) &&
                                             x.TimeStamp.Month == month &&
                                             x.TimeStamp.Year == year)

                                      .ToListAsync();

            var result = new AnalyticsPrice
            {
                Open  = listOfSharesByMonth.OrderBy(p => p.TimeStamp).FirstOrDefault().Rate,
                High  = listOfSharesByMonth.Max(p => p.Rate),
                Low   = listOfSharesByMonth.Min(p => p.Rate),
                Close = listOfSharesByMonth.OrderByDescending(p => p.TimeStamp).FirstOrDefault().Rate,
            };

            return(result);
        }
        public async Task <IActionResult> Weekly([FromRoute] string symbol, [FromRoute] int year, [FromRoute] int week)
        {
            // TODO: Add implementation for the weekly summary

            //validate route values
            if (symbol.Length > 3 || symbol.Length < 3)
            {
                return(BadRequest("Symbol characters exceeded or below"));
            }

            if (year < 1000)
            {
                return(BadRequest("The year value is less"));
            }

            if (!Enumerable.Range(1, 52).Contains(week))
            {
                return(BadRequest("Incorrect Week Value"));
            }

            AnalyticsPrice weekAnalyticPrice = new AnalyticsPrice();

            try
            {
                weekAnalyticPrice = await _analyticsService.GetWeeklyAsync(symbol, year, week);
            }
            catch (Exception e)
            {
                return(NotFound(e.Message));
            }
            var result = new WeeklyModel()
            {
                Symbol = symbol,
                Year   = year,
                Week   = week,
                Price  = Map(weekAnalyticPrice)
            };

            return(Ok(result));
        }
Exemple #12
0
        public async Task <AnalyticsPrice> GetDailyAsync(string symbol, DateTime day)
        {
            // TODO: Add implementation for the daily summary
            //Check if symbol exists first
            var symbolExists = await _shareRepository.ShareSymbolExists(symbol);

            if (!symbolExists)
            {
                throw new Exception("Share Symbol doesn't exist");
            }

            //Load all prices for the share at that time
            var allShares = await _shareRepository.TodayAllShares(symbol, day);

            var analyticsPrice = new AnalyticsPrice();

            //First and Last index contains Open and Closing price
            if (allShares.Count != 0)
            {
                analyticsPrice.Open  = allShares[0].Rate;
                analyticsPrice.Close = allShares[allShares.Count - 1].Rate;

                //Calculate High and Low price
                List <decimal> valuesPrice = new List <decimal>();

                foreach (var rate in allShares)
                {
                    valuesPrice.Add(rate.Rate);
                }

                analyticsPrice.High = valuesPrice.Max();
                analyticsPrice.Low  = valuesPrice.Min();
            }



            return(analyticsPrice);
        }