Skip to content

Commit

Permalink
add clear to python interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Kautenja committed Jan 25, 2020
1 parent 0e57c9e commit e2557f2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
11 changes: 9 additions & 2 deletions limit_order_book/limit_order_book.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
29 changes: 29 additions & 0 deletions limit_order_book/test/test_limit_order_book.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
5 changes: 5 additions & 0 deletions src/lib_lob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit e2557f2

Please sign in to comment.