public async Task <IActionResult> PortfolioAsync()
        {
            var userId = _userManager.GetUserId(User);

            //Retrieves data from database
            StockPortfolioViewModel stockPortfolioViewModel = new StockPortfolioViewModel();
            PlayerProfile           player = _ProfileRepository.FindByCondition(p => p.UserId == userId).First();

            stockPortfolioViewModel.PlayProfile = player;
            IEnumerable <PlayerStock> playerStocks = _stockRepository.FindByCondition(p => p.ProfileId == player.Id);

            if (playerStocks.Any())
            {
                //Builds API Url from list of stocks the player owns
                StringBuilder sb = new StringBuilder();
                sb.Append($@"https://cloud.iexapis.com/stable/stock/market/batch/?types=quote&token={_iexcloudKey}&symbols=");


                for (int i = 0; i < playerStocks.Count(); i++)
                {
                    if (i == playerStocks.Count() - 1)
                    {
                        sb.Append(playerStocks.ElementAt(i).StockSymbol);
                    }
                    else
                    {
                        sb.Append(playerStocks.ElementAt(i).StockSymbol + ",");
                    }
                }
                _url = sb.ToString();



                //Gets real time stock quote for each stock player owns
                var json = await ApiHub.GetJson(_url);

                var root = JObject.Parse(json);
                List <StockQuote> stocksApi = new List <StockQuote>();
                foreach (var r in root)
                {
                    var stock = JsonConvert.DeserializeObject <StockQuote>(r.Value["quote"].ToString());
                    stocksApi.Add(stock);
                }


                List <PortfolioStock> stocks = new List <PortfolioStock>();
                foreach (var pStocks in playerStocks)
                {
                    StockQuote stock          = stocksApi.Where(s => s.Symbol == pStocks.StockSymbol).FirstOrDefault();
                    var        portfolioStock = new PortfolioStock();
                    portfolioStock.PlayerStockId = pStocks.Id;
                    portfolioStock.Symbol        = stock.Symbol;
                    portfolioStock.Name          = stock.CompanyName;
                    portfolioStock.CurrentPrice  = stock.LatestPrice;
                    portfolioStock.Change        = (decimal)stock.Change;
                    portfolioStock.PercentChange = stock.ChangePercent;
                    portfolioStock.Shares        = pStocks.Shares;
                    portfolioStock.PurchasePrice = pStocks.PurchasePrice;
                    portfolioStock.PurchaseDate  = pStocks.CreatedDate;
                    stocks.Add(portfolioStock);
                }

                foreach (var stock in stocks)
                {
                    stockPortfolioViewModel.TotalStockValue += stock.MarketValue;
                    stockPortfolioViewModel.TotalStockCost  += stock.TotalCost;
                    stockPortfolioViewModel.TotalTodaysGain += stock.TodaysGain;
                }
                stockPortfolioViewModel.Stocks = stocks;
            }

            return(View("Portfolio", stockPortfolioViewModel));
        }
Beispiel #2
0
        public StockPortfolioViewModel GetPortfolioInfo()
        {
            //Get userID for the user who is currently logged in
            UserManager <AppUser> userManager = new UserManager <AppUser>(new UserStore <AppUser>(db));
            var user = userManager.FindById(User.Identity.GetUserId());

            StockPortfolioViewModel portfolioInfo = new StockPortfolioViewModel();

            //TODO: ** add logic to each value
            portfolioInfo.CurrentValue     = 0;
            portfolioInfo.TotalGains       = 0;
            portfolioInfo.TotalFees        = 0;
            portfolioInfo.TotalBonuses     = 0;
            portfolioInfo.CashAvailable    = 0;
            portfolioInfo.Balanced         = false;
            portfolioInfo.StockMarketValue = 0;

            AppUser currentUser = db.Users.Find(User.Identity.GetUserId());

            //find total of bonuses
            foreach (Transaction t in currentUser.StockPortfolio.Transactions)
            {
                if (t.TransType == TransType.Bonus)
                {
                    portfolioInfo.TotalBonuses += t.Amount;
                }
            }

            //Look at all of the fee transactions associated with the stock portfolio and add to the FEES PORTION
            //of the portfolio's value
            foreach (Transaction tr in user.StockPortfolio.Transactions)
            {
                if (tr.TransType == bevo.Models.TransType.Fee)
                {
                    portfolioInfo.TotalFees += tr.Amount;
                }
            }

            //Look at the type of transaction for each transaction on the stock portfolio and either addto or
            //subtract from the appropriate CASH AVAILABLE PORTION of the portfolio
            portfolioInfo.CashAvailable = user.StockPortfolio.Balance;
            //foreach(Transaction tr in user.StockPortfolio.Transactions)
            //{
            //    if(tr.TransType == bevo.Models.TransType.Deposit)
            //    {
            //        portfolioInfo.CashAvailable += tr.Amount;
            //    }
            //    else if(tr.TransType == bevo.Models.TransType.Purchase_Stock && GetAccountNumbers().Contains(tr.FromAccount))
            //    {
            //        portfolioInfo.CashAvailable -= tr.Amount;
            //    }
            //    else if(tr.TransType == bevo.Models.TransType.Sell_Stock && tr.FromAccount == user.StockPortfolio.AccountNum)
            //    {
            //        portfolioInfo.CashAvailable += tr.Amount;
            //    }
            //    else if(tr.TransType == bevo.Models.TransType.Withdrawal)
            //    {
            //        portfolioInfo.CashAvailable -= tr.Amount;
            //    }
            //    else if(tr.TransType == bevo.Models.TransType.Transfer && tr.ToAccount == user.StockPortfolio.AccountNum)
            //    {
            //        portfolioInfo.CashAvailable += tr.Amount;
            //    }
            //    else if(tr.TransType == bevo.Models.TransType.Transfer && tr.ToAccount != user.StockPortfolio.AccountNum)
            //    {
            //        portfolioInfo.CashAvailable -= tr.Amount;
            //    }
            //}

            //Get a list of all the stock tickers in the account
            //Also get the value of the STOCKMARKETVALUE PORTION of the portfolio
            List <String> tickersInAccount = new List <String>();

            foreach (Transaction t in user.StockPortfolio.Transactions)
            {
                if (t.TransType == TransType.Purchase_Stock)
                {
                    decimal stockVal = t.Amount;
                    portfolioInfo.StockMarketValue += stockVal;
                    tickersInAccount.Add(t.Stock.StockTicker);
                }
                else if (t.TransType == TransType.Sell_Stock)
                {
                    Decimal?stockMarketValueReduction = t.SMVRedux;
                    portfolioInfo.StockMarketValue -= stockMarketValueReduction;
                }
            }

            //Define how much is in the GAINS PORTION of the portfolio
            decimal currentMarketValue = new decimal();

            currentMarketValue = 0;
            foreach (StockViewModel s in PortfolioSnapshot())
            {
                currentMarketValue += (s.CurrentPrice * s.NumInAccount);
            }
            portfolioInfo.TotalGains = currentMarketValue - portfolioInfo.StockMarketValue;

            //Find the total value of the entire portfolio
            portfolioInfo.CurrentValue = portfolioInfo.CashAvailable + portfolioInfo.StockMarketValue - portfolioInfo.TotalFees
                                         + portfolioInfo.TotalBonuses + portfolioInfo.TotalGains;


            return(portfolioInfo);
        }