public async Task <string> SelectRandomGoodFirms() { _logger.LogInformation("Starting to select firms for the requester"); List <PiotroskiScoreMd> selectedFirms; try { selectedFirms = _connectionHandlerCF.Get(r => r.Rating >= 7 && r.FYear == DateTime.Now.Year) .ToList(); if (selectedFirms == null || !selectedFirms.Any()) { selectedFirms = _connectionHandlerCF.Get(r => r.Rating >= 7 && r.FYear == DateTime.Now.Year - 1) .ToList(); } if (!selectedFirms.Any()) { return(""); } } catch (Exception ex) { _logger.LogCritical("Error while getting data from MongoDb from Collection PiotroskiScore"); _logger.LogCritical(ex.Message); return(""); } selectedFirms.Shuffle(); selectedFirms = selectedFirms.Take(4) .OrderByDescending(r => r.Rating) .ThenByDescending(r => r.Revenue) .ToList(); var messageString = new StringBuilder(); messageString.Append("Here are a few recommendations for you.\n"); foreach (var selectedFirm in selectedFirms) { var companyName = await _resolveCompanyName.ResolveTicker(selectedFirm.Ticker); var symbol = Regex.Replace(selectedFirm.Ticker, ".{1}", "$0 "); messageString.Append($"{companyName} with ticker {symbol} has Piotrosk-F Score of {selectedFirm.Rating}\n"); QuotesFromWorldTrading quotes = await _obtainStockQuote.GetStockQuotes(selectedFirm.Ticker); var companyStats = await ObtainCompanyStats(selectedFirm.Ticker); if (quotes != null && quotes.Data.Any()) { var currentPrice = (float)quotes.Data[0].Price; messageString.Append($"It last traded at ${Math.Round(currentPrice,2)}. "); } if (companyStats != null && companyStats.week52high != 0) { var week52Low = Math.Round(companyStats.week52low, 2); var week52High = Math.Round(companyStats.week52high, 2); var peRatio = companyStats.peRatio == null ? 0 : companyStats.peRatio; messageString.Append($"52 week range is between ${week52Low} and {week52High} and P E Ratio is {peRatio}. "); } messageString.Append("\n\n"); } return(messageString.ToString()); }