def test_index_value(self): tea_stock = StockFactory.get_stock_by_ticker_symbol(TickerSymbol.TEA) gin_stock = StockFactory.get_stock_by_ticker_symbol(TickerSymbol.GIN) gbce = GlobalBeverageCorporationExchange([tea_stock, gin_stock]) tea_stock_trades = TradeFactory.get_trades_for_stock(TickerSymbol.TEA) gin_stock_trades = TradeFactory.get_trades_for_stock(TickerSymbol.GIN) for trade in tea_stock_trades + gin_stock_trades: gbce.record_trade(trade) last_tea_stock_trade = sorted(tea_stock_trades, key=lambda t: t.timestamp, reverse=True)[0] last_gin_stock_trade = sorted(gin_stock_trades, key=lambda t: t.timestamp, reverse=True)[0] current_time = max([last_tea_stock_trade.timestamp, last_gin_stock_trade.timestamp]) tea_stock_price = tea_stock.price(current_time) gin_stock_price = gin_stock.price(current_time) expected_value = (tea_stock_price * gin_stock_price)**(1/2) self.assertEqual(gbce.all_share_index(current_time), expected_value)
def test_index_value(self): tea_stock = StockFactory.get_stock_by_ticker_symbol(TickerSymbol.TEA) gin_stock = StockFactory.get_stock_by_ticker_symbol(TickerSymbol.GIN) gbce = GlobalBeverageCorporationExchange([tea_stock, gin_stock]) tea_stock_trades = TradeFactory.get_trades_for_stock(TickerSymbol.TEA) gin_stock_trades = TradeFactory.get_trades_for_stock(TickerSymbol.GIN) for trade in tea_stock_trades + gin_stock_trades: gbce.record_trade(trade) last_tea_stock_trade = sorted(tea_stock_trades, key=lambda t: t.timestamp, reverse=True)[0] last_gin_stock_trade = sorted(gin_stock_trades, key=lambda t: t.timestamp, reverse=True)[0] current_time = max( [last_tea_stock_trade.timestamp, last_gin_stock_trade.timestamp]) tea_stock_price = tea_stock.price(current_time) gin_stock_price = gin_stock.price(current_time) expected_value = (tea_stock_price * gin_stock_price)**(1 / 2) self.assertEqual(gbce.all_share_index(current_time), expected_value)
def test_add_stock(self): exchange = GlobalBeverageCorporationExchange(dict()) stock_1 = Stock(self.symbol_1, StockType.COMMON, self.par_value_1, self.last_dividend_1, self.fixed_dividend_0) stock_2 = Stock(self.symbol_1, StockType.PREFERRED, self.par_value_1, self.last_dividend_0, self.fixed_dividend_1) exchange.add_stock(stock_1) exchange.add_stock(stock_2) stocks = exchange.get_all_stocks() self.assertEqual(len(stocks), 2) self.assertEqual(stocks[stock_1.symbol_and_type()].symbol, stock_1.symbol) self.assertEqual(stocks[stock_1.symbol_and_type()].stock_type, stock_1.stock_type) self.assertEqual(stocks[stock_2.symbol_and_type()].symbol, stock_2.symbol) self.assertEqual(stocks[stock_2.symbol_and_type()].stock_type, stock_2.stock_type)
def test_record_trade(self): exchange = GlobalBeverageCorporationExchange(dict()) stock_1 = Stock(self.symbol_1, StockType.COMMON, self.par_value_1, self.last_dividend_1, self.fixed_dividend_0) stock_2 = Stock(self.symbol_1, StockType.PREFERRED, self.par_value_1, self.last_dividend_0, self.fixed_dividend_1) exchange.add_stock(stock_1) exchange.add_stock(stock_2) trade_1 = Trade(self.symbol_1, StockType.COMMON, self.timestamp_now, self.quantity_1, self.price_per_share_1, BuySellIndicator.SELL) trade_2 = Trade(self.symbol_1, StockType.PREFERRED, self.timestamp_now, self.quantity_2, self.price_per_share_2, BuySellIndicator.SELL) exchange.record_trade(trade_1) exchange.record_trade(trade_2) stocks = exchange.get_all_stocks() self.assertEqual(len(stocks[stock_1.symbol_and_type()].trades), 1) self.assertEqual(len(stocks[stock_2.symbol_and_type()].trades), 1)
def test_checks_empty_stocks(self): with self.assertRaises(ValueError): gbce = GlobalBeverageCorporationExchange([])
def test_not_enough_significant_trades_returns_none(self): stocks = StockFactory.get_stocks() gbce = GlobalBeverageCorporationExchange(stocks) index = gbce.all_share_index() self.assertIsNone(index)
def test_all_share_index(self): exchange = GlobalBeverageCorporationExchange(dict()) stock_1 = Stock(self.symbol_1, StockType.COMMON, self.par_value_1, self.last_dividend_1, self.fixed_dividend_0) stock_2 = Stock(self.symbol_1, StockType.PREFERRED, self.par_value_1, self.last_dividend_0, self.fixed_dividend_1) stock_3 = Stock(self.symbol_2, StockType.PREFERRED, self.par_value_1, self.last_dividend_0, self.fixed_dividend_1) exchange.add_stock(stock_1) exchange.add_stock(stock_2) exchange.add_stock(stock_3) trade_1 = Trade(self.symbol_1, StockType.COMMON, self.timestamp_now, self.quantity_1, 90, BuySellIndicator.SELL) trade_2 = Trade(self.symbol_1, StockType.PREFERRED, self.timestamp_now, self.quantity_2, 100, BuySellIndicator.SELL) trade_3 = Trade(self.symbol_2, StockType.PREFERRED, self.timestamp_now, self.quantity_2, 30, BuySellIndicator.SELL) exchange.record_trade(trade_1) exchange.record_trade(trade_2) exchange.record_trade(trade_3) self.assertEqual(int(exchange.all_share_index()), 64)