Beispiel #1
0
class StockBroker:
    def __init__(self, initialCashDeposit, stockRepository):
        self.__portfolio = Portfolio(initialCashDeposit, stockRepository)
        self.stockRepository = stockRepository

    def MarketBuy(self, stockTickerSymbol, quantity):
        self.__portfolio.Buy(stockTickerSymbol, quantity)

    def MarketSell(self, stockTickerSymbol, quantity):
        self.__portfolio.Sell(stockTickerSymbol, quantity)

    # Set order to buy a stock if it hits a certain price.
    def LimitBuy(self, stockTickerSymbol, quantity, buyLimit):
        return 'Not yet implemented'

    # Set order to sell a stock if it hits a certain price.
    def LimitSell(self, stockTickerSymbol, quantity, sellLimit):
        return 'Not yet implemented'

    # Set order to buy a stock at market price, then sell if it hits the
    #   sellPrice
    def StopLoss(self, stockTickerSymbol, quantity, sellPrice):
        return 'Not yet implemented'

    def GetPortfolio(self):
        return self.__portfolio
 def test_SellAllStocks_SellOrderLessThanHeldQuantity(self):
     # Arrange
     mockStockRepository = BackTestStockRepository()
     mockStockRepository.GetCurrentStockPrice = MagicMock(return_value = 300)
     startingCash = 1000
     portfolio = Portfolio(startingCash, mockStockRepository)
     portfolio.heldStocks['AAPL'] = 10
     numberOfStocksToSell = 5
     expectedStockHeldQuantity = 5
     expectedPortfolioValue = 4000
     # Act
     portfolio.Sell('AAPL', numberOfStocksToSell)
     # Assert
     actualValue = portfolio.GetTotalValue()
     self.assertEqual(expectedPortfolioValue, actualValue)
     self.assertEqual(expectedStockHeldQuantity, portfolio.heldStocks['AAPL'])