/// <summary> /// Updates holdings for filled orders. /// </summary> private void updateHoldings(Order order) { //Only process filled orders if (order.Status != OrderStatus.Filled) { return; } //OrderHistory.Add(order); TotalTrades++; //Transaction fees TotalTransactionFees += TransactionFee; AvailableCash -= TransactionFee; decimal orderTotal = order.Quantity * order.FillPrice; /////////////////////////////// //Buying ////////////////////////////// switch (order.Action) { //Buying case Action.Buy: AvailableCash -= orderTotal; //Find existing stock in holdings if (IsHoldingStock(order.Symbol)) { Stock stock = StockPortfolio.Find(p => p.Symbol == order.Symbol); stock.Quantity += order.Quantity; stock.TotalInvested += orderTotal; stock.CurrentPrice = order.FillPrice; } //Create a new stock object and add to holdings else { Stock newStock = new Stock() { Symbol = order.Symbol, Quantity = order.Quantity, TotalInvested = orderTotal, CurrentPrice = order.FillPrice }; StockPortfolio.Add(newStock); } break; /////////////////////// //Selling /////////////////////// case Action.Sell: AvailableCash += orderTotal; _totalSellTrades++; if (IsHoldingStock(order.Symbol)) { Stock stock = StockPortfolio.Find(p => p.Symbol == order.Symbol); //Is this a win or loss? if (orderTotal > (stock.AverageFillPrice * order.Quantity)) { _totalWins++; } else { _totalLosses++; } stock.TotalInvested -= stock.AverageFillPrice * order.Quantity; stock.Quantity -= order.Quantity; //remove the stock from the portfolio if no longer holding any. if (stock.Quantity <= 0) { StockPortfolio.Remove(stock); } } break; } }
public void GetCash_OneCredit_ReturnsCash() { _stockPortfolio.Add(DateTime.Today.AddDays(-1), 26d); var past = _stockPortfolio.GetCash(DateTime.Today.AddDays(-10)); var actual = _stockPortfolio.GetCash(DateTime.Today); var future = _stockPortfolio.GetCash(DateTime.Today.AddDays(10)); Assert.AreEqual(0d, past); Assert.AreEqual(26d, actual); Assert.AreEqual(26d, future); }