def test_price_no_recent_trades(self): stock = Stock(self.symbol_1, StockType.COMMON, self.par_value_1, self.last_dividend_1, self.fixed_dividend_0) trade = Trade(self.symbol_1, StockType.COMMON, self.timestamp_now - timedelta(minutes=20), self.quantity_1, self.price_per_share_1, BuySellIndicator.SELL) stock.record_trade(trade) self.assertEqual(stock.price(self.timestamp_now), None)
def test_price(self): exchange = Exchange('Test Exchange') stock = Stock('TEST', 'common', 100) exchange.add_stock(stock) self.assertIsNone(stock.price()) stock.record_trade(10, 'buy', 2) stock.record_trade(20, 'sell', 2) stock.record_trade(30, 'buy', 3) self.assertEqual(stock.price(), Decimal('2.5')) new_trades = [] for trade in exchange.trades[stock.symbol]: six_minutes_ago = datetime.utcnow() - timedelta(minutes=6) new_trades.append( Trade( date_time=six_minutes_ago, symbol=trade.symbol, quantity=trade.quantity, buy_or_sell=trade.buy_or_sell, price=trade.price, )) exchange.trades[stock.symbol] = new_trades self.assertEqual(stock.price(), 3)
def test_price_use_only_recent_trades(self): stock = Stock(self.symbol_1, StockType.COMMON, self.par_value_1, self.last_dividend_1, self.fixed_dividend_0) 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.COMMON, self.timestamp_now - timedelta(minutes=10), self.quantity_1, self.price_per_share_2, BuySellIndicator.SELL) trade_3 = Trade(self.symbol_1, StockType.COMMON, self.timestamp_now - timedelta(minutes=30), self.quantity_2, self.price_per_share_2, BuySellIndicator.SELL) stock.record_trade(trade_1) stock.record_trade(trade_2) stock.record_trade(trade_3) self.assertEqual(stock.price(), 175)