Ejemplo n.º 1
0
        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));
        }
Ejemplo n.º 2
0
        public void Evaluate()
        {
            var purchases = BuyBusiness.ListPendingEscrowResult();

            foreach (var purchase in purchases)
            {
                try
                {
                    var history = PortfolioHistoryBusiness.ListHistory(purchase.PortfolioId);
                    if (history.Max(c => c.Date) < purchase.ExpirationDate.Value)
                    {
                        continue;
                    }

                    var projection = ProjectionBusiness.Get(purchase.ProjectionId);
                    var dailyEstimatedPercentage = Util.Util.ConvertMonthlyToDailyRate(projection.ProjectionValue);
                    var estimatedPercentage      = Math.Pow(1.0 + dailyEstimatedPercentage, purchase.Days) - 1.0;

                    var startDate = purchase.ExpirationDate.Value.AddDays(-purchase.Days);
                    if (startDate < history.Min(c => c.Date))
                    {
                        startDate = history.Min(c => c.Date);
                    }

                    var historyOfPeriod     = history.Where(c => c.Date >= startDate && c.Date <= purchase.ExpirationDate.Value);
                    var historyResult       = PortfolioHistoryBusiness.GetHistoryResult(historyOfPeriod);
                    var performedPercentage = historyResult.Value / 100.0;

                    decimal buyerTokenAmount = 0, ownerTokenAmount = 0;
                    if (estimatedPercentage > performedPercentage)
                    {
                        buyerTokenAmount = purchase.Price;
                    }
                    else
                    {
                        ownerTokenAmount = purchase.Price;
                    }

                    using (var transaction = new TransactionalDapperCommand())
                    {
                        var escrowResult = new EscrowResult()
                        {
                            BuyId            = purchase.Id,
                            CreationDate     = DateTime.UtcNow,
                            BuyerTokenResult = buyerTokenAmount,
                            OwnerTokenResult = ownerTokenAmount
                        };
                        transaction.Insert(escrowResult);
                        var trans = TransactionBusiness.SetNew(purchase.UserId);
                        transaction.Insert(trans);
                        var escrowResultTrans = EscrowResultTransactionBusiness.SetNew(escrowResult.Id, trans.Id);
                        transaction.Insert(escrowResultTrans);
                        transaction.Commit();
                    }
                }
                catch (Exception ex)
                {
                    Logger.LogCritical(ex, $"Error on evaluate purchase {purchase.Id}");
                }
            }
        }