Exemplo n.º 1
0
        public async Task <IEnumerable <StockAnnualizedReturn> > GetAnnualizedReturnForCurrentHoldings(ulong userId, uint?monthsSincePurchase)
        {
            var toDate          = monthsSincePurchase == null ? DateTime.MaxValue : DateTime.Today.AddMonths((int)-monthsSincePurchase);
            var currentHoldings = await _stockHoldingService.GetAllHoldingsForUser(userId);

            var purchaseIds  = ExtractPurchaseIds(currentHoldings);
            var purchaseTask = _stockPurchaseService.GetPurchasesByIdFilteredByDates(userId, purchaseIds, DateTime.MinValue, toDate);
            var purchases    = await purchaseTask;
            var stockIds     = FilterStockIds(purchases, currentHoldings);
            var stockTask    = _stockService.GetStocksById(userId, stockIds);
            var stocks       = await stockTask;

            var tickers         = stocks.Select(x => x.Ticker);
            var stockQuotesTask = _marketDataService.GetLastStockQuote(tickers);
            var stockQuotes     = await stockQuotesTask;


            var stockReturnModel = BuildStockReturnModel(stocks.ToDictionary(x => x.StockId),
                                                         purchases.ToDictionary(x => x.PurchaseId),
                                                         stockQuotes.ToDictionary(x => x.Symbol),
                                                         currentHoldings);

            ComputeAnnualizedReturn(stockReturnModel);

            stockReturnModel = stockReturnModel.OrderByDescending(x => x.AnnualizedReturn);

            return(stockReturnModel);
        }
        private Task <IEnumerable <Purchase> > GetPurchases(ulong userId,
                                                            IEnumerable <ulong> purchaseIds,
                                                            DateTime from,
                                                            DateTime to)
        {
            if (from != DateTime.MinValue && to != DateTime.MinValue && from <= to)
            {
                return(_stockPurchaseService.GetPurchasesByIdFilteredByDates(userId, purchaseIds, from, to));
            }

            else
            {
                return(_stockPurchaseService.GetPurchasesById(userId, purchaseIds));
            }
        }