Skip to content

Commit

Permalink
[OrderBookManager] Add pair [price, size] conversion to order format
Browse files Browse the repository at this point in the history
  • Loading branch information
Herklos committed Jul 5, 2020
1 parent c4fa0f6 commit 239f5e6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
3 changes: 2 additions & 1 deletion octobot_trading/data_manager/order_book_manager.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ cdef class OrderBookManager(Initializable):
cdef void _remove_bids(self, double price)

cdef int _order_id_index(str order_id, list order_list)

cdef list _convert_price_size_list_to_order(list price_size_list, str side)
cdef dict _convert_price_size_to_order(list price_size, str side)
32 changes: 30 additions & 2 deletions octobot_trading/data_manager/order_book_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ def handle_new_book(self, orders):

def handle_new_books(self, asks, bids, timestamp=None):
self.reset_order_book()
self.asks.update(asks)
self.bids.update(bids)
self.handle_book_adds(_convert_price_size_list_to_order(asks, TradeOrderSide.SELL.value))
self.handle_book_adds(_convert_price_size_list_to_order(bids, TradeOrderSide.BUY.value))
if timestamp:
self.timestamp = timestamp
self.order_book_initialized = True
Expand Down Expand Up @@ -192,3 +192,31 @@ def _order_id_index(order_id, order_list):
if order[ECOBIC.ORDER_ID.value] == order_id:
return index
return ORDER_ID_NOT_FOUND


def _convert_price_size_list_to_order(price_size_list, side):
"""
Convert a [price, size] list to the book order format
:param price_size_list: the list of [price, size]
:param side: the order side
:return: the converted list
"""
return [
_convert_price_size_to_order(price_size, side)
for price_size in price_size_list
]


def _convert_price_size_to_order(price_size, side):
"""
Convert a pair of price and size to an order book dict format
:param price_size: the pair, size pair to convert
:param side: the order side
:return: the converted pair dict
"""
return {
ECOBIC.SIDE.value: side,
ECOBIC.PRICE.value: price_size[0],
ECOBIC.SIZE.value: price_size[1],
ECOBIC.ORDER_ID.value: None
}
4 changes: 2 additions & 2 deletions tests/util/random_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def random_funding_rate(min_value=0):


def random_order_book_side(min_value=0, count=2):
return zip(random_prices(min_value=min_value, count=count),
random_quantities(min_value=min_value, count=count))
return [list(pair) for pair in zip(random_prices(min_value=min_value, count=count),
random_quantities(min_value=min_value, count=count))]


def random_candle_tuple():
Expand Down

0 comments on commit 239f5e6

Please sign in to comment.