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);
        }
        private void FindAmountsByIndustry(IndividualStockWeightage individualWeightage, PortfolioComposition portfolioComposition)
        {
            if (!string.IsNullOrEmpty(individualWeightage.Industry))
            {
                if (!portfolioComposition.InvestmentAmountByIndustry.ContainsKey(individualWeightage.Industry))
                {
                    portfolioComposition.InvestmentAmountByIndustry[individualWeightage.Industry] = individualWeightage.TotalPurchasePrice;
                }
                else
                {
                    portfolioComposition.InvestmentAmountByIndustry[individualWeightage.Industry] += individualWeightage.TotalPurchasePrice;
                }

                if (!portfolioComposition.CurrentValueAmountByIndustry.ContainsKey(individualWeightage.Industry))
                {
                    portfolioComposition.CurrentValueAmountByIndustry[individualWeightage.Industry] = individualWeightage.TotalCurrentValue;
                }
                else
                {
                    portfolioComposition.CurrentValueAmountByIndustry[individualWeightage.Industry] += individualWeightage.TotalCurrentValue;
                }
            }
        }