Skip to content

Commit

Permalink
Merge pull request #455 from yakir4123/yakir/feat/delete-orders
Browse files Browse the repository at this point in the history
feat(FutureExchange/order store): delete canceled / executed orders to avoid O(N) and O(N^2) loops
  • Loading branch information
saleh-mir authored Jul 1, 2024
2 parents 37ffdfc + 0567661 commit e6126da
Show file tree
Hide file tree
Showing 7 changed files with 316 additions and 270 deletions.
2 changes: 1 addition & 1 deletion jesse/exchanges/sandbox/Sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def stop_order(self, symbol: str, qty: float, price: float, side: str, reduce_on

def cancel_all_orders(self, symbol: str) -> None:
orders = filter(lambda o: o.is_new,
store.orders.get_orders(self.name, symbol))
store.orders.get_active_orders(self.name, symbol))

for o in orders:
o.cancel()
Expand Down
1 change: 1 addition & 0 deletions jesse/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,7 @@ def today_to_timestamp() -> int:
return arrow.utcnow().floor('day').int_timestamp * 1000


@lru_cache
def type_to_side(t: str) -> str:
from jesse.enums import trade_types, sides

Expand Down
10 changes: 10 additions & 0 deletions jesse/libs/dynamic_numpy_array/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,13 @@ def append_multiple(self, items: np.ndarray) -> None:
self.array = np_shift(self.array, -shift_num)

self.array[self.index - len(items) + 1 : self.index + 1] = items

def delete(self, index: int, axis=None) -> None:
self.array = np.delete(self.array, index, axis=axis)
self.index -= 1
if self.index == -1:
new_bucket = np.zeros(self.shape)
self.array = np.concatenate((self.array, new_bucket), axis=0)



40 changes: 18 additions & 22 deletions jesse/models/FuturesExchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,17 @@ def on_order_execution(self, order: Order) -> None:
base_asset = jh.base_asset(order.symbol)

if not order.reduce_only:
order_array = np.array([order.qty, order.price])
if order.side == sides.BUY:
# find and set order to [0, 0] (same as removing it)
for index, item in enumerate(self.buy_orders[base_asset]):
if item[0] == order.qty and item[1] == order.price:
self.buy_orders[base_asset][index] = np.array([0, 0])
break
item_index = np.where(np.all(self.buy_orders[base_asset].array == order_array, axis=1))[0]
if len(item_index) > 0:
index = item_index[0]
self.buy_orders[base_asset].delete(index, axis=0)
else:
# find and set order to [0, 0] (same as removing it)
for index, item in enumerate(self.sell_orders[base_asset]):
if item[0] == order.qty and item[1] == order.price:
self.sell_orders[base_asset][index] = np.array([0, 0])
break
item_index = np.where(np.all(self.sell_orders[base_asset].array == order_array, axis=1))[0]
if len(item_index) > 0:
index = item_index[0]
self.sell_orders[base_asset].delete(index, axis=0)

def on_order_cancellation(self, order: Order) -> None:
if jh.is_livetrading():
Expand All @@ -153,20 +152,17 @@ def on_order_cancellation(self, order: Order) -> None:
self.available_assets[base_asset] -= order.qty
# self.available_assets[quote_asset] += order.qty * order.price
if not order.reduce_only:
order_array = np.array([order.qty, order.price])
if order.side == sides.BUY:
# find and set order to [0, 0] (same as removing it)
for index in range(len(self.buy_orders[base_asset]) - 1, -1, -1):
item = self.buy_orders[base_asset][index]
if item[0] == order.qty and item[1] == order.price:
self.buy_orders[base_asset][index] = np.array([0, 0])
break
item_index = np.where(np.all(self.buy_orders[base_asset].array == order_array, axis=1))[0]
if len(item_index) > 0:
index = item_index[0]
self.buy_orders[base_asset].delete(index, axis=0)
else:
# find and set order to [0, 0] (same as removing it)
for index in range(len(self.sell_orders[base_asset]) - 1, -1, -1):
item = self.sell_orders[base_asset][index]
if item[0] == order.qty and item[1] == order.price:
self.sell_orders[base_asset][index] = np.array([0, 0])
break
item_index = np.where(np.all(self.sell_orders[base_asset].array == order_array, axis=1))[0]
if len(item_index) > 0:
index = item_index[0]
self.sell_orders[base_asset].delete(index, axis=0)

def update_from_stream(self, data: dict) -> None:
"""
Expand Down
Loading

0 comments on commit e6126da

Please sign in to comment.