/// <summary> /// Buy stocks /// </summary> /// <param name="companyName"></param> /// <param name="companySymbol"></param> /// <param name="currentSharePrice"></param> /// <param name="numOfShares"></param> /// <param name="time"></param> /// <returns></returns> public bool BuyStock(string companyName, string companySymbol, double currentSharePrice, int numOfShares, DateTime time) { if (!stock.IsOpenStockMarket() && EnforceMarketClosure) { MessageBox.Show("The market is closed."); return(false); } if (numOfShares * currentSharePrice > Cash) { MessageBox.Show("You don't have enough money for that purchase. Dummy."); return(false); } double transactionAmount = (numOfShares * currentSharePrice); // create a new holding if one doesn't already exist Holding holding = GetHolding(companySymbol); if (holding == null) { holding = new Holding(companySymbol); Holdings.Add(holding); holding.companyName = companyName; holding.currentSharePrice = currentSharePrice; } holding.numOfShares += numOfShares; holding.totalInvested += (transactionAmount); holding.worth += (transactionAmount); // Add to list of transactions Transactions.Add(new Transaction(companyName, companySymbol, currentSharePrice, numOfShares, time)); MessageBox.Show("You just purchased " + numOfShares + " shares of " + companyName + " stock at" + currentSharePrice.ToString("C2") + " per share."); Cash = Cash - transactionAmount; Cash = Cash - transactionFee; refresh(); return(true); }
/// <summary> /// Sell stocks /// </summary> /// <param name="companyName"></param> /// <param name="companySymbol"></param> /// <param name="currentSharePrice"></param> /// <param name="numOfShares"></param> /// <param name="time"></param> /// <returns></returns> public bool SellStock(string companyName, string companySymbol, double currentSharePrice, int numOfShares, DateTime time) { Holding holding = GetHolding(companySymbol); if (!stock.IsOpenStockMarket() && EnforceMarketClosure) { MessageBox.Show("The market is closed."); return(false); } if (holding == null) { return(false); } if (numOfShares > holding.numOfShares) { MessageBox.Show("You cannot sell more shares than you own, dummy..."); return(false); } // Sale amount is subtracted from worth because worth is represents the overall worth of the stocks. double saleAmount = (numOfShares * currentSharePrice); holding.worth = holding.worth - saleAmount; Cash = Cash + saleAmount; Cash = Cash - transactionFee; // The effective price per share is the total amount of money invested divided by the number of shares being held. double effectivePricePerShare = (holding.totalInvested / holding.numOfShares); holding.totalInvested = holding.totalInvested - (effectivePricePerShare * numOfShares); holding.numOfShares = holding.numOfShares - numOfShares; refresh(); MessageBox.Show("You sold " + numOfShares.ToString() + " share(s) of " + companyName + " at " + currentSharePrice.ToString("C2") + " a share. Total sale amount: " + saleAmount.ToString("C2")); return(true); }
/// <summary> /// Buy stocks /// </summary> /// <param name="companyName"></param> /// <param name="companySymbol"></param> /// <param name="currentSharePrice"></param> /// <param name="numOfShares"></param> /// <param name="time"></param> /// <returns></returns> public bool BuyStock(string companyName, string companySymbol, double currentSharePrice, int numOfShares, DateTime time) { if (!stock.IsOpenStockMarket() && EnforceMarketClosure) { MessageBox.Show("The market is closed."); return false; } if (numOfShares * currentSharePrice > Cash) { MessageBox.Show("You don't have enough money for that purchase. Dummy."); return false; } double transactionAmount = (numOfShares * currentSharePrice); // create a new holding if one doesn't already exist Holding holding = GetHolding(companySymbol); if (holding == null) { holding = new Holding(companySymbol); Holdings.Add(holding); holding.companyName = companyName; holding.currentSharePrice = currentSharePrice; } holding.numOfShares += numOfShares; holding.totalInvested += (transactionAmount); holding.worth += (transactionAmount); // Add to list of transactions Transactions.Add(new Transaction(companyName, companySymbol, currentSharePrice, numOfShares, time)); MessageBox.Show("You just purchased " + numOfShares + " shares of " + companyName + " stock at" + currentSharePrice.ToString("C2") + " per share."); Cash = Cash - transactionAmount; Cash = Cash - transactionFee; refresh(); return true; }
private void AddTestDataToHoldings() { Holding holding = new Holding("AAPL"); holding.numOfShares = 5; holding.totalInvested = 80.00; Holdings.Add(holding); holding = new Holding("TSLA"); holding.numOfShares = 10; holding.totalInvested = 10000.00; Holdings.Add(holding); }