diff --git a/limit_order_book/limit_order_book.py b/limit_order_book/limit_order_book.py index 208ec42..494c94a 100644 --- a/limit_order_book/limit_order_book.py +++ b/limit_order_book/limit_order_book.py @@ -27,12 +27,15 @@ class Types: Volume = ctypes.c_uint32 -# setup the argument and return types for Initialize +# setup the argument and return types for new_ LIB.new_.argtypes = None LIB.new_.restype = Types.Pointer -# setup the argument and return types for Delete +# setup the argument and return types for delete_ LIB.delete_.argtypes = [Types.Pointer] LIB.delete_.restype = None +# setup the argument and return types for clear +LIB.clear.argtypes = [Types.Pointer] +LIB.clear.restype = None # setup the argument and return types for limit_sell @@ -124,6 +127,10 @@ def __del__(self): """Delete this limit order book.""" LIB.delete_(self._book) + def clear(self): + """Clear all the orders in the book.""" + LIB.clear(self._book) + def limit_sell(self, order_id, size, price): """ Place a sell limit order with given size and price. diff --git a/limit_order_book/test/test_limit_order_book.py b/limit_order_book/test/test_limit_order_book.py index 42c1f59..c66b76a 100644 --- a/limit_order_book/test/test_limit_order_book.py +++ b/limit_order_book/test/test_limit_order_book.py @@ -188,3 +188,32 @@ def test(self): self.assertEqual(1, book.size_sell()) self.assertEqual(0, book.size_buy()) self.assertEqual(1, book.size()) + + +class ShouldClearSellLimitOrders(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()) diff --git a/src/lib_lob.cpp b/src/lib_lob.cpp index 3363bc3..6526d8c 100644 --- a/src/lib_lob.cpp +++ b/src/lib_lob.cpp @@ -34,6 +34,11 @@ extern "C" { delete book; } + /// Clear all the orders in the book. + EXP void clear(LimitOrderBook* book) { + book->clear(); + } + /// Add a new sell limit order to the book. EXP void limit_sell(LimitOrderBook* book, UID order_id, Size size, Price price) { book->limit_sell(order_id, size, price, 0);