private string GetStockValuation(double stockPrice, List <MedpsGurufocusModel> stockPriceList) { MedpsGurufocusModel nearestPredictedPrice = stockPriceList.First(x => x.PriceDate > DateTime.Today); MedpsGurufocusModel lastPrice = stockPriceList.Last(x => x.PriceDate <= DateTime.Today); double avgPriceChange = (nearestPredictedPrice.PriceValue - lastPrice.PriceValue) / (nearestPredictedPrice.PriceDate - lastPrice.PriceDate).Days; double expectedTodayPrice = lastPrice.PriceValue + avgPriceChange * (DateTime.Today - lastPrice.PriceDate).Days; double difference = Math.Abs((expectedTodayPrice - stockPrice) / stockPrice) * 100; string stockValuation = string.Empty; if (difference < 5) { stockValuation = "Справедливо оценена"; } else if (difference < 20) { stockValuation = (stockPrice < expectedTodayPrice) ? "Недооценена" : "Переоценена"; } else { stockValuation = (stockPrice < expectedTodayPrice) ? "Сильно недооценена" : "Сильно переоценена"; } return(stockValuation); }
private bool CheckConditions(Company company, List <MedpsGurufocusModel> stockPriceList) { var culture = new CultureInfo("ru-RU"); bool firstCondition = ( company.Status == "Справедливо оценена" || company.Status == "Недооценена" || company.Status == "Сильно недооценена"); bool secondCondition = false; MedpsGurufocusModel lastPredictedPrice = stockPriceList.Last(); double yearsDifference = (lastPredictedPrice.PriceDate - DateTime.Today).TotalDays / 365; double priceDifference = (lastPredictedPrice.PriceValue - company.Price) / company.Price * 100; switch (company.Category) { case "growrecommendations": if (lastPredictedPrice.PriceValue > company.Price && (priceDifference / yearsDifference) >= 15) { secondCondition = true; } break; case "dividends": case "biotech": if (lastPredictedPrice.PriceValue > company.Price) { secondCondition = true; } break; default: break; } company.FutureForecast = new StockFutureForecast { PriceDifference = (int)priceDifference, Date = lastPredictedPrice.PriceDate.ToString("MMMM yyyy", culture) }; return(firstCondition && secondCondition); }
private async Task <List <MedpsGurufocusModel> > GetStockPriceList(string stockId) { var stockPriceList = new List <MedpsGurufocusModel>(); string result = await SendWebRequest($"https://www.gurufocus.com/reader/_api/chart/{stockId}/valuation"); JsonElement root = GetJsElemFromString(result); try { // medps - это массив вида [[String date1, Double price1],...,[String dateN, Double priceN]], // хранящий цены акции в прошлом и прогнозируемые цены в будущем // он есть не для всех акций, поэтому тут может возникнуть KeyNotFoundException JsonElement medps = root.GetProperty("medps"); // преобразуем medps в удобную форму foreach (JsonElement stockPriceArray in medps.EnumerateArray()) { var medpsModel = new MedpsGurufocusModel(); foreach (JsonElement stockPrice in stockPriceArray.EnumerateArray()) { if (stockPrice.ValueKind == JsonValueKind.String) { medpsModel.PriceDate = DateTime.Parse(stockPrice.GetString()); } else { medpsModel.PriceValue = stockPrice.GetDouble(); } } stockPriceList.Add(medpsModel); } } catch (KeyNotFoundException) { // вернем пустой список в случае отсутствия medps return(new List <MedpsGurufocusModel>()); } return(stockPriceList); }