Ejemplo n.º 1
0
    public void AddStockToPortfolio(string inPortfolioID, string inStockName, int numberOfShares)
    {
        Portfolio portfolio        = this.getPortfolioFromStockExchange(inPortfolioID);
        Stock     stock            = getStockFromStockExchange(inStockName);
        int       stocksSharesSold = 0;

        if (this.soldStocks.ContainsKey(inStockName))
        {
            stocksSharesSold = this.soldStocks [inStockName];
        }
        else
        {
            this.soldStocks.Add(inStockName, stocksSharesSold);
        }

        if (stocksSharesSold + numberOfShares > Convert.ToInt(stock.getStockQuantity()))
        {
            throw new StockExchangeException("Not enough stocks");
        }

        portfolio.AddStockToPortfolio(stock, numberOfShares);
        this.soldStocks [stock] += numberOfShares;
    }
Ejemplo n.º 2
0
        // Application starting point
        public async Task Run()
        {
            string todaysStockDate  = DateTime.Now.ToString("yyyy-MM-dd");
            var    currentPortfolio = new Portfolio();

            var applePrice  = (await _stockApiService.GetStockPrice("AAPL")).TimeSeriesDaily[todaysStockDate].The4Close;
            var googlePrice = (await _stockApiService.GetStockPrice("GOOG")).TimeSeriesDaily[todaysStockDate].The4Close;
            var cybrPrice   = (await _stockApiService.GetStockPrice("CYBR")).TimeSeriesDaily[todaysStockDate].The4Close;
            var abbPrice    = (await _stockApiService.GetStockPrice("ABB")).TimeSeriesDaily[todaysStockDate].The4Close;

            var gfnPrice = (await _stockApiService.GetStockPrice("GFN")).TimeSeriesDaily[todaysStockDate].The4Close;

            //Sleeping here because apparently only 5 calls are allowed per minute
            System.Threading.Thread.Sleep(61000);
            var acadPrice = (await _stockApiService.GetStockPrice("ACAD")).TimeSeriesDaily[todaysStockDate].The4Close;

            currentPortfolio.AddStockToPortfolio("AAPL", applePrice, 50);
            currentPortfolio.AddStockToPortfolio("GOOG", googlePrice, 200);
            currentPortfolio.AddStockToPortfolio("CYBR", cybrPrice, 150);
            currentPortfolio.AddStockToPortfolio("ABB", abbPrice, 900);

            var desiredAppleStockTotal  = (int)currentPortfolio.TotalPortfolioValue * (decimal)0.22;
            var desiredGoogleStockTotal = (int)currentPortfolio.TotalPortfolioValue * (decimal)0.38;
            var desiredGfnStockTotal    = (int)currentPortfolio.TotalPortfolioValue * (decimal)0.25;
            var desiredAcadStockTotal   = (int)currentPortfolio.TotalPortfolioValue * (decimal)0.15;

            var numberOfSharesToBuyApple  = (int)(desiredAppleStockTotal / applePrice);
            var numberOfSharesToBuyGoogle = (int)(desiredGoogleStockTotal / googlePrice);
            var numberOfSharesToBuyGfn    = (int)(desiredGfnStockTotal / gfnPrice);
            var numberOfSharesToBuyAcad   = (int)(desiredAcadStockTotal / acadPrice);

            var newAllocationApple  = (int)((desiredAppleStockTotal / currentPortfolio.TotalPortfolioValue) * 100);
            var newAllocationGoogle = (int)((desiredGoogleStockTotal / currentPortfolio.TotalPortfolioValue) * 100);
            var newAllocationGfn    = (int)((desiredGfnStockTotal / currentPortfolio.TotalPortfolioValue) * 100);
            var newAllocationAcad   = (int)((desiredAcadStockTotal / currentPortfolio.TotalPortfolioValue) * 100);

            string output = $@"
            Today's Prices:
            AAPL: {applePrice}
            GOOG: {googlePrice}
            CYBR: {cybrPrice}
            ABB: {abbPrice}
            GFN: {gfnPrice}
            ACAD: {acadPrice}

            Current Total Portfolio Value: {currentPortfolio.TotalPortfolioValue}

            Desired Dollar amounts per stock:
            Apple: {desiredAppleStockTotal}
            Google: {desiredGoogleStockTotal}
            GFN: {desiredGfnStockTotal}
            ACAD: {desiredAcadStockTotal}

            Number of shares to buy for each stock:
            Apple: {numberOfSharesToBuyApple}
            Google: {numberOfSharesToBuyGoogle}
            GFN: {numberOfSharesToBuyGfn}
            ACAD: {numberOfSharesToBuyAcad}

            Portfolio allocation per stock:
            Apple: {newAllocationApple}
            Google: {newAllocationGoogle}
            GFN: {newAllocationGfn}
            ACAD: {newAllocationAcad}
            ";


            Console.WriteLine(output);
        }