public Goal SetNew(int userId, int goalOptionId, int timeframe, int risk, double?targetAmount, double startingAmount, double monthlyContribution) { GoalOptionsBusiness.Get(goalOptionId); RiskType riskType = RiskType.Get(risk); if (timeframe <= 0) { throw new ArgumentException("Invalid timeframe for goal."); } if ((startingAmount == 0 && monthlyContribution == 0) || startingAmount < 0 || monthlyContribution < 0) { throw new ArgumentException("Invalid contribution."); } var goal = new Goal(); goal.UserId = userId; goal.GoalOptionId = goalOptionId; goal.CreationDate = DateTime.UtcNow; goal.MonthlyContribution = monthlyContribution; goal.StartingAmount = startingAmount; goal.TargetAmount = targetAmount; goal.Timeframe = timeframe; goal.Risk = riskType.Value; return(goal); }
public Projection CreateProjection(string email, int portfolioId, double projection, int risk, double?optimisticProjection, double?pessimisticProjection, Dictionary <int, double> distribution) { return(ProjectionBusiness.Create(email, portfolioId, projection, RiskType.Get(risk), optimisticProjection, pessimisticProjection, distribution)); }
public KeyValuePair <int, IEnumerable <Model.Portfolio> > ListRoboAdvisors(string email, int goalOptionId, int risk) { var user = UserBusiness.GetValidUser(email); var purchases = Task.Factory.StartNew(() => BuyBusiness.ListPurchases(user.Id)); var goalOption = GoalOptionsBusiness.Get(goalOptionId); var riskType = RiskType.Get(risk, goalOption.Risk); var riskPriority = RiskType.GetRiskPriority(riskType); var advisors = Data.ListRobosAvailable(); var portfolios = Task.Factory.StartNew(() => PortfolioBusiness.List(advisors.Select(c => c.Id))); Task.WaitAll(portfolios); var portfolioQty = Task.Factory.StartNew(() => BuyBusiness.ListPortfoliosPurchases(portfolios.Result.SelectMany(c => c.Value.Select(x => x.Id)))); List <Task <List <PortfolioHistory> > > histories = new List <Task <List <PortfolioHistory> > >(); foreach (DomainObjects.Portfolio.Portfolio portfolio in portfolios.Result.SelectMany(c => c.Value)) { histories.Add(Task.Factory.StartNew(() => PortfolioHistoryBusiness.ListHistory(portfolio.Id))); } Task.WaitAll(purchases, portfolioQty); Task.WaitAll(histories.ToArray()); List <Model.Portfolio> portfolioWithSameRisk = new List <Model.Portfolio>(); List <Model.Portfolio> portfolioWithLittleLowerRisk = new List <Model.Portfolio>(); List <Model.Portfolio> portfolioWithLittleHigherRisk = new List <Model.Portfolio>(); List <Model.Portfolio> portfolioWithLowerRisk = new List <Model.Portfolio>(); List <Model.Portfolio> portfolioWithHigherRisk = new List <Model.Portfolio>(); List <Model.Portfolio> portfolioWithVeryLowerRisk = new List <Model.Portfolio>(); List <Model.Portfolio> portfolioWithVeryHigherRisk = new List <Model.Portfolio>(); foreach (KeyValuePair <int, List <DomainObjects.Portfolio.Portfolio> > advisorPortfolios in portfolios.Result) { var advisor = advisors.Single(c => c.Id == advisorPortfolios.Key); advisorPortfolios.Value.ForEach(c => c.PortfolioHistory = histories.SelectMany(x => x.Result.Where(g => g.PortfolioId == c.Id)).ToList()); foreach (var r in riskPriority) { var riskFound = advisorPortfolios.Value.SingleOrDefault(c => c.Projection.RiskType == r); if (riskFound != null) { var port = PortfolioBusiness.FillPortfolioModel(riskFound, advisor, user, purchases.Result, portfolioQty.Result); var difference = riskFound.Projection.RiskType.Value - riskType.Value; if (difference == 0) { portfolioWithSameRisk.Add(port); } else if (difference == 1) { portfolioWithLittleHigherRisk.Add(port); } else if (difference == -1) { portfolioWithLittleLowerRisk.Add(port); } else if (difference == 2) { portfolioWithHigherRisk.Add(port); } else if (difference == -2) { portfolioWithLowerRisk.Add(port); } else if (difference > 2) { portfolioWithVeryHigherRisk.Add(port); } else { portfolioWithVeryLowerRisk.Add(port); } break; } } } var result = portfolioWithSameRisk.OrderByDescending(c => c.PurchaseQuantity).ThenByDescending(c => c.ProjectionPercent).ToList(); result.AddRange(portfolioWithLittleLowerRisk.OrderByDescending(c => c.PurchaseQuantity).ThenByDescending(c => c.ProjectionPercent)); result.AddRange(portfolioWithLittleHigherRisk.OrderByDescending(c => c.PurchaseQuantity).ThenByDescending(c => c.ProjectionPercent)); result.AddRange(portfolioWithLowerRisk.OrderByDescending(c => c.PurchaseQuantity).ThenByDescending(c => c.ProjectionPercent)); result.AddRange(portfolioWithHigherRisk.OrderByDescending(c => c.PurchaseQuantity).ThenByDescending(c => c.ProjectionPercent)); result.AddRange(portfolioWithVeryLowerRisk.OrderByDescending(c => c.PurchaseQuantity).ThenByDescending(c => c.ProjectionPercent)); result.AddRange(portfolioWithVeryHigherRisk.OrderByDescending(c => c.PurchaseQuantity).ThenByDescending(c => c.ProjectionPercent)); return(new KeyValuePair <int, IEnumerable <Model.Portfolio> >(riskType.Value, result)); }
public Model.Portfolio Get(string email, int portfolioId) { var user = UserBusiness.GetValidUser(email); var purchase = Task.Factory.StartNew(() => BuyBusiness.Get(user.Id, portfolioId)); var portfolio = Task.Factory.StartNew(() => Get(portfolioId)); Task.WaitAll(portfolio, purchase); var owned = user.Id == portfolio.Result.Advisor.UserId; var purchased = purchase.Result != null && BuyBusiness.IsValidPurchase(purchase.Result); if (portfolio.Result == null || (!owned && !purchased && (!portfolio.Result.Detail.Enabled || !portfolio.Result.Advisor.Detail.Enabled))) { throw new ArgumentException("Invalid portfolio."); } var portfolioQty = Task.Factory.StartNew(() => BuyBusiness.ListPortfoliosPurchases(new int[] { portfolioId })); var history = Task.Factory.StartNew(() => PortfolioHistoryBusiness.ListHistory(portfolioId)); Task <List <Distribution> > distribution = null; Task <List <EscrowResult> > escrowResult = null; Task <decimal?> purchaseAmount = null; if (purchased && purchase.Result.LastTransaction.TransactionStatus == TransactionStatus.Success.Value) { distribution = Task.Factory.StartNew(() => DistributionBusiness.List(new int[] { portfolio.Result.ProjectionId.Value })); Task.WaitAll(history, portfolioQty, distribution); } else if (owned) { distribution = Task.Factory.StartNew(() => DistributionBusiness.List(new int[] { portfolio.Result.ProjectionId.Value })); escrowResult = Task.Factory.StartNew(() => EscrowResultBusiness.ListByPortfolio(portfolio.Result.Id)); purchaseAmount = Task.Factory.StartNew(() => BuyBusiness.ListPortfolioPurchaseAmount(portfolio.Result.Id)); Task.WaitAll(history, portfolioQty, distribution, escrowResult, purchaseAmount); } else { Task.WaitAll(history, portfolioQty); } portfolio.Result.PortfolioHistory = history.Result; var result = FillPortfolioModel(portfolio.Result, portfolio.Result.Advisor, user, purchase.Result != null ? new Buy[] { purchase.Result } : null, portfolioQty.Result); result.Purchased = purchased; result.Histogram = PortfolioHistoryBusiness.GetHistogram(history.Result); result.HistoryData = history.Result.Select(c => new Model.Portfolio.History() { Date = c.Date, Value = c.RealValue }).ToList(); result.AssetDistribution = distribution == null ? null : distribution.Result.Select(c => new Model.Portfolio.Distribution() { Id = c.AssetId, Code = c.Asset.Code, Name = c.Asset.Name, Percentage = c.Percent, Type = (int)c.Asset.Type }).OrderByDescending(c => c.Percentage).ToList(); if (purchased) { GoalOption goalOption = null; if (purchase.Result.Goal != null) { goalOption = GoalOptionsBusiness.Get(purchase.Result.Goal.GoalOptionId); } result.PurchaseData = new Model.Portfolio.Purchase() { CreationDate = purchase.Result.CreationDate, ExpirationDate = purchase.Result.ExpirationDate, Price = purchase.Result.PortfolioDetail.Price, AucEscrow = distribution != null ? purchase.Result.PortfolioDetail.Price : 0, TransactionStatus = (int)purchase.Result.LastTransaction.TransactionStatus, Risk = purchase.Result.Projection.Risk, Goal = purchase.Result.Goal == null ? null : new Model.Portfolio.Goal() { Timeframe = purchase.Result.Goal.Timeframe, MonthlyContribution = purchase.Result.Goal.MonthlyContribution, StartingAmount = purchase.Result.Goal.StartingAmount, TargetAmount = purchase.Result.Goal.TargetAmount, Risk = RiskType.Get(purchase.Result.Goal.Risk, goalOption.Risk).Value } }; } else if (owned) { result.OwnerData = new Model.Portfolio.Owner(); if (purchaseAmount.Result.HasValue && purchaseAmount.Result.Value > 0) { var validResults = escrowResult.Result.Where(c => c.LastTransaction.TransactionStatus == TransactionStatus.Success.Value); result.OwnerData.AucReached = validResults.Sum(c => c.OwnerTokenResult); result.OwnerData.AucLost = validResults.Sum(c => c.BuyerTokenResult); result.OwnerData.AucEscrow = purchaseAmount.Result.Value - result.OwnerData.AucReached - result.OwnerData.AucLost; } } return(result); }