public async Task <TotalHoldingsValue> GetTotalValueForUserHolding(ulong userId)
        {
            var holdings = await _stockHoldingService.GetAllHoldingsForUserWithStockDetails(userId);

            /*
             * var tickers = GetStockTickersFromHoldings(holdings);
             * var stockPricesTask = _marketDataService.GetLastStockQuote(tickers);
             * var lastStockQuote = await stockPricesTask;
             * var tickerStockQuoteMap = MapTickerToLastStockQuote(lastStockQuote);
             * var individualStockValues = new List<IndividualStockValue>();
             *
             * foreach(var holding in holdings)
             * {
             *  var ticker = holding.Stock.Ticker;
             *  var holdingDetails = holding.HoldingDetails;
             *  var individualStockValue = new IndividualStockValue();
             *  individualStockValue.Ticker = ticker;
             *
             *  foreach (var holdingDetail in holdingDetails)
             *  {
             *      individualStockValue.TotalStockCurrentPrice += tickerStockQuoteMap[ticker] * holdingDetail.Quantity;
             *      individualStockValue.TotalStockPurchasePrice += holdingDetail.Price * holdingDetail.Quantity;
             *  }
             *
             *  individualStockValue.TotalStockGainOrLoss = individualStockValue.TotalStockCurrentPrice - individualStockValue.TotalStockPurchasePrice;
             *  individualStockValue.TotalStockGainOrLossPercentage = (individualStockValue.TotalStockGainOrLoss / individualStockValue.TotalStockPurchasePrice) * 100;
             *  individualStockValues.Add(individualStockValue);
             * }
             *
             * return ComputeTotalHoldingsValue(individualStockValues);*/
            return(new TotalHoldingsValue());
        }
        public async Task <PortfolioComposition> GetPortfolioComposition(ulong userId)
        {
            var portfolioComposition = new PortfolioComposition();
            var individualWeightages = new List <IndividualStockWeightage>();

            var holdings = await _stockHoldingService.GetAllHoldingsForUserWithStockDetails(userId);

            var tickers            = holdings.Select(x => x.Stock.Ticker);
            var lastQuoteForStocks = await _marketDataService.GetLastStockQuote(tickers);

            var tickerLastQuoteMap = lastQuoteForStocks.ToDictionary(x => x.Symbol);

            foreach (var holding in holdings)
            {
                if (tickerLastQuoteMap.ContainsKey(holding.Stock.Ticker))
                {
                    var individualWeightage = new IndividualStockWeightage();
                    individualWeightage.Ticker   = holding.Stock.Ticker;
                    individualWeightage.Country  = holding.Stock.Country;
                    individualWeightage.Sector   = holding.Stock.Sector;
                    individualWeightage.Industry = holding.Stock.Industry;

                    foreach (var holdingDetail in holding.HoldingDetails)
                    {
                        individualWeightage.TotalCurrentValue  += tickerLastQuoteMap[individualWeightage.Ticker].Price * holdingDetail.Quantity;
                        individualWeightage.TotalPurchasePrice += holdingDetail.Price * holdingDetail.Quantity;
                    }

                    FindAmountsByCountry(individualWeightage, portfolioComposition);
                    FindAmountsByIndustry(individualWeightage, portfolioComposition);
                    FindAmountsBySector(individualWeightage, portfolioComposition);

                    portfolioComposition.TotalCost            += individualWeightage.TotalPurchasePrice;
                    portfolioComposition.TotalInvestmentValue += individualWeightage.TotalCurrentValue;

                    individualWeightages.Add(individualWeightage);
                }
            }

            foreach (var individualWeightage in individualWeightages)
            {
                individualWeightage.PurchasePriceRatio = Math.Round(((individualWeightage.TotalPurchasePrice / portfolioComposition.TotalCost) * 100), 2);
                individualWeightage.CurrentValueRatio  = Math.Round((individualWeightage.TotalCurrentValue / portfolioComposition.TotalInvestmentValue) * 100, 2);
            }

            Task.WaitAll(Task.Run(() => { FindRatiosByCountry(portfolioComposition); }),
                         Task.Run(() => { FindRatiosByIndustry(portfolioComposition); }),
                         Task.Run(() => { FindRatiosBySector(portfolioComposition); }));

            portfolioComposition.IndividualStockWeightages = individualWeightages;

            return(portfolioComposition);
        }
        public async Task PerformStockSplit(ulong userId, StockSplit splitDetails)
        {
            var purchaseTask       = _stockPurchaseService.GetAllPurchasesForTicker(userId, splitDetails.Ticker);
            var holdingTask        = _holdingService.GetAllHoldingsForUserWithStockDetails(userId);
            var purchases          = await purchaseTask;
            var holdings           = await holdingTask;
            var requiredHolding    = holdings.Where(x => x.Stock.Ticker == splitDetails.Ticker).First();
            var purchaseIdsUpdated = UpdatePurchases(purchases, splitDetails);

            UpdateUserHoldings(requiredHolding, splitDetails, purchaseIdsUpdated);
            await _stockPurchaseService.UpdatePurchasePriceAndQuantityByPurchaseId(userId, purchases);

            await _holdingService.UpdateHoldingDetails(userId, requiredHolding);
        }
Esempio n. 4
0
        public async Task <CompositionAndRecommendation> GetCompositionAndRecommendation(ulong userId)
        {
            var compositionAndRecommendation = new CompositionAndRecommendation();
            var holdings = await _holdingsService.GetAllHoldingsForUserWithStockDetails(userId);

            var tickers            = holdings.Select(x => x.Stock.Ticker).Distinct();
            var recommendationTask = _stockRecommendationService.GetAnalysisAndRecommendation(tickers);
            var portfolioTask      = _portfolioCompositionService.GetPortfolioComposition(userId);
            var portfolio          = await portfolioTask;
            var recommendation     = await recommendationTask;

            MapCompositionAndRecommendation(compositionAndRecommendation, portfolio, recommendation);

            return(compositionAndRecommendation);
        }