예제 #1
0
        public async Task <PortfolioVm> Handle(GetPortfolioRequest request, CancellationToken cancellationToken)
        {
            var portfolio      = new PortfolioVm();
            var holdingsResult = new List <HoldingVm>();

            var holdings = _context.Holdings
                           .Where(x => x.UserId == _identityService.UserId)
                           .OrderByDescending(x => x.Created)
                           .ToList();

            foreach (var holding in holdings)
            {
                holding.SetAccountDetails(await _holdingsService.GetDetailsByTickerAsync(holding.Ticker));
                holding.SetCurrency(AbstractEnumeration.FromValue <Currency>(holding.CurrencyId));
                holdingsResult.Add(_mapper.Map <HoldingVm>(holding));
            }

            portfolio.TotalShares          = holdings.Sum(h => h.Shares);
            portfolio.TotalAverageCost     = holdings.Average(h => h.PurchasePrice);
            portfolio.TotalCostBasis       = holdings.Sum(h => h.CostBasis);
            portfolio.TotalMarketValue     = holdings.Sum(h => h.MarketValue);
            portfolio.TotalGainLoss        = holdings.Sum(h => h.GainLoss);
            portfolio.TotalGainLossPercent = portfolio.TotalGainLoss / portfolio.TotalCostBasis;

            foreach (var holdingResult in holdingsResult)
            {
                holdingResult.Weight = holdingResult.CostBasis / portfolio.TotalMarketValue;
            }

            portfolio.Holdings = holdingsResult;

            return(portfolio);
        }
예제 #2
0
        public async Task <HoldingVm> Handle(GetHoldingByIdRequest request, CancellationToken cancellationToken)
        {
            var holding = await _context.Holdings
                          .AsNoTracking()
                          .Include(x => x.Account)
                          .FirstOrDefaultAsync(h => h.Id == request.Id && h.UserId == _identityService.UserId, cancellationToken: cancellationToken);

            holding?.SetCurrency(AbstractEnumeration.FromValue <Currency>(holding.CurrencyId));
            holding?.SetAccountDetails(await _holdingsService.GetDetailsByTickerAsync(holding.Ticker));

            return(_mapper.Map <HoldingVm>(holding));
        }
예제 #3
0
        public async Task <List <HoldingVm> > Handle(GetHoldingsRequest request, CancellationToken cancellationToken)
        {
            var results = new List <HoldingVm>();

            var holdings = _context.Holdings
                           .Where(x => x.UserId == _identityService.UserId)
                           .OrderByDescending(x => x.Created)
                           .ToList();

            foreach (var holding in holdings)
            {
                holding.SetCurrency(AbstractEnumeration.FromValue <Currency>(holding.CurrencyId));
                holding.SetAccountDetails(await _holdingsService.GetDetailsByTickerAsync(holding.Ticker));
                results.Add(_mapper.Map <HoldingVm>(holding));
            }

            return(await Task.Run(() => results.ToList(), cancellationToken));
        }