public void SellPostion_SellMoreThanWhatIsOwned_Test() { const StockId StockOwned = StockId.BONDS; const int ExpectedCashValue = 5000; const int ShareValue = 1000; const int NumberOfStocksOwned = 5; const int NumberOfStocksToSell = 6; const string PlayerName = "test"; IDictionary <StockId, IStockPosition> mockPortfolio = new Dictionary <StockId, IStockPosition>(); Mock <IStockPosition> mockStockPosition = new Mock <IStockPosition>(); mockStockPosition.Setup(x => x.GetShareValue()).Returns(ShareValue); mockStockPosition.SetupGet(x => x.NumberOwned).Returns(NumberOfStocksOwned); mockPortfolio[StockOwned] = mockStockPosition.Object; Player player = new Player(PlayerName, mockPortfolio); try { player.SellPosition(StockOwned, NumberOfStocksToSell); Assert.Fail(); } catch (ArgumentException) { Assert.AreEqual(ExpectedCashValue, player.GetCashValue()); } }
public void NotifyObservers(StockId stockId, int dividendAmount) { foreach (IObserver observer in _observers) { observer.Update(stockId, dividendAmount); } }
public IStockTO GetStockById(StockId stockId) { var stockTo = new StockTO(); stockTo = (StockTO)_stocks[stockId].GetStockData(); return(stockTo); }
public async Task GetCompanyInformation() { StockId id = "sh.600519"; var result = await TuShare.GetCompanyInformation(id); Assert.AreEqual(result.Id.ToString("b"), id.ToString("b")); }
public void BuyPosition_ValueLessThanCash_Test() { const StockId StockToBuy = StockId.BONDS; const int TestShareValue = 4000; const int EndingCashValue = 1000; const int NumberOfStocksToBuy = 1; const string PlayerName = "test"; IDictionary <StockId, IStockPosition> mockPortfolio = new Dictionary <StockId, IStockPosition>(); Mock <IStockPosition> mockStockPosition = new Mock <IStockPosition>(); mockStockPosition.Setup(x => x.GetShareValue()).Returns(TestShareValue); mockPortfolio[StockToBuy] = mockStockPosition.Object; Player player = new Player(PlayerName, mockPortfolio); try { player.BuyPosition(StockToBuy, NumberOfStocksToBuy); } catch (ArgumentException) { Assert.Fail(); } Assert.AreEqual(player.GetCashValue(), EndingCashValue); }
/// <summary> /// </summary> /// <typeparam name="T"></typeparam> /// <param name="api"></param> /// <param name="args"></param> /// <returns></returns> public T Fetch <T>(Api api, params object[] args) { string[] arguments = args.Select( argument => argument switch { DateTime arg => arg.ToString("yyyy-MM-dd"), StockId arg => arg.ToString("b"), null => null, _ => argument.ToString() }
public override int GetHashCode() { unchecked { var hashCode = StockId.GetHashCode(); hashCode = (hashCode * 397) ^ BuyOrderId.GetHashCode(); hashCode = (hashCode * 397) ^ SellOrderId.GetHashCode(); return(hashCode); } }
public override int GetHashCode() { unchecked { var hashCode = StockId.GetHashCode(); hashCode = (hashCode * 397) ^ BidPrice.GetHashCode(); hashCode = (hashCode * 397) ^ BidQuantity.GetHashCode(); hashCode = (hashCode * 397) ^ OrderId.GetHashCode(); return(hashCode); } }
public override int GetHashCode() { int hash = 1; if (StockId.Length != 0) { hash ^= StockId.GetHashCode(); } hash ^= priceUpdates_.GetHashCode(); hash ^= volumeUpdates_.GetHashCode(); return(hash); }
public void SellPosition(StockId stockId, int numberOfStocks) { if (numberOfStocks <= _portfolio[stockId].NumberOwned) { int value = _portfolio[stockId].GetShareValue() * numberOfStocks; _cash += value; _portfolio[stockId].NumberOwned -= numberOfStocks; } else { throw new ArgumentException("Not enough stocks to sell"); } }
public override int GetHashCode() { unchecked { var hashCode = OrderId.GetHashCode(); hashCode = (hashCode * 397) ^ StockId.GetHashCode(); hashCode = (hashCode * 397) ^ (int)Side; hashCode = (hashCode * 397) ^ OriginalQuantity.GetHashCode(); hashCode = (hashCode * 397) ^ Price.GetHashCode(); hashCode = (hashCode * 397) ^ Fills.GetHashCode(); return(hashCode); } }
public void BuyPosition(StockId stockId, int numberOfStocks) { var stockToPurchase = _portfolio[stockId]; int cost = stockToPurchase.GetShareValue() * numberOfStocks; if (cost <= _cash) { _cash -= cost; _portfolio[stockId].NumberOwned += numberOfStocks; } else { throw new ArgumentException("Not enough money"); } }
public virtual void MarketMovement() { const int FIVE = 1; const int TEN = 2; const int TWENTY = 3; const int DOWN = 1; const int DIVIDENDS = 2; const int UP = 3; const int NUMBER_OF_STOCKS_LIMIT = 7; Random rnd = new Random(); int action = rnd.Next(1, 4); int selectedStock = rnd.Next(1, NUMBER_OF_STOCKS_LIMIT); int amount = 0; switch (rnd.Next(1, 4)) { case FIVE: amount = 5; break; case TEN: amount = 10; break; case TWENTY: amount = 20; break; } StockId id = (StockId)selectedStock; if (action == UP) { _stocks[id].Value += amount; } else if (action == DIVIDENDS && _stocks[id].Value >= DIVIDEND_MIN_VALUE) { NotifyObservers(id, amount); } else if (action == DOWN) { _stocks[id].Value -= amount; } }
public override int GetHashCode() { int hash = 1; if (StockId.Length != 0) { hash ^= StockId.GetHashCode(); } if (CurrentAvgVolume != 0D) { hash ^= CurrentAvgVolume.GetHashCode(); } if (TimeIssued != 0L) { hash ^= TimeIssued.GetHashCode(); } return(hash); }
public int CompareTo(object?obj) { if (obj is StockHistoryEntity other) { if (TranDate.ToDate() == other.TranDate.ToDate() && StockId == other.StockId) { return(0); } if (TranDate.ToDate() > other.TranDate.ToDate()) { return(1); } if (TranDate.ToDate() < other.TranDate.ToDate()) { return(-1); } return(StockId.CompareTo(other.StockId)); } return(0); }
public void PlayerCollectsCorrectDividendsWhenThreeDividendStocksOwned() { const int FINAL_CASH = 2600; const int FINAL_NET_WORTH = 5600; const int DIVIDEND_AMOUNT = 20; const int NUM_POSITIONS_TO_BUY = 3; StockId purchaseStock = StockId.BONDS; string playerName = "Cool Guy"; Player player = new Player(playerName, _portfolio); try { player.BuyPosition(purchaseStock, NUM_POSITIONS_TO_BUY); } catch (ArgumentException) { Assert.Fail(); } player.CollectDividends(purchaseStock, DIVIDEND_AMOUNT); Assert.AreEqual(player.GetCashValue(), FINAL_CASH); Assert.AreEqual(player.GetNetWorth(), FINAL_NET_WORTH); }
private void IncrementStock(StockId stockId, int incrementValue) { _stocks[stockId].Value += incrementValue; }
public Stock(StockId id) { _id = id; _value = START_VALUE; }
/// <summary> /// Observer pattern update method. This is to update the /// dividends that the player collects when the stock market /// pays dividends /// </summary> /// <param name="stockId">Id of stock paying dividends</param> /// <param name="dividendAmount">number of dividends paid</param> public void Update(StockId stockId, int dividendAmount) { CollectDividends(stockId, dividendAmount); }
public void CollectDividends(StockId stockId, int dividendAmount) { int payout = _portfolio[stockId].GetDividenPayout(dividendAmount); _cash += payout; }
public IStockTO GetStock(StockId stockId) { return(_stockMarket.GetStockById(stockId)); }
/// <summary> /// This method sells the given number of given stocks for the given /// player /// </summary> /// <param name="playerName">player selling stocks</param> /// <param name="stockId">stock to sell</param> /// <param name="numberOfShares">number of stocks to sell</param> public void SellStock(string playerName, StockId stockId, int numberOfShares) { Player selectedPlayer = _players.Find(r => r.GetName() == playerName); selectedPlayer.SellPosition(stockId, numberOfShares); }