diff --git a/limit_order_book/test/test_limit_order_book.py b/limit_order_book/test/test_limit_order_book.py index c66b76a..649aa17 100644 --- a/limit_order_book/test/test_limit_order_book.py +++ b/limit_order_book/test/test_limit_order_book.py @@ -22,6 +22,9 @@ def test(self): self.assertEqual(0, book.size_buy()) self.assertEqual(0, book.size()) +# +# MARK: limit +# class ShouldPlaceSellLimitOrder(TestCase): def test(self): @@ -42,27 +45,23 @@ def test(self): self.assertEqual(1, book.size()) -class ShouldCancelSellLimitOrder(TestCase): +class ShouldPlaceSellLimitOrderByValue(TestCase): def test(self): book = limit_order_book.LimitOrderBook() - book.limit_sell(1, 100, 50) - self.assertTrue(book.has(1)) - book.cancel(1) - self.assertFalse(book.has(1)) - self.assertEqual(0, book.best_sell()) + book.limit(False, 1, 100, 50) + self.assertEqual(50, book.best_sell()) self.assertEqual(0, book.best_buy()) - self.assertEqual(0, book.best(False)) + self.assertEqual(50, book.best(False)) self.assertEqual(0, book.best(True)) - self.assertEqual(0, book.volume_sell()) - self.assertEqual(0, book.volume_sell(100)) + self.assertEqual(100, book.volume_sell()) + self.assertEqual(100, book.volume_sell(50)) self.assertEqual(0, book.volume_buy()) - self.assertEqual(0, book.volume_buy(100)) - self.assertEqual(0, book.volume()) - self.assertEqual(0, book.volume(100)) - self.assertEqual(0, book.size_at(100)) - self.assertEqual(0, book.size_sell()) + self.assertEqual(100, book.volume()) + self.assertEqual(100, book.volume(50)) + self.assertEqual(1, book.size_at(50)) + self.assertEqual(1, book.size_sell()) self.assertEqual(0, book.size_buy()) - self.assertEqual(0, book.size()) + self.assertEqual(1, book.size()) class ShouldPlaceBuyLimitOrder(TestCase): @@ -85,6 +84,97 @@ def test(self): self.assertEqual(1, book.size()) +class ShouldPlaceBuyLimitOrderByValue(TestCase): + def test(self): + book = limit_order_book.LimitOrderBook() + book.limit(True, 1, 100, 50) + self.assertEqual(0, book.best_sell()) + self.assertEqual(50, book.best_buy()) + self.assertEqual(0, book.best(False)) + self.assertEqual(50, book.best(True)) + self.assertEqual(0, book.volume_sell()) + self.assertEqual(0, book.volume_sell(50)) + self.assertEqual(100, book.volume_buy()) + self.assertEqual(100, book.volume_buy(50)) + self.assertEqual(100, book.volume()) + self.assertEqual(100, book.volume(50)) + self.assertEqual(1, book.size_at(50)) + self.assertEqual(0, book.size_sell()) + self.assertEqual(1, book.size_buy()) + self.assertEqual(1, book.size()) + +# +# MARK: limit match +# + +class ShouldMatchSellLimitOrderWithIncomingBuy(TestCase): + def test(self): + book = limit_order_book.LimitOrderBook() + book.limit_sell(1, 100, 50) + book.limit_buy(2, 100, 50) + self.assertEqual(0, book.best_sell()) + self.assertEqual(0, book.best_buy()) + self.assertEqual(0, book.best(False)) + self.assertEqual(0, book.best(True)) + self.assertEqual(0, book.volume_sell()) + self.assertEqual(0, book.volume_sell(50)) + self.assertEqual(0, book.volume_buy()) + self.assertEqual(0, book.volume_buy(50)) + self.assertEqual(0, book.volume()) + self.assertEqual(0, book.volume(50)) + self.assertEqual(0, book.size_at(50)) + self.assertEqual(0, book.size_sell()) + self.assertEqual(0, book.size_buy()) + self.assertEqual(0, book.size()) + + +class ShouldMatchBuyLimitOrderWithIncomingSell(TestCase): + def test(self): + book = limit_order_book.LimitOrderBook() + book.limit_buy(1, 100, 50) + book.limit_sell(2, 100, 50) + self.assertEqual(0, book.best_sell()) + self.assertEqual(0, book.best_buy()) + self.assertEqual(0, book.best(False)) + self.assertEqual(0, book.best(True)) + self.assertEqual(0, book.volume_sell()) + self.assertEqual(0, book.volume_sell(50)) + self.assertEqual(0, book.volume_buy()) + self.assertEqual(0, book.volume_buy(50)) + self.assertEqual(0, book.volume()) + self.assertEqual(0, book.volume(50)) + self.assertEqual(0, book.size_at(50)) + self.assertEqual(0, book.size_sell()) + self.assertEqual(0, book.size_buy()) + self.assertEqual(0, book.size()) + +# +# MARK: cancel +# + +class ShouldCancelSellLimitOrder(TestCase): + def test(self): + book = limit_order_book.LimitOrderBook() + book.limit_sell(1, 100, 50) + self.assertTrue(book.has(1)) + book.cancel(1) + self.assertFalse(book.has(1)) + self.assertEqual(0, book.best_sell()) + self.assertEqual(0, book.best_buy()) + self.assertEqual(0, book.best(False)) + self.assertEqual(0, book.best(True)) + self.assertEqual(0, book.volume_sell()) + self.assertEqual(0, book.volume_sell(100)) + self.assertEqual(0, book.volume_buy()) + self.assertEqual(0, book.volume_buy(100)) + self.assertEqual(0, book.volume()) + self.assertEqual(0, book.volume(100)) + self.assertEqual(0, book.size_at(100)) + self.assertEqual(0, book.size_sell()) + self.assertEqual(0, book.size_buy()) + self.assertEqual(0, book.size()) + + class ShouldCancelBuyLimitOrder(TestCase): def test(self): book = limit_order_book.LimitOrderBook() @@ -107,6 +197,9 @@ def test(self): self.assertEqual(0, book.size_buy()) self.assertEqual(0, book.size()) +# +# MARK: market +# class ShouldPlaceSellMarketOrderEmptyBook(TestCase): def test(self): @@ -189,6 +282,9 @@ def test(self): self.assertEqual(0, book.size_buy()) self.assertEqual(1, book.size()) +# +# MARK: clear +# class ShouldClearSellLimitOrders(TestCase): def test(self): @@ -217,3 +313,32 @@ def test(self): self.assertEqual(0, book.size_sell()) self.assertEqual(0, book.size_buy()) self.assertEqual(0, book.size()) + + +class ShouldClearBuyLimitOrders(TestCase): + def test(self): + book = limit_order_book.LimitOrderBook() + book.limit_sell(1, 100, 50) + book.limit_sell(2, 100, 50) + book.limit_sell(3, 100, 50) + self.assertTrue(book.has(1)) + self.assertTrue(book.has(2)) + self.assertTrue(book.has(3)) + book.clear() + self.assertFalse(book.has(1)) + self.assertFalse(book.has(2)) + self.assertFalse(book.has(3)) + self.assertEqual(0, book.best_sell()) + self.assertEqual(0, book.best_buy()) + self.assertEqual(0, book.best(False)) + self.assertEqual(0, book.best(True)) + self.assertEqual(0, book.volume_sell()) + self.assertEqual(0, book.volume_sell(100)) + self.assertEqual(0, book.volume_buy()) + self.assertEqual(0, book.volume_buy(100)) + self.assertEqual(0, book.volume()) + self.assertEqual(0, book.volume(100)) + self.assertEqual(0, book.size_at(100)) + self.assertEqual(0, book.size_sell()) + self.assertEqual(0, book.size_buy()) + self.assertEqual(0, book.size())