public async Task <OperationResult <ValuePercent> > GetPortfolioPaymentProfit(int portfolioId, int userId) { var result = await GetPortfolioPayments(portfolioId, userId); if (!result.IsSuccess) { return(new OperationResult <ValuePercent>() { IsSuccess = result.IsSuccess, Message = result.Message, Result = null }); } var payments = result.Result; var profit = payments.Aggregate(0, (sum, payment) => sum + payment.PaymentValue); var investingSum = _balanceService.GetInvestSum(portfolioId, userId); var percent = FinanceHelpers.DivWithOneDigitRound(profit, investingSum); return(new OperationResult <ValuePercent>() { IsSuccess = true, Message = "Дивидендная прибыль", Result = new ValuePercent() { Value = profit, Percent = percent } }); }
private async Task <AssetData> GetAssetData(AssetInfo assetInfo) { var name = await assetInfo.GetName(); var price = await assetInfo.GetPrice(); var percentChange = await assetInfo.GetPriceChange(); var allPrice = await assetInfo.GetAllPrice(); var paperProfit = await assetInfo.GetPaperProfit(); var paperProfitPercent = await assetInfo.GetPaperProfitPercent(); var updateTime = await assetInfo.GetUpdateTime(); var sumPayments = assetInfo.GetSumPayments(); return(new AssetData() { Name = name, Ticket = assetInfo.Ticket, Price = price, PriceChange = FinanceHelpers.GetPriceDouble(percentChange), AllPrice = FinanceHelpers.GetPriceDouble(allPrice), BoughtPrice = FinanceHelpers.GetPriceDouble(assetInfo.BoughtPrice), Amount = assetInfo.Amount, PaidDividends = FinanceHelpers.GetPriceDouble(sumPayments), PaperProfit = FinanceHelpers.GetPriceDouble(paperProfit), PaperProfitPercent = paperProfitPercent, UpdateTime = updateTime, NearestDividend = assetInfo.GetNearestPayment(), Payments = assetInfo.PaymentsData }); }
public async Task AggregatePaperProfit() { var result1 = await _aggregatePortfolioService.AggregatePaperProfit(new[] { 10, 11 }, 1); var result2 = await _aggregatePortfolioService.AggregatePaperProfit(new[] { 10, 11, 12 }, 1); var result3 = await _aggregatePortfolioService.AggregatePaperProfit(new[] { 10 }, 1); var result4 = await _aggregatePortfolioService.AggregatePaperProfit(new[] { 12 }, 2); var result5 = await _aggregatePortfolioService.AggregatePaperProfit(new[] { 12 }, 1); Assert.IsTrue(result1.IsSuccess); Assert.AreEqual(34720 * 2 + 26580 * 2 + 20000 + 6283 - 100000 + 26580, result1.Result.Value); Assert.AreEqual( FinanceHelpers.DivWithOneDigitRound(34720 * 2 + 26580 * 2 + 20000 + 6283 - 100000 + 26580, 3000000), result1.Result.Percent); Assert.IsTrue(result3.IsSuccess); Assert.AreEqual(34720 * 2 + 26580 * 2 + 20000 + 6283 - 100000, result3.Result.Value); Assert.AreEqual( FinanceHelpers.DivWithOneDigitRound(34720 * 2 + 26580 * 2 + 20000 + 6283 - 100000, 2000000), result3.Result.Percent); Assert.IsTrue(result4.IsSuccess); Assert.AreEqual(34720, result4.Result.Value); Assert.AreEqual(FinanceHelpers.DivWithOneDigitRound(34720, 1500000), result4.Result.Percent); Assert.IsFalse(result2.IsSuccess, "Считается портфель чужого пользователя"); Assert.IsFalse(result5.IsSuccess, "Считается портфель чужого пользователя"); }
public async Task GetPaperProfit() { var profit1 = await _portfolioService.GetPaperProfit(10, 1); var profit2 = await _portfolioService.GetPaperProfit(11, 1); var profit3 = await _portfolioService.GetPaperProfit(12, 2); var profit4 = await _portfolioService.GetPaperProfit(12, 1); Assert.IsTrue(profit1.IsSuccess, "Неуспешное выполнение операции"); Assert.AreEqual(34720 * 2 + 26580 * 2 + 20000 + 6283 - 100000, profit1.Result.Value, "Неверная прибыль"); Assert.AreEqual(FinanceHelpers.DivWithOneDigitRound(34720 * 2 + 26580 * 2 + 20000 + 6283 - 100000, 2000000), profit1.Result.Percent, "Неверный процент"); Assert.IsTrue(profit2.IsSuccess, "Неуспешное выполнение операции"); Assert.AreEqual(26580, profit2.Result.Value, "Неверная прибыль"); Assert.AreEqual(FinanceHelpers.DivWithOneDigitRound(26580, 1000000), profit2.Result.Percent, "Неверный процент"); Assert.IsTrue(profit3.IsSuccess, "Неуспешное выполнение операции"); Assert.AreEqual(34720, profit3.Result.Value, "Неверная прибыль"); Assert.AreEqual(FinanceHelpers.DivWithOneDigitRound(34720, 1500000), profit3.Result.Percent, "Неверный процент"); Assert.IsFalse(profit4.IsSuccess, "Прибыль в чужом портфеле"); }
public static AllPortfoliosReport GetAllPortfoliosReport(int userId, IMarketService marketService, IBalanceService balanceService, IAggregatePortfolioService aggregatePortfolioService) { var allCostResult = aggregatePortfolioService.AggregateCost(new[] { 1, 2 }, userId).Result; var allCost = FinanceHelpers.GetPriceDouble(allCostResult.Result); var paperProfitResult = aggregatePortfolioService.AggregatePaperProfit(new[] { 1, 2 }, userId).Result.Result; var allPaperProfit = FinanceHelpers.GetPriceDouble(paperProfitResult.Value); var allPaperProfitPercent = paperProfitResult.Percent; var paymentProfitResult = aggregatePortfolioService.AggregatePaymentProfit(new[] { 1, 2 }, userId).Result.Result; var allPaymentProfit = FinanceHelpers.GetPriceDouble(paymentProfitResult.Value); var allPaymentProfitPercent = paymentProfitResult.Percent; var allInvestSum = FinanceHelpers.GetPriceDouble(balanceService.GetAggregateInvestSum(new [] { 1, 2 }, userId)); var balanceResult = balanceService.AggregateBalance(new[] { 1, 2 }, userId).Result; var allBalance = FinanceHelpers.GetPriceDouble(balanceResult.Result); return(new AllPortfoliosReport() { AllCost = allCost, AllPaperProfit = allPaperProfit, AllPaperProfitPercent = allPaperProfitPercent, AllPaymentProfit = allPaymentProfit, AllPaymentProfitPercent = allPaymentProfitPercent, AllInvestSum = allInvestSum, AllUserBalance = allBalance }); }
public async Task <OperationResult <ValuePercent> > AggregatePaperProfit(IEnumerable <int> portfolioIds, int userId) { var sumProfit = 0; var sumInvest = 0; var ids = portfolioIds.ToList(); foreach (var portfolioId in ids) { var resultProfit = await _portfolioService.GetPaperProfit(portfolioId, userId); var resultInvestSum = _balanceService.GetInvestSum(portfolioId, userId); if (!resultProfit.IsSuccess) { return(resultProfit); } sumProfit += resultProfit.Result.Value; sumInvest += resultInvestSum; } var percent = FinanceHelpers.DivWithOneDigitRound(sumProfit, sumInvest); return(new OperationResult <ValuePercent>() { IsSuccess = true, Message = $"Суммарная бумажная прибыль портфелей(я) c id={string.Join(", ", ids)}", Result = new ValuePercent() { Value = sumProfit, Percent = percent } }); }
public static async Task <AssetPricesReport> GetAllAssetPricesReport(int userId, IMarketService marketService) { var assetPrices = await marketService.GetAllAssetPrices(userId); return(new AssetPricesReport() { StockPrice = FinanceHelpers.GetPriceDouble(assetPrices.StockPrice), FondPrice = FinanceHelpers.GetPriceDouble(assetPrices.FondPrice), BondPrice = FinanceHelpers.GetPriceDouble(assetPrices.BondPrice) }); }
public override async Task <int> GetAllPrice() { var price = await GetPrice(); if (Math.Abs(price - (-1)) < 0.1) { return(0); } var nkd = FinanceHelpers.GetValueOfColumnSecurities("ACCRUEDINT", Data).GetDouble(); var nominal = FinanceHelpers.GetValueOfColumnSecurities("FACEVALUE", Data).GetDouble(); return(FinanceHelpers.GetPriceInt((price / 100 * nominal + nkd) * Amount)); }
public async Task <int> GetPriceChange() { var data = await GetData(); var jsonPriceChange = FinanceHelpers.GetValueOfColumnMarketdata("LASTTOPREVPRICE", data); if (jsonPriceChange.ValueKind == JsonValueKind.Undefined) { return(-1); } var changePercent = jsonPriceChange.GetDouble(); return(FinanceHelpers.GetPriceInt(changePercent)); }
public async Task <string> GetUpdateTime() { var data = await GetData(); var jsonUpdateTime = FinanceHelpers.GetValueOfColumnMarketdata("TIME", data); if (jsonUpdateTime.ValueKind == JsonValueKind.Undefined) { return(""); } var updateTime = jsonUpdateTime.GetString(); return(updateTime); }
public async Task <string> GetName() { var data = await GetData(); var result = FinanceHelpers.GetValueOfColumnSecurities("SHORTNAME", data); try { return(result.GetString()); } catch (Exception) { return(""); } }
//TODO Проверка на нужного пользователя public async Task <OperationResult> RefillBalance(int portfolioId, int price, DateTime date) { var portfolio = await _financeDataService.EfContext.Portfolios.FindAsync(portfolioId); var refillAction = await _financeDataService.EfContext.CurrencyActions.FirstOrDefaultAsync(a => a.Name == SeedFinanceData.REFILL_ACTION); if (portfolio == null) { return(new OperationResult() { IsSuccess = false, Message = "Портфель не найден" }); } if (price < 0) { return(new OperationResult() { IsSuccess = false, Message = "Нельзя пополнить на отрицательную сумму" }); } var currencyOperation = new CurrencyOperation() { Portfolio = portfolio, PortfolioId = portfolioId, CurrencyAction = refillAction, CurrencyActionId = refillAction.Id, CurrencyId = SeedFinanceData.RUB_CURRENCY_ID, CurrencyName = SeedFinanceData.RUB_CURRENCY_NAME, Date = date, Price = price }; await _financeDataService.EfContext.CurrencyOperations.AddAsync(currencyOperation); await _financeDataService.EfContext.SaveChangesAsync(); return(new OperationResult() { IsSuccess = true, Message = $"Баланс пополнен на {FinanceHelpers.GetPriceDouble(price)} ₽ успешно" }); }
public static IEnumerable <PaymentDataReport> GetAllFuturePaymentsReport(int userId, IMarketService marketService) { var payments = marketService.GetAllFuturePayments(userId); return(payments .Select(p => new PaymentDataReport() { Name = p.Name, Ticket = p.Ticket, PaymentValue = FinanceHelpers.GetPriceDouble(p.PaymentValue), Amount = p.Amount, AllPayment = FinanceHelpers.GetPriceDouble(p.AllPayment), CurrencyId = p.CurrencyId, RegistryCloseDate = p.RegistryCloseDate }) .OrderBy(p => p.RegistryCloseDate)); }
public async Task <double> GetPrice() { var data = await GetData(); var jsonPrice = FinanceHelpers.GetValueOfColumnMarketdata("LAST", data); var jsonDecimals = FinanceHelpers.GetValueOfColumnSecurities("DECIMALS", data); if (jsonPrice.ValueKind == JsonValueKind.Undefined) { return(-1); } var price = jsonPrice.GetDouble(); var decimals = jsonDecimals.GetInt32(); return(Math.Round(price, decimals)); }
public static async Task <List <BondReport> > GetBondReports(int userId, IPortfolioService portfolioService, int portfolioId) { var bonds = portfolioService.GetBonds(portfolioId, userId); var bondReports = new List <BondReport>(); foreach (var bondInfo in bonds) { var name = await bondInfo.GetName(); var price = await bondInfo.GetPrice(); var percentChange = await bondInfo.GetPriceChange(); var allPrice = await bondInfo.GetAllPrice(); var paperProfit = await bondInfo.GetPaperProfit(); var paperProfitPercent = await bondInfo.GetPaperProfitPercent(); var updateTime = await bondInfo.GetUpdateTime(); var bondReport = new BondReport() { Name = name, Ticket = bondInfo.Ticket, Price = price, PriceChange = FinanceHelpers.GetPriceDouble(percentChange), AllPrice = FinanceHelpers.GetPriceDouble(allPrice), BoughtPrice = FinanceHelpers.GetPriceDouble(bondInfo.BoughtPrice), Amount = bondInfo.Amount, PaidPayments = FinanceHelpers.GetPriceDouble(bondInfo.GetSumPayments()), PaperProfit = FinanceHelpers.GetPriceDouble(paperProfit), PaperProfitPercent = paperProfitPercent, UpdateTime = updateTime, NearestPayment = bondInfo.GetNearestPayment(), HasAmortized = bondInfo.HasAmortized, AmortizationDate = bondInfo.AmortizationDate }; bondReports.Add(bondReport); } return(bondReports); }
public static async Task <List <StockReport> > GetStockReports(int userId, IPortfolioService portfolioService, int portfolioId) { var stocks = portfolioService.GetStocks(portfolioId, userId); var stockReports = new List <StockReport>(); foreach (var stockInfo in stocks) { var name = await stockInfo.GetName(); var price = await stockInfo.GetPrice(); var percentChange = await stockInfo.GetPriceChange(); var allPrice = await stockInfo.GetAllPrice(); var paperProfit = await stockInfo.GetPaperProfit(); var paperProfitPercent = await stockInfo.GetPaperProfitPercent(); var updateTime = await stockInfo.GetUpdateTime(); var stockReport = new StockReport() { Name = name, Ticket = stockInfo.Ticket, Price = price, PriceChange = FinanceHelpers.GetPriceDouble(percentChange), AllPrice = FinanceHelpers.GetPriceDouble(allPrice), BoughtPrice = FinanceHelpers.GetPriceDouble(stockInfo.BoughtPrice), Amount = stockInfo.Amount, PaidDividends = FinanceHelpers.GetPriceDouble(stockInfo.GetSumPayments()), PaperProfit = FinanceHelpers.GetPriceDouble(paperProfit), PaperProfitPercent = paperProfitPercent, UpdateTime = updateTime, NearestDividend = stockInfo.GetNearestPayment() }; stockReports.Add(stockReport); } return(stockReports); }
public static async Task <List <FondReport> > GetFondReports(int userId, IPortfolioService portfolioService, int portfolioId) { var fonds = portfolioService.GetFonds(portfolioId, userId); var fondReports = new List <FondReport>(); foreach (var fondInfo in fonds) { var name = await fondInfo.GetName(); var price = await fondInfo.GetPrice(); var percentChange = await fondInfo.GetPriceChange(); var allPrice = await fondInfo.GetAllPrice(); var paperProfit = await fondInfo.GetPaperProfit(); var paperProfitPercent = await fondInfo.GetPaperProfitPercent(); var updateTime = await fondInfo.GetUpdateTime(); var fondReport = new FondReport() { Name = name, Ticket = fondInfo.Ticket, Price = price, PriceChange = FinanceHelpers.GetPriceDouble(percentChange), AllPrice = FinanceHelpers.GetPriceDouble(allPrice), BoughtPrice = FinanceHelpers.GetPriceDouble(fondInfo.BoughtPrice), Amount = fondInfo.Amount, PaperProfit = FinanceHelpers.GetPriceDouble(paperProfit), PaperProfitPercent = paperProfitPercent, UpdateTime = updateTime, }; fondReports.Add(fondReport); } return(fondReports); }
private async Task <List <FondReport> > GetFondReports(IEnumerable <FondInfo> fonds) { var fondReports = new List <FondReport>(); foreach (var fondInfo in fonds) { var name = await fondInfo.GetName(); var price = await fondInfo.GetPrice(); var percentChange = await fondInfo.GetPriceChange(); var allPrice = await fondInfo.GetAllPrice(); var paperProfit = await fondInfo.GetPaperProfit(); var paperProfitPercent = await fondInfo.GetPaperProfitPercent(); var updateTime = await fondInfo.GetUpdateTime(); var fondReport = new FondReport() { Name = name, Ticket = fondInfo.Ticket, Price = price, PriceChange = FinanceHelpers.GetPriceDouble(percentChange), AllPrice = FinanceHelpers.GetPriceDouble(allPrice), BoughtPrice = FinanceHelpers.GetPriceDouble(fondInfo.BoughtPrice), Amount = fondInfo.Amount, PaperProfit = FinanceHelpers.GetPriceDouble(paperProfit), PaperProfitPercent = paperProfitPercent, UpdateTime = updateTime, }; fondReports.Add(fondReport); } return(fondReports); }
public async Task <OperationResult <ValuePercent> > GetPaperProfit(int portfolioId, int userId) { var portfolio = await _financeData.EfContext.Portfolios.FindAsync(portfolioId); var validationResult = CommonValidate <ValuePercent>(portfolioId, userId, portfolio); if (validationResult != null) { return(validationResult); } var portfolioData = GetPortfolioData(portfolioId); var sumProfit = 0; foreach (var asset in portfolioData.Assets) { var profit = await asset.GetPaperProfit(); sumProfit += profit; } var investingSum = _balanceService.GetInvestSum(portfolioId, userId); var percent = FinanceHelpers.DivWithOneDigitRound(sumProfit, investingSum); return(new OperationResult <ValuePercent>() { IsSuccess = true, Message = $"Бумажная прибыль портфеля {portfolio.Name}", Result = new ValuePercent() { Value = sumProfit, Percent = percent } }); }
public void GetPriceDouble__wholeNumber() { var doublePrice = FinanceHelpers.GetPriceDouble(30215200); Assert.AreEqual(302152, doublePrice); }
public override async Task <int> GetAllPrice() { var price = await GetPrice(); return(FinanceHelpers.GetPriceInt(price * Amount)); }
public async Task <double> GetPaperProfitPercent() { var profit = await GetPaperProfit(); return(FinanceHelpers.DivWithOneDigitRound(profit, BoughtPrice)); }
public void GetValueOfColumn__invalidIndex() { var strPrice = FinanceHelpers.GetValueOfColumnMarketdata("BLABLA", _data); Assert.AreEqual(JsonValueKind.Undefined, strPrice.ValueKind); }
public void GetValueOfColumnSecurities() { var shortName = FinanceHelpers.GetValueOfColumnSecurities("SHORTNAME", _data); Assert.AreEqual("Yandex clA", shortName.GetString()); }
public void GetValueOfColumnMarketdata() { var strPrice = FinanceHelpers.GetValueOfColumnMarketdata("LAST", _data); Assert.AreEqual(434720, (int)(strPrice.GetDouble() * 100)); }
public void GetPriceDouble() { var doublePrice = FinanceHelpers.GetPriceDouble(302152); Assert.AreEqual(3021.52, doublePrice); }