Exemplo n.º 1
0
        private PortfolioHistory CreatePortfolioHistoryForDate(DateTime date, DomainObjects.Portfolio.Portfolio portfolio, List <AssetValue> currentAssetValues, List <AssetValue> previousAssetValues)
        {
            var projection = PortfolioBusiness.GetProjectionAtDate(date, portfolio);

            if (projection != null)
            {
                var assetsIds = projection.Distribution.Select(d => d.AssetId).Distinct();
                if (assetsIds.Count() == currentAssetValues.Count && assetsIds.Count() == previousAssetValues.Count)
                {
                    var portfolioRealValue = 0.0;
                    foreach (var assetDistribution in projection.Distribution)
                    {
                        var currentAssetValue  = currentAssetValues.FirstOrDefault(a => a.AssetId == assetDistribution.AssetId);
                        var previousAssetValue = previousAssetValues.FirstOrDefault(a => a.AssetId == assetDistribution.AssetId);

                        portfolioRealValue += currentAssetValue.Value / previousAssetValue.Value * assetDistribution.Percent;
                    }
                    var portfolioHistory = new PortfolioHistory()
                    {
                        Date                       = date,
                        PortfolioId                = portfolio.Id,
                        RealValue                  = portfolioRealValue - 100.0,
                        ProjectionValue            = projection.ProjectionValue,
                        OptimisticProjectionValue  = projection.OptimisticProjectionValue,
                        PessimisticProjectionValue = projection.PessimisticProjectionValue
                    };
                    Data.Insert(portfolioHistory);
                    return(portfolioHistory);
                }
            }
            return(null);
        }
Exemplo n.º 2
0
        public Projection Create(string email, int portfolioId, double projectionValue, RiskType risk, double? optimisticProjection,
            double? pessimisticProjection, Dictionary<int, double> distribution)
        {
            var user = UserBusiness.GetValidUser(email);
            var portfolio = PortfolioBusiness.GetValidByOwner(user.Id, portfolioId);
            if (portfolio == null)
                throw new ArgumentException("Invalid portfolio.");

            return Create(portfolio, projectionValue, risk, optimisticProjection, pessimisticProjection, distribution);
        }
Exemplo n.º 3
0
        public List <Model.Portfolio.Distribution> ListByUserPortfolio(string email, int portfolioId)
        {
            var user      = UserBusiness.GetValidUser(email);
            var purchase  = Task.Factory.StartNew(() => BuyBusiness.Get(user.Id, portfolioId));
            var portfolio = Task.Factory.StartNew(() => PortfolioBusiness.GetWithDetails(portfolioId));

            Task.WaitAll(purchase, portfolio);

            var purchased = purchase.Result != null && BuyBusiness.IsValidPurchase(purchase.Result) && purchase.Result.LastTransaction.TransactionStatus == TransactionStatus.Success.Value;
            var owned     = portfolio.Result != null && portfolio.Result.Advisor.UserId == user.Id;

            if (!purchased && !owned)
            {
                throw new ArgumentException("Invalid portfolio distribution.");
            }

            return(ListByProjection(portfolio.Result.ProjectionId.Value));
        }
Exemplo n.º 4
0
        public DomainObjects.Portfolio.Portfolio UpdateWithDistribution(string email, int portfolioId, decimal price, string name,
                                                                        string description, Dictionary <int, double> distribution)
        {
            var user      = UserBusiness.GetValidUser(email);
            var portfolio = GetValidByOwner(user.Id, portfolioId);

            if (portfolio == null)
            {
                throw new ArgumentException("Invalid portfolio.");
            }

            var projection = ProjectionBusiness.Get(portfolio.ProjectionId.Value);

            if (projection == null)
            {
                throw new ArgumentException("Invalid projection.");
            }

            var risk = PortfolioBusiness.GetRisk(projection.ProjectionValue, distribution);

            using (var transaction = new TransactionalDapperCommand())
            {
                var portfolioDetail = PortfolioDetailBusiness.SetNew(portfolioId, price, name, description, true);
                transaction.Insert(portfolioDetail);

                var newProjection = ProjectionBusiness.SetNew(portfolio.Id, projection.ProjectionValue, risk, projection.OptimisticProjectionValue, projection.PessimisticProjectionValue);
                transaction.Insert(newProjection);

                var distributions = DistributionBusiness.SetNew(newProjection.Id, distribution);
                foreach (Distribution dist in distributions)
                {
                    transaction.Insert(dist);
                }

                portfolio.ProjectionId = newProjection.Id;
                transaction.Update(portfolio);

                newProjection.Distribution = distributions;
                portfolio.Detail           = portfolioDetail;
                portfolio.Projection       = newProjection;
                transaction.Commit();
            }
            return(portfolio);
        }
Exemplo n.º 5
0
        public List <Distribution> Create(string email, int portfolioId, Dictionary <int, double> distribution)
        {
            var user      = UserBusiness.GetValidUser(email);
            var portfolio = PortfolioBusiness.GetValidByOwner(user.Id, portfolioId);

            if (portfolio == null)
            {
                throw new ArgumentException("Invalid portfolio.");
            }

            var projection = ProjectionBusiness.Get(portfolio.ProjectionId.Value);

            if (projection == null)
            {
                throw new ArgumentException("Invalid projection.");
            }

            var risk = PortfolioBusiness.GetRisk(projection.ProjectionValue, distribution);

            return(ProjectionBusiness.Create(portfolio, projection.ProjectionValue, risk, projection.OptimisticProjectionValue,
                                             projection.PessimisticProjectionValue, distribution).Distribution);
        }