def test_08_all_share_index(self): exchange = sssm.Exchange() exchange.trades = [ sssm.Trade(stock=MMock(symbol='TEA'), quantity=16, action=sssm.TradeType.buy, price=100), sssm.Trade(stock=MMock(symbol='POP'), quantity=32, action=sssm.TradeType.buy, price=50), sssm.Trade(stock=MMock(symbol='POP'), quantity=64, action=sssm.TradeType.buy, price=100) ] self.assertAlmostEqual(91.2870929, exchange.all_share_index(duration=FIVE_MINUTES))
def test_04_record_multiple_trades(self): exchange = sssm.Exchange() trades = [MMock(), MMock(), MMock(), MMock(), MMock()] for trade in trades: exchange.record_trade(trade) # Test that trades are recorded self.assertEqual(5, len(exchange.trades)) # Test append order preserved self.assertEqual(trades[4], exchange.trades[-1]) self.assertEqual(trades[0], exchange.trades[0])
def test_03_volume_weighted_price(self): stock = MMock() trades = [ sssm.Trade(stock=stock, quantity=16, action=sssm.TradeType.buy, price=100), sssm.Trade(stock=stock, quantity=8, action=sssm.TradeType.buy, price=50) ] self.assertAlmostEqual(83.3333333, sssm.Trade.volume_weighted_price(trades))
def test_01_create_buy_trade(self): stock = MMock() trade = sssm.Trade(stock=stock, quantity=64, action=sssm.TradeType.buy, price=100) self.assertEqual(stock, trade.stock) self.assertEqual(64, trade.quantity) self.assertEqual(sssm.TradeType.buy, trade.action) self.assertEqual(100, trade.price) self.assertLessEqual((trade.timestamp - datetime.utcnow()).total_seconds(), 0.1)
def test_05_volume_weighted_price_by_stock(self): exchange = sssm.Exchange() stock = MMock(symbol='TEST') exchange.trades = [ sssm.Trade(stock=stock, quantity=16, action=sssm.TradeType.buy, price=100), sssm.Trade(stock=stock, quantity=32, action=sssm.TradeType.buy, price=50) ] self.assertAlmostEqual(66.666666666, exchange.price_by_stock('TEST', duration=5*60))
def test_07_volume_weighted_price_by_stock_duration(self): exchange = sssm.Exchange() stock = MMock(symbol='TEST') exchange.trades = [ sssm.Trade(stock=stock, quantity=16, action=sssm.TradeType.buy, price=100), sssm.Trade(stock=stock, quantity=32, action=sssm.TradeType.buy, price=50), sssm.Trade(stock=stock, quantity=64, action=sssm.TradeType.buy, price=100) ] # Move trade out of specified time interval exchange.trades[0].timestamp = (datetime.utcnow() - timedelta(seconds=FIVE_MINUTES+10)) self.assertAlmostEqual(83.3333333, exchange.price_by_stock('TEST', duration=FIVE_MINUTES))
def _get_params(self, method, email, domain): event = self.get_event('api-proxy') event['httpMethod'] = method event['requestContext']['domainName'] = domain event['requestContext']['authorizer']['claims']['email'] = email return event, MMock()
def test_02_record_trade(self): exchange = sssm.Exchange() trade = MMock() exchange.record_trade(trade) # Test that the last trade is appended to the end of list type class self.assertEqual(trade, exchange.trades[-1])
def test_02_create_sell_trade(self): stock = MMock() trade = sssm.Trade(stock=stock, quantity=32, action=sssm.TradeType.sell, price=100) self.assertEqual(sssm.TradeType.sell, trade.action)