private Error SellStocks(StockEvent e) { Stock_M s = _mainModel.Stocks.Find((stock) => stock.Tag == e.Name); if (s != null) { BuyOrSell_M bos = _currentPortfolio.CurrentlyHeld().Find(b => b.StockName == s.Name); if (e.Amount > bos.Quantity) { return(new Error("Selling too many stocks")); } _currentPortfolio.SellStock(s, e.Amount); } Broadcast(new PortfolioEvent("portfolio", _currentPortfolio.Name)); return(Error.None); }
/// <summary> /// Sells the amount given of a stock held in the portfolio and sends the cash back to the account funds /// </summary> /// <param name="stock"></param> /// <param name="amount"></param> public void SellStock(Stock_M stock, int amount) { if (_verifierStock(stock)) { if (_fundsManager(BuyOrSell_M.BuyOrSellEnum.Sell, (stock.Price * amount), Fee_M.BUY_OR_SELL)) { BuyOrSell_M buyOrSell = new BuyOrSell_M(stock, amount, BuyOrSell_M.BuyOrSellEnum.Sell); _transactionList.Add(buyOrSell); Fee_M fee = new Fee_M(Fee_M.FeeSelect.BuyOrSell); _transactionList.Add(fee); } else { throw new ArgumentException("Not enough cash"); } } }
/// <summary> /// Returns the List of stocks held in the portfolio /// </summary> /// <returns></returns> public List <BuyOrSell_M> CurrentlyHeld() { List <BuyOrSell_M> HeldStockList = new List <BuyOrSell_M>(); //Dictionary<BuyOrSell, int> amounts foreach (Transaction curTrans in _transactionList) { if (curTrans.GetType() == typeof(BuyOrSell_M)) { BuyOrSell_M curBOS = (BuyOrSell_M)curTrans; BuyOrSell_M found = HeldStockList.Find(bos => curBOS.StockName == bos.StockName); if (found == null) { HeldStockList.Add(curBOS); } else { if (curBOS.BuyOrSellState == BuyOrSell_M.BuyOrSellEnum.Buy) { HeldStockList[HeldStockList.IndexOf(found)] = new BuyOrSell_M(curBOS.StockName, curBOS.Quantity + found.Quantity, curBOS.PricePerStock, BuyOrSell_M.BuyOrSellEnum.Buy); } else { if (curBOS.Quantity - found.Quantity != 0) { HeldStockList[HeldStockList.IndexOf(found)] = new BuyOrSell_M(curBOS.StockName, found.Quantity - curBOS.Quantity, curBOS.PricePerStock, BuyOrSell_M.BuyOrSellEnum.Buy); } else { HeldStockList.Remove(found); } } } } } return(HeldStockList); }